Thursday, February 2, 2012

AppEngine Go SDK's 500-byte string limitation

AppEngine Go SDK has an undocumented limitation of datastore strings: they must not exceed 500 bytes. The dev_appserver will happily accept Go structs with strings longer than this limit, but when you try to deploy your app or list datastore entities from the development console, you will encounter an error like this:

BadValueError: Property Value is 501 bytes long; it must be 500 or less. Consider Text instead, which can store strings of any length.

To store long strings in the datastore, you'll need to manually convert a string type to a byte slice ([]byte), which will be stored as an unindexed blob. For example:

type T struct {
  LongString []byte

func store(longString string, r *http.Request) os.Error {
  ctx := appengine.NewContext(r)
  key := datastore.NewIncompleteKey(ctx, "T", nil)
  t := new(T)
  t.LongString = []byte(longString)
  _, e := datastore.Put(ctx, key, t)
  return e