从电梯中的任何映射器中按名称检索字段

发布于 2024-12-18 03:24:23 字数 679 浏览 5 评论 0原文

我们如何从任何映射器检索所需的字段,以便在查询参数中使用它。

就我而言,我想找到我的“所需字段”具有 value = somevalue 的记录。

它尝试了以下方法

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")

}

,但随后它强制我在 asInstanceOf[MappedField[_,Users]] 中指定实际的映射器(Users 实例在此处传递)。我想让它适用于任何映射器,而不仅仅是“用户”。

它不适用于 asInstanceOf[MappedField[_,_]]

我了解每个映射器可能没有我想要的字段,在这种情况下,如果 .fieldByName() code> 函数应该抛出一些异常,这是可以接受的。但至少对于那些已经提交申请的人来说,它应该有效。

任何人都可以帮助我吗...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除→记忆 2024-12-25 03:24:23
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])
}

当你打电话时你能做这样的事情吗:

foo[Users,String](Users)
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)
失去的东西太少 2024-12-25 03:24:23

与其使用一个超级方法来完成所有这些事情,为什么不将查找函数作为参数传递呢?

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

并称之为:

foo[User](() => User.find(By(User.userName, "dummy"))

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"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文