Hi, Here's another example.
{"lotto": { "id": 5, "winning-numbers": [2,45,34,23,7,5,3], "draw-date": "2009-09-14T18:00:00Z", "winners": [ {"winner-id": 23, "numbers": [2,45,34,23,3,5] }, {"winner-id": 54, "numbers":[52,3,12,11,18,22] } ] } } At the moment I'm extracting this with following case class structure. case class Winner(@path("winner-id") id: Long, numbers: List[Int]) case class Lotto(id: Long, @path("winning-numbers") winningNumbers: List[Int], winners: List[Winner], @path("draw-date") drawDate: Option[Date]) Using a trait approach it would be something like: case class Winner(id: Long with WinnerIdNominator, numbers: List[Int]) case class Lotto(id: Long, winningNumbers: List[Int] with WinningNumbersNominator, winners: List[Winner], drawDate: Option[Date] with DrawDateNominator) trait WinnerIdNominator extends Nominator { def name = "winner-id" } trait WinningNumbersNominator extends Nominator { def name = "winning-numbers" } trait DrawDateNominator extends Nominator { def name = "draw-date" } Now, there's some problems. 1. Mixin a trait with a primitive fails: scala> case class Winner(id: Long with WinnerIdNominator, numbers: List [Int]) <console>:5: error: ambiguous reference to overloaded definition, both method == in class Object of type (AnyRef)Boolean and method == in class Long of type (Long)Boolean match argument types (Long with WinnerIdNominator) case class Winner(id: Long with WinnerIdNominator, numbers: List [Int]) 2. How to get access to Nominator.name using reflection? When extracting values we have json and type of target instance, in this example classOf[Lotto]. It is not clear to me how those traits should be reflected at runtime. Cheers Joni On Sep 14, 1:31 am, "marius d." <marius.dan...@gmail.com> wrote: > On Sep 13, 3:15 pm, Joni Freeman <freeman.j...@gmail.com> wrote: > > > > > Hi, > > > That annotation is used to configure the json path when extracting > > values. By default the extraction code assumes that case class > > parameter names match with json field names. For instance these match: > > > case class Foo(bar: String, baz: Int) > > { "bar": "qwerty", "baz": 10 } > > > But sometimes json field names can contain characters which are not > > allowed in Scala identifiers. For example: > > { "foo-bar": "qwerty", "baz": 10 } > > > Now, to able to extract this we have to somehow tell the extractor the > > exact path explicitly. Currently @path annotation is used for that: > > case class Foo(@path("foo-bar") bar: String, baz: Int) > > > I don't see how a trait can accomplish this, maybe I'm missing > > something? > > > The reason why it is in Java is that Scala annotations are not > > accessible at runtime. > > Right but I'd also suggest removing Java code from Lift stack. The > above can be easily achieved by introducing a trait such as: > > case class Foo(bar: String with Nominator, baz: Int) > > Lift is a 100% Scala code with zero Java code. We also have strong > opinions in the team that we should stay away from annotations. > > one option would be something like this: > > Lift would have : > > trait Nominator{ > def name : String > > } > > In user's code: > > case class Foo(bar: String with MyNominator, baz: Int) > > trait MyNominator extends Nominator { > def name = "foo-bar" > > } > > Yes it is more verbose then the annotation but IMHO it is more Scala- > ish & Lift-ish. > > > > > Cheers Joni > > > On Sep 13, 11:03 pm, Timothy Perrett <timo...@getintheloop.eu> wrote: > > > > Just had a browse over the latest commit and found the following in > > > path.java: > > > > @Retention(RetentionPolicy.RUNTIME) > > > @Target(ElementType.TYPE) > > > public @interface path { > > > public String value(); > > > > } > > > > Any reason were not using a trait etc to complete the same > > > functionality? > > > > Cheers, Tim > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~----------~----~----~----~------~----~------~--~---