lundi 29 avril 2019

Is there a way to call a member in a generic type in Scala

I'm trying to call a member from a generic type and here is an original way to run the function

SourceStruct is the object that I want to replace with something like T.

override def process(t: SourceStruct, runtimeContext: RuntimeContext, requestIndexer: RequestIndexer): Unit = {
        val json = new util.HashMap[String, String]()
        json.put("time", t.rating.toString)
        json.put("userId", t.userId.id.toString)
        json.put("itemId", t.itemId.id)

        requestIndexer.add(request)
      }


when I replace the SourceStruct with T, how can I call the t.userId?

override def process(t: T, runtimeContext: RuntimeContext, requestIndexer: RequestIndexer): Unit = {
      val json = new util.HashMap[String, String]()
//      json.put("userId", t.userId.id)
//      json.put("itemId", t.itemId.id)
//      json.put("rating", t.rating.toString)

      val request = Requests
        .indexRequest()
        .index(index)
        .`type`(indexType)
        .source(json)
      requestIndexer.add(request)
    }

thanks in advance

I can get the member by using

typeOf[T].members.collect {
case m: MethodSymbol if m.isCaseAccessor => m
}.toList

but still no clue about the question above





Aucun commentaire:

Enregistrer un commentaire