Search in sources :

Example 1 with WrappedValueImpl

use of com.google.appengine.api.datastore.Entity.WrappedValueImpl in project appengine-java-standard by GoogleCloudPlatform.

the class DataTypeTranslator method addPropertyToMap.

private static void addPropertyToMap(String name, Value value, Map<String, @Nullable Object> map) {
    boolean isOrContainsIndexedEntityValue = false;
    boolean indexed;
    Object result;
    if (DatastoreServiceConfig.getEmptyListSupport() && value.getValueTypeCase() == Value.ValueTypeCase.ARRAY_VALUE && value.getArrayValue().getValuesCount() == 0) {
        result = new ArrayList<Object>();
        indexed = !value.getExcludeFromIndexes();
    } else if (value.getArrayValue().getValuesCount() > 0) {
        indexed = false;
        // Must be ArrayList for backwards compatibility.
        ArrayList<@Nullable Object> resultList = new ArrayList<>(value.getArrayValue().getValuesCount());
        for (Value subValue : value.getArrayValue().getValuesList()) {
            if (subValue.getValueTypeCase() == ValueTypeCase.ARRAY_VALUE) {
                throw new IllegalArgumentException("Invalid Entity PB: list within a list.");
            }
            result = getValue(subValue);
            if (!subValue.getExcludeFromIndexes()) {
                // This is not optimal but it's needed to be "bug compatible" with v3.
                indexed = true;
                if (result instanceof EmbeddedEntity) {
                    isOrContainsIndexedEntityValue = true;
                }
            }
            resultList.add(result);
        }
        result = resultList;
    } else {
        // Empty list becomes null.
        indexed = !value.getExcludeFromIndexes();
        result = getValue(value);
        if (indexed && result instanceof EmbeddedEntity) {
            isOrContainsIndexedEntityValue = true;
        }
    }
    if (isOrContainsIndexedEntityValue) {
        result = new WrappedValueImpl(result, true, true);
    } else if (!indexed) {
        result = new UnindexedValue(result);
    }
    map.put(name, result);
}
Also used : ArrayList(java.util.ArrayList) UnindexedValue(com.google.appengine.api.datastore.Entity.UnindexedValue) UserValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.UserValue) PropertyValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue) ReferenceValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.ReferenceValue) NullValue(com.google.protobuf.NullValue) ArrayValue(com.google.datastore.v1.ArrayValue) Value(com.google.datastore.v1.Value) WrappedValue(com.google.appengine.api.datastore.Entity.WrappedValue) UnindexedValue(com.google.appengine.api.datastore.Entity.UnindexedValue) WrappedValueImpl(com.google.appengine.api.datastore.Entity.WrappedValueImpl) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with WrappedValueImpl

use of com.google.appengine.api.datastore.Entity.WrappedValueImpl in project appengine-java-standard by GoogleCloudPlatform.

the class EntityTest method testWrappedValue.

@Test
public void testWrappedValue() {
    WrappedValueImpl firstString = new WrappedValueImpl("string", true, true);
    WrappedValueImpl secondString = new WrappedValueImpl("string", true, false);
    WrappedValueImpl thirdString = new WrappedValueImpl("string", false, false);
    WrappedValueImpl firstInt = new WrappedValueImpl(1, true, true);
    WrappedValueImpl firstNull = new WrappedValueImpl(null, true, false);
    new EqualsTester().addEqualityGroup(firstString).addEqualityGroup(firstInt).addEqualityGroup(firstNull).testEquals();
    assertThat(secondString.hashCode()).isNotEqualTo(firstString.hashCode());
    assertThat(thirdString.hashCode()).isNotEqualTo(secondString.hashCode());
    assertThat(firstInt.hashCode()).isNotEqualTo(firstString.hashCode());
    assertThat(firstNull.hashCode()).isNotEqualTo(firstString.hashCode());
    assertThat(firstString.getValue()).isEqualTo("string");
    assertThat(firstInt.getValue()).isEqualTo(1);
    assertThat(firstNull.getValue()).isEqualTo(null);
    assertThat(firstString.isIndexed()).isTrue();
    assertThat(thirdString.isIndexed()).isFalse();
    assertThat(firstString.getForceIndexedEmbeddedEntity()).isTrue();
    assertThat(secondString.getForceIndexedEmbeddedEntity()).isFalse();
    assertThrows(IllegalArgumentException.class, () -> new WrappedValueImpl("string", false, true));
}
Also used : EqualsTester(com.google.common.testing.EqualsTester) WrappedValueImpl(com.google.appengine.api.datastore.Entity.WrappedValueImpl) Test(org.junit.Test)

Example 3 with WrappedValueImpl

use of com.google.appengine.api.datastore.Entity.WrappedValueImpl in project appengine-java-standard by GoogleCloudPlatform.

the class PropertyContainer method setPropertiesFrom.

/**
 * A convenience method that populates properties from those in the given container.
 *
 * <p>This method transfers information about unindexed properties and clones any mutable values.
 *
 * @param src The container from which we will populate ourself.
 */
