views:

315

answers:

4

I haven't found any reason for the existence of a few of the App Engine classes. There's a PhoneNumber, a Link, a PostalAddress, a GeoPt, a Rating, etc. Why are these given special treatment? They don't seem to have any smarts - e.g. geo searching. I know Link has more space than a String property, but the rest?

See: http://code.google.com/appengine/docs/java/datastore/dataclasses.html

+2  A: 

I think they're mostly just there to cover common cases and save developers time. If a lot of apps use a phone number field, why require each developer to have to write them? A developer can still write their own if they need/want to.

Jason Hall
Agreed, except the cost seems to be junk added to the API with very little benefit. We don't need a phone type - String works perfectly and now we have to getValue out of the PhoneNumber.
Richard Watson
Really? Can String handle the differences betweeen US and UK phone numbers without outside code? Disclaimer: Never used Those classes so can't tell if they do either.
Macha
Macha, unfortunately they don't, which is the whole point. They're just wrappers for Strings.
Domchi
+4  A: 

Those types are 'semantic' types. They're present in the Java API for parity with the Python API. In the Python API, they define special behaviour with regards to the .to_xml() method - for example, a PhoneNumberProperty serializes like this:

<property name="foo" type="gd:phonenumber"><gd:phoneNumber>12345-678</gd:phoneNumber></property>
Nick Johnson
Would you say there's any benefit in usage? Do we have to import the classes into queries?
Richard Watson
I wouldn't bother with them, personally. Queries don't use the classes at all, as far as I'm aware.
Nick Johnson
Great, thank you.
Richard Watson
+1  A: 

Basically using this types in your models allows to add indirect meta data to your code. This may be useful if you are working with any kind of universal renderer for your model classes or if you are performing validation of user input on your models.

For example if you are using PhoneNumber type for a field named userNumber you reflection based renderer may understand that it should automatically assign corresponding validator to text field which will represent it.

Regards, Pavel.

Null Pointer
+1  A: 

Not sure about java, but in python the following model/code (tested on dev server) will throw BadValueError, with the message "Invalid URL: stackoverflow.com"

class foo(db.model):
    link = db.LinkProperty()

bar = foo()
bar.link = 'stackoverflow.com'

While:

bar.link = 'http://stackoverflow.com'

Works fine.

I haven't tested, but the other properties may or may not also do validation.

wf