Question

How do we retrieve desired filed from any mapper so as to use it in Query parameter.

In my case i want to find records where my 'desired-field' is having value = somevalue.

It tried the following way

foo(Users)

// foo defined here...

def foo ( modelObject:Mapper[_])={

    var field =modelObject.fieldByName("UserName").openTheBox.asInstanceOf[MappedField[_,Users]]
    var requiredUser = modelObject.find(By(field, "dummyUser")

}

but then it forces me to specify the actual Mapper in the asInstanceOf[MappedField[_,Users]] ( Users instance is passed here ). I want to make it work for any Mapper and not just 'Users'.

It doesn't work with asInstanceOf[MappedField[_,_]]

I understand that each Mapper may not have the field that i want and In that case if the .fieldByName() function should throw some exception, it's acceptable. But at least for those having the filed, it should work.

can anyone help me in this...

Was it helpful?

Solution

def foo[A<: Mapper[A],T](modelObject: A) = {
  val field = modelObject.asInstanceOf[Mapper[A]].fieldByName("userName").openTheBox.asInstanceOf[MappedField[T, A]]
  modelObject.asInstanceOf[MetaMapper[A]].find(By(field, "dummyUser".asInstanceOf[T])
}

Can you do something like this when you call:

foo[Users,String](Users)

OTHER TIPS

Instead of having a super method that does all that stuff why don't you pass the lookup function as parameter.

def foo[T](find: () => Option[T]) : Option[T] =  find()

and call this:

foo[User](() => User.find(By(User.userName, "dummy"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top