public void setPropertiesFrom(PropertyContainer src) {
    for (Map.Entry<String, @Nullable Object> entry : src.getPropertyMap().entrySet()) {
        String name = entry.getKey();
        Object entryValue = entry.getValue();
        boolean isUnindexedValue = false;
        boolean isWrappedValue = false;
        boolean indexed = true;
        boolean forceIndexedEntityValue = false;
        Object valueToAdd = entryValue;
        if (entryValue instanceof UnindexedValue) {
            isUnindexedValue = true;
            indexed = false;
            valueToAdd = ((UnindexedValue) entryValue).getValue();
        } else if (entryValue instanceof WrappedValue) {
            WrappedValue wrappedValue = (WrappedValue) entryValue;
            isWrappedValue = true;
            indexed = wrappedValue.isIndexed();
            forceIndexedEntityValue = wrappedValue.getForceIndexedEmbeddedEntity();
            valueToAdd = wrappedValue.getValue();
        }
        // Clone collections
        if (valueToAdd instanceof Collection<?>) {
            // Convert all collections to an ArrayList, as that is the representation
            // returned from the datastore.
            Collection<?> srcColl = (Collection<?>) valueToAdd;
            Collection<Object> destColl = new ArrayList<Object>(srcColl.size());
            valueToAdd = destColl;
            for (Object element : srcColl) {
                destColl.add(cloneIfMutable(element));
            }
        } else {
            valueToAdd = cloneIfMutable(valueToAdd);
        }
        if (isUnindexedValue) {
            valueToAdd = new UnindexedValue(valueToAdd);
        } else if (isWrappedValue) {
            valueToAdd = new WrappedValueImpl(valueToAdd, indexed, forceIndexedEntityValue);
        }
        // Put the copy in the map
        getPropertyMap().put(name, valueToAdd);
    }
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) UnindexedValue(com.google.appengine.api.datastore.Entity.UnindexedValue) WrappedValueImpl(com.google.appengine.api.datastore.Entity.WrappedValueImpl) HashMap(java.util.HashMap) Map(java.util.Map) WrappedValue(com.google.appengine.api.datastore.Entity.WrappedValue)

Example 4 with WrappedValueImpl

use of com.google.appengine.api.datastore.Entity.WrappedValueImpl in project appengine-java-standard by GoogleCloudPlatform.

the class PropertyContainer method setIndexedProperty.

/**
 * Like {link #setProperty}, but requires that the value is indexable or a collection of indexable
 * values.
 */
public void setIndexedProperty(String propertyName, Object value) {
    DataTypeUtils.checkSupportedValue(propertyName, value, true, false, true);
    getPropertyMap().put(propertyName, new WrappedValueImpl(value, true, true));
}
Also used : WrappedValueImpl(com.google.appengine.api.datastore.Entity.WrappedValueImpl)

Example 5 with WrappedValueImpl

use of com.google.appengine.api.datastore.Entity.WrappedValueImpl in project appengine-java-standard by GoogleCloudPlatform.

the class DataTypeTranslator method addPropertyToMap.

private static void addPropertyToMap(Property property, boolean indexed, Map<String, @Nullable Object> map) {
    String name = property.getName();
    if (property.getMeaningEnum() == Meaning.EMPTY_LIST) {
        // Read an empty list, but user hasn't enabled empty list support.  In order to be
        // backward compatible, return null because thats what they used to get for empty lists.
        Object emptyListValue = DatastoreServiceConfig.getEmptyListSupport() ? new ArrayList<Object>() : null;
        map.put(name, wrapIfUnindexed(indexed, emptyListValue));
    } else {
        Object value = getPropertyValue(property);
        if (property.isMultiple()) {
            @SuppressWarnings({ "unchecked" }) List<Object> resultList = (List<Object>) PropertyContainer.unwrapValue(map.get(name));
            if (resultList == null) {
                resultList = new ArrayList<Object>();
                map.put(name, indexed ? resultList : new UnindexedValue(resultList));
            }
            if (indexed && value instanceof EmbeddedEntity) {
                map.put(name, new WrappedValueImpl(resultList, true, true));
            }
            resultList.add(value);
        } else {
            if (indexed && value instanceof EmbeddedEntity) {
                value = new WrappedValueImpl(value, true, true);
            } else if (!indexed) {
                value = new UnindexedValue(value);
            }
            map.put(name, value);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) UnindexedValue(com.google.appengine.api.datastore.Entity.UnindexedValue) ByteString(com.google.protobuf.ByteString) WrappedValueImpl(com.google.appengine.api.datastore.Entity.WrappedValueImpl)

Aggregations

WrappedValueImpl (com.google.appengine.api.datastore.Entity.WrappedValueImpl)6 UnindexedValue (com.google.appengine.api.datastore.Entity.UnindexedValue)3 ArrayList (java.util.ArrayList)3 WrappedValue (com.google.appengine.api.datastore.Entity.WrappedValue)2 Test (org.junit.Test)2 EqualsTester (com.google.common.testing.EqualsTester)1 ArrayValue (com.google.datastore.v1.ArrayValue)1 Value (com.google.datastore.v1.Value)1 ByteString (com.google.protobuf.ByteString)1 NullValue (com.google.protobuf.NullValue)1 PropertyValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue)1 ReferenceValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.ReferenceValue)1 UserValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.UserValue)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1