Skip to content

Commit

Permalink
#222 Text coercion based on datastore indexed String limit fix (500 c…
Browse files Browse the repository at this point in the history
…hars->1500 bytes)
  • Loading branch information
Timothy committed May 31, 2018
1 parent fe87bbf commit b622510
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
13 changes: 8 additions & 5 deletions core/src/main/groovyx/gaelyk/datastore/DatastoreEntityCoercion.java 100644 → 100755
@@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions core/src/main/groovyx/gaelyk/extensions/DatastoreExtensions.java 100644 → 100755
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down

0 comments on commit b622510

Please sign in to comment.