Permalink
Browse files

#222 Text coercion based on datastore indexed String limit fix (500 c…

…hars->1500 bytes)
  • Loading branch information...
Timothy
Timothy committed May 31, 2018
1 parent fe87bbf commit b622510430049ef83cf8c1b6dc8dc5abb54edbbb
@@ -1,12 +1,14 @@
package groovyx.gaelyk.datastore;
import groovy.lang.GString;
import groovyx.gaelyk.extensions.DatastoreExtensions;
import com.google.appengine.api.datastore.Entities;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Text;
import groovy.lang.GString;
import groovyx.gaelyk.extensions.DatastoreExtensions;
import static java.nio.charset.StandardCharsets.UTF_8;
public class DatastoreEntityCoercion {
public static enum Proxy {
@@ -79,9 +81,10 @@ public static Entity convert(DatastoreEntity<?> dsEntity){
private static Object transformValueForStorage(Object value) {
Object newValue = (value instanceof GString || value instanceof Enum<?>) ? value.toString() : value;
// if we store a string longer than 500 characters
// if we store a string longer than 1500 bytes
// it needs to be wrapped in a Text instance
if (newValue instanceof String && ((String)newValue).length() > 500) {
// See https://github.com/gaelyk/gaelyk/issues/222
if (newValue instanceof String && ((String)newValue).getBytes(UTF_8).length > 1500) {
newValue = new Text((String) newValue);
}
return newValue;
@@ -44,6 +44,8 @@
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.datastore.TransactionOptions;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Extension methods dedicated to the low-level DataStore service
*
@@ -145,7 +147,7 @@ public static Object get(Entity entity, String name) {
return transformValueForRetrieval(entity.getProperty(name));
}
static Object transformValueForRetrieval(Object value) {
public static Object transformValueForRetrieval(Object value) {
return value instanceof Text ? ((Text) value).getValue() : value;
}
@@ -171,9 +173,10 @@ public static Object transformValueForStorage(Object value) {
// the datastore doesn't allow to store GStringImpl
// so we need a toString() first
Object newValue = value instanceof GString ? value.toString() : value;
// if we store a string longer than 500 characters
// if we store a string longer than 1500 bytes
// it needs to be wrapped in a Text instance
if (newValue instanceof String && ((String) newValue).length() > 500) {
// See https://github.com/gaelyk/gaelyk/issues/222
if (newValue instanceof String && ((String)newValue).getBytes(UTF_8).length > 1500) {
newValue = new Text((String) newValue);
}
return newValue;

0 comments on commit b622510

Please sign in to comment.