[lift-couchdb] optionnal subrecord

5 views
Skip to first unread message

David Bernard

unread,
Sep 15, 2010, 4:36:05 PM9/15/10
to liftweb
Hi,

I would like to add test into lift-couchdb. but I don't know how to deal with the following.
I added subRecord into Person (see below), 

but

A JSON record should encode basic records correctly  Time elapsed: 0.002 sec  <<< FAILURE!
org.specs.runner.UserError: '{"address":{"city":"","country":"","postalCode":"","street":""},"age":25,"name":"Alice"}' is not equal to '{"age":25,"name":"Alice"}'

for 
    def testRec1: Person = Person.createRecord.name("Alice").age(25)
    val testDoc1: JObject = ("age" -> 25) ~ ("name" -> "Alice")

is it possible to set subRecord optional ? (currently it extends MandatoryTypedField[X])

----
package jsontestrecords {
  class Person private () extends JSONRecord[Person] {
    def meta = Person

    object name extends StringField(this, 200)
    object age extends IntField(this) {
      override def defaultValue = 0
    }
    object favoriteColor extends OptionalStringField(this, 200)
    object address extends JSONSubRecordField(this, Address, Empty) {
      override def optional_? = true
    }
  }

  object Person extends Person with JSONMetaRecord[Person]
  
  class Address extends JSONRecord[Address] {
 def meta = Address
//  object country extends CountryField(this)
//  object postalCode extends PostalCodeField(this, country)
 object country extends StringField(this, 60)
 object postalCode extends StringField(this, 10)
 object city extends StringField(this, 60)
 object street extends StringField(this, 200)
}
object Address extends Address with JSONMetaRecord[Address]
}
----

/davidB

Ross Mellgren

unread,
Sep 15, 2010, 4:38:50 PM9/15/10
to lif...@googlegroups.com
At some point Optional variants should be created, but MandatoryTypedFields can be made optional, contrary to their name (this is intentional)

override def optional_? = true


field.setBox(Empty)

-Ross


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

David Bernard

unread,
Sep 15, 2010, 4:45:51 PM9/15/10
to lif...@googlegroups.com
On Wed, Sep 15, 2010 at 22:38, Ross Mellgren <dri...@gmail.com> wrote:
At some point Optional variants should be created, but MandatoryTypedFields can be made optional, contrary to their name (this is intentional)

override def optional_? = true


field.setBox(Empty)

it's what I did, isn't it ? with

    object address extends JSONSubRecordField(this, Address, Empty) {
      override def optional_? = true
    }

I'll investigate later tomorrow.

Thanks.

Ross Mellgren

unread,
Sep 15, 2010, 4:52:02 PM9/15/10
to lif...@googlegroups.com
Oh sorry, didn't read all the way down :-/

It should work -- are you sure you set the field value to Empty? Also, I don't see how this compiles -- JSONSubRecordField takes three arguments, two as the explicit argument list (rec, valueMeta), and the third is a Manifest, so I don't know how you're passing in Empty. In any case, it looks like you may be assuming the third argument is the default value... the proper way to set a default value is:

override def defaultValueBox = Empty

-Ross

David Bernard

unread,
Sep 15, 2010, 4:57:36 PM9/15/10
to lif...@googlegroups.com
I'll try

currently I think using 
class JSONSubRecordField ....

  def this(rec: OwnerType, valueMeta: JSONMetaRecord[SubRecordType], value: Box[SubRecordType])
          (implicit subRecordType: Manifest[SubRecordType]) = {
      this(rec, valueMeta)
      setBox(value)
  }
  ...
}

Ross Mellgren

unread,
Sep 15, 2010, 5:02:53 PM9/15/10
to lif...@googlegroups.com
Apparently I took stupid pills today. Can you push the branch so I can take a look?

-Ross

David Bernard

unread,
Sep 16, 2010, 3:23:51 AM9/16/10
to lif...@googlegroups.com
pushed (with failed test) in branch davidb_issue_649.

