Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow autoconversion of various types into Text (just like String).
  • Loading branch information
stickfigure committed Jan 13, 2014
commit 2184a9ffac864ae1cdb0e6d1a9c794dc7c39b1fa
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.googlecode.objectify.impl.translate;

import java.lang.reflect.Type;

import com.google.appengine.api.datastore.Text;
import com.googlecode.objectify.impl.Path;
import com.googlecode.objectify.impl.Property;


/**
* Knows how to convert Texts. Aside from String and Text, will work with anything that's in the
* datastore just by calling toString() on what we get back; convenient for converting between
* say Number and the Text representation, possibly dangerous otherwise.
*/
public class TextTranslatorFactory extends ValueTranslatorFactory<Text, Object>
{
/** */
public TextTranslatorFactory() {
super(Text.class);
}

@Override
protected ValueTranslator<Text, Object> createSafe(Path path, Property property, Type type, CreateContext ctx)
{
return new ValueTranslator<Text, Object>(path, Object.class) {
@Override
protected Text loadValue(Object value, LoadContext ctx) {
if (value instanceof Text)
return (Text)value;
else
return new Text(value.toString());
}

@Override
protected Object saveValue(Text value, SaveContext ctx) {
return value;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public TranslatorRegistry(ObjectifyFactory fact)
this.insertPoint = this.translators.size();

this.translators.add(new StringTranslatorFactory());
this.translators.add(new TextTranslatorFactory());
this.translators.add(new NumberTranslatorFactory());
this.translators.add(new KeyTranslatorFactory());
this.translators.add(new RefTranslatorFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Text;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.SaveException;
import com.googlecode.objectify.annotation.Cache;
Expand Down Expand Up @@ -213,6 +214,51 @@ public void testBigStringsWithIndexInversion() throws Exception

assert fetched.string.equals(BIG_STRING);
}

/** */
@com.googlecode.objectify.annotation.Entity
@Cache
public static class HasText
{
public @Id Long id;
public Text text;
}

/**
* Stored Strings can be converted to Text in the data model
*/
@Test
public void stringsCanBeConvertedToText() throws Exception
{
fact().register(HasText.class);

Entity ent = new Entity(Key.getKind(HasText.class));
ent.setProperty("text", "foo"); // setting a string
ds().put(null, ent);

Key<HasText> key = Key.create(ent.getKey());
HasText fetched = ofy().load().key(key).now();

assert fetched.text.getValue().equals("foo");
}

/**
* Stored numbers can be converted to Text in the data model
*/
@Test
public void numbersCanBeConvertedToText() throws Exception
{
fact().register(HasText.class);

Entity ent = new Entity(Key.getKind(HasText.class));
ent.setProperty("text", 2); // setting a number
ds().put(null, ent);

Key<HasText> key = Key.create(ent.getKey());
HasText fetched = ofy().load().key(key).now();

assert fetched.text.getValue().equals("2");
}

/** */
@com.googlecode.objectify.annotation.Entity
Expand Down