Try this:
/** Enum data field for JSON records. Encodes as JString */
class JSONEnumNameField[OwnerType <: JSONRecord[OwnerType], EnumType <:
Enumeration]
(rec: OwnerType, enum: EnumType)(implicit enumValueType:
Manifest[EnumType#Value])
extends EnumField[OwnerType, EnumType](rec, enum) with JSONField
{
def this(rec: OwnerType, enum: EnumType, value: EnumType#Value)(implicit
enumValueType: Manifest[EnumType#Value]) = {
this(rec, enum)
set(value)
}
def this(rec: OwnerType, enum: EnumType, value: Box[EnumType#Value])(implicit
enumValueType: Manifest[EnumType#Value]) = {
this(rec, enum)
setBox(value)
}
def asJValue: JValue = valueBox.map(v => JString(v.toString)) openOr
(JNothing: JValue)
def fromJValue(jvalue: JValue): Box[EnumType#Value] = jvalue match {
case JNothing|JNull if optional_? => setBox(Empty)
case JString(s) => setBox(enum.valueOf(s) ?~ ("Unknown
value \"" + s + "\""))
case other => setBox(expectedA("JString", other))
}
}
Let me know if it works for you. If so, I'll start the process of getting it
into master as soon as I can.
-Ross
On Mar 3, 2010, at 8:12 PM, Craig Blake wrote:
> Sure, will do. The only thing I think I'll need to figure out is how to
> persist an enumeration by name rather than ordinal value, but I imagine that
> it should be pretty straight-forward to add a new field type in my app to
> handle it.
>
> Thanks,
> Craig
>
> On Mar 3, 2010, at 7:45 PM, Ross Mellgren wrote:
>
>> It's no problem, as I mentioned the compiler error is practically useless.
>>
>> Hope you get along well, let me know if you have any other issues.
>>
>> -Ross
>>
>> On Mar 3, 2010, at 7:29 PM, Craig Blake wrote:
>>
>>> Yep, that seems to be better. Sorry for the noise, I don't know why I
>>> didn't think to check that.
>>>
>>> Thanks for the quick answer.
>>>
>>> Craig
>>>
>>> On Mar 3, 2010, at 4:44 PM, Ross Mellgren wrote:
>>>
>>>> Unfortunately the compiler error is bizarre (due to some of the type
>>>> shuffling involved), but the underlying problem you're experiencing is
>>>> that DateTimeFields (and therefore JSONDateTimeFields) have a storage type
>>>> of Calendar, and you're trying to assign a Date to them. Try
>>>> Calendar.getInstance instead of new Date() and see if that resolves it for
>>>> you?
>>>>
>>>> -Ross
>>>>
>>>> On Mar 3, 2010, at 4:32 PM, Craig Blake wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I am getting familiar with the lift-couchdb module, and trying to put
>>>>> together a sample based on the tests in the module. Trying to create a
>>>>> record, based on this test code:
>>>>>
>>>>> def testRec1: Person = Person.createRecord.name("Alice").age(25)
>>>>>
>>>>> this is what I have:
>>>>>
>>>>> class Account extends CouchRecord[Account] {
>>>>> def meta = Account
>>>>> object created extends JSONDateTimeField(this)
>>>>> }
>>>>> object Account extends Account with CouchMetaRecord[Account]
>>>>>
>>>>> ...
>>>>>
>>>>> val account = Account.createRecord.created(new Date())
>>>>>
>>>>>
>>>>> I get a compilation error:
>>>>>
>>>>> [WARNING] Test.scala:44: error: overloaded method value apply with
>>>>> alternatives ((net.liftweb.common.Box[_12.MyType])test.Account) forSome {
>>>>> val _12: object test.Account#created } <and> ((_13.MyType)test.Account)
>>>>> forSome { val _13: object test.Account#created } cannot be applied to
>>>>> (java.util.Date)
>>>>> [WARNING] val account = Account.createRecord.created( new
>>>>> Date())
>>>>>
>>>>>
>>>>> I'm sure that I am just missing something obvious. Any ideas what?
>>>>>
>>>>> Thanks,
>>>>> Craig
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google Groups
>>>>> "Lift" group.
>>>>> To post to this group, send email to [email protected].
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected].
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/liftweb?hl=en.
>>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google Groups
>>>> "Lift" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected].
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/liftweb?hl=en.
>>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Lift" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/liftweb?hl=en.
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Lift" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/liftweb?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Lift" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/liftweb?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en.