2 Failed test :

A JSON record should encode basic records correctly  Time elapsed: 0.002 sec  <<< FAILURE!
org.specs.runner.UserError: '{"address":{"city":"","country":"","postalCode":"","street":""},"age":25,"name":"Alice"}' is not equal to '{"age":25,"name":"Alice"}'
at net.liftweb.couchdb.JSONRecordTestSpecs$$anonfun$1$$anonfun$apply$8.apply(JSONRecordSpecs.scala:90)
at net.liftweb.couchdb.JSONRecordTestSpecs$$anonfun$1$$anonfun$apply$8.apply(JSONRecordSpecs.scala:90)
at org.specs.specification.LifeCycle$class.withCurrent(ExampleLifeCycle.scala:60)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)

A JSON record should preserve extra fields from JSON  Time elapsed: 0.001 sec  <<< FAILURE!
org.specs.runner.UserError: '{"a[ddress":{"city":"","country":"","postalCode":"","street":""},"a]ge":1,"extra1":"valu... is not equal to '{"a[]ge":1,"extra1":"valu...
at net.liftweb.couchdb.JSONRecordTestSpecs$$anonfun$1$$anonfun$apply$13.apply(JSONRecordSpecs.scala:112)
at net.liftweb.couchdb.JSONRecordTestSpecs$$anonfun$1$$anonfun$apply$13.apply(JSONRecordSpecs.scala:104)

/davidB

Ross Mellgren

unread,
Sep 16, 2010, 12:45:56 PM9/16/10
to lif...@googlegroups.com
So there is definitely a bug. I think it's probably a regression from the screen handling the gummed up some of the trickier corner cases in the optional field support, because during set_! processing it needs to convert to ValueType temporarily. Here's where I think the problem is:

  protected def setFilterBox: List[Box[MyType] => Box[MyType]] = ((in: Box[MyType]) => toBoxMyType(setFilter.foldLeft(toValueType(in))((prev, f) => f(prev)))) :: Nil

I'm not going to have time to fix this in the next couple days, but a ticket should probably be opened for it.

Thanks for pushing the branch.

-Ross

David Pollak

unread,
Sep 16, 2010, 12:49:18 PM9/16/10
to lif...@googlegroups.com
On Thu, Sep 16, 2010 at 9:45 AM, Ross Mellgren <dri...@gmail.com> wrote:
So there is definitely a bug. I think it's probably a regression from the screen handling the gummed up some of the trickier corner cases in the optional field support, because during set_! processing it needs to convert to ValueType temporarily. Here's where I think the problem is:

  protected def setFilterBox: List[Box[MyType] => Box[MyType]] = ((in: Box[MyType]) => toBoxMyType(setFilter.foldLeft(toValueType(in))((prev, f) => f(prev)))) :: Nil

Is this something we need to fix for 2.1?



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics

Ross Mellgren

unread,
Sep 16, 2010, 12:52:48 PM9/16/10
to lif...@googlegroups.com
I would tentatively say "no", since this is the first reported issue with it, and it's a bit synthetic. However, I think the priority needs to be determined by those using record in 2.1 -- Harry and Jorge come to mind.

-Ross

David Bernard

unread,
Sep 16, 2010, 1:11:09 PM9/16/10
to lif...@googlegroups.com
Currently I use record but I'm not in live (I'll use 2.2-x) but I don't have optionals fields / subrecord.So, in my point of view, it's not blocker nor urgent.
More, I'm not able to fix (as I don't understand the line ;-) ).

Thanks for help.
After 2.1 release, when it'll be fixe, I'll update the wiki.

/davidB

Ross Mellgren

unread,
Sep 20, 2010, 2:23:21 AM9/20/10
to lif...@googlegroups.com
The record optional support should be fixed with http://reviewboard.liftweb.net/r/448/

Can you try your case against rmm_wip_652?

-Ross
Reply all
Reply to author
Forward
0 new messages