Search in sources :

Example 16 with Value

use of com.google.api.expr.v1alpha1.Value in project appengine-java-standard by GoogleCloudPlatform.

the class DataTypeTranslator method toV1Value.

private static Value.Builder toV1Value(@Nullable Object value) {
    boolean indexed = true;
    boolean forceIndexedEmbeddedEntity = false;
    if (value instanceof WrappedValue) {
        WrappedValue wrappedValue = (WrappedValue) value;
        indexed = wrappedValue.isIndexed();
        forceIndexedEmbeddedEntity = wrappedValue.getForceIndexedEmbeddedEntity();
        value = wrappedValue.getValue();
    }
    if (value instanceof Collection<?>) {
        Collection<?> values = (Collection<?>) value;
        if (values.isEmpty()) {
            if (DatastoreServiceConfig.getEmptyListSupport()) {
                return Value.newBuilder().setExcludeFromIndexes(!indexed).setArrayValue(ArrayValue.getDefaultInstance());
            } else {
                // null and empty list is lost.
                return toV1Value(null, indexed, forceIndexedEmbeddedEntity);
            }
        } else {
            Value.Builder valueBuilder = Value.newBuilder();
            for (Object listValue : values) {
                valueBuilder.getArrayValueBuilder().addValues(toV1Value(listValue, indexed, forceIndexedEmbeddedEntity));
            }
            return valueBuilder;
        }
    } else {
        return toV1Value(value, indexed, forceIndexedEmbeddedEntity);
    }
}
Also used : 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) Collection(java.util.Collection) WrappedValue(com.google.appengine.api.datastore.Entity.WrappedValue)

Example 17 with Value

use of com.google.api.expr.v1alpha1.Value 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 18 with Value

use of com.google.api.expr.v1alpha1.Value in project java-firestore by googleapis.

the class UserDataConverter method decodeValue.

static Object decodeValue(FirestoreRpcContext<?> rpcContext, Value v) {
    Value.ValueTypeCase typeCase = v.getValueTypeCase();
    switch(typeCase) {
        case NULL_VALUE:
            return null;
        case BOOLEAN_VALUE:
            return v.getBooleanValue();
        case INTEGER_VALUE:
            return v.getIntegerValue();
        case DOUBLE_VALUE:
            return v.getDoubleValue();
        case TIMESTAMP_VALUE:
            return Timestamp.fromProto(v.getTimestampValue());
        case STRING_VALUE:
            return v.getStringValue();
        case BYTES_VALUE:
            return Blob.fromByteString(v.getBytesValue());
        case REFERENCE_VALUE:
            String pathName = v.getReferenceValue();
            return new DocumentReference(rpcContext, ResourcePath.create(pathName));
        case GEO_POINT_VALUE:
            return new GeoPoint(v.getGeoPointValue().getLatitude(), v.getGeoPointValue().getLongitude());
        case ARRAY_VALUE:
            List<Object> list = new ArrayList<>();
            List<Value> lv = v.getArrayValue().getValuesList();
            for (Value iv : lv) {
                list.add(decodeValue(rpcContext, iv));
            }
            return list;
        case MAP_VALUE:
            Map<String, Object> outputMap = new HashMap<>();
            Map<String, Value> inputMap = v.getMapValue().getFieldsMap();
            for (Map.Entry<String, Value> entry : inputMap.entrySet()) {
                outputMap.put(entry.getKey(), decodeValue(rpcContext, entry.getValue()));
            }
            return outputMap;
        default:
            throw FirestoreException.forInvalidArgument(String.format("Unknown Value Type: %s", typeCase));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MapValue(com.google.firestore.v1.MapValue) Value(com.google.firestore.v1.Value) ArrayValue(com.google.firestore.v1.ArrayValue) NullValue(com.google.protobuf.NullValue) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with Value

use of com.google.api.expr.v1alpha1.Value in project java-firestore by googleapis.

the class Query method encodeValue.

private Value encodeValue(FieldPath fieldPath, Object value) {
    Object sanitizedObject = CustomClassMapper.serialize(value);
    Value encodedValue = UserDataConverter.encodeValue(fieldPath, sanitizedObject, UserDataConverter.ARGUMENT);
    if (encodedValue == null) {
        throw FirestoreException.forInvalidArgument("Cannot use Firestore sentinels in FieldFilter or cursors");
    }
    return encodedValue;
}
Also used : AutoValue(com.google.auto.value.AutoValue) AttributeValue(io.opencensus.trace.AttributeValue) Value(com.google.firestore.v1.Value) Int32Value(com.google.protobuf.Int32Value)

Example 20 with Value

use of com.google.api.expr.v1alpha1.Value in project java-firestore by googleapis.

the class DocumentSnapshot method extractField.

/**
 * Returns the Value Proto at 'fieldPath'. Returns null if the field was not found.
 */
@Nullable
Value extractField(@Nonnull FieldPath fieldPath) {
    Value value = null;
    if (fields != null) {
        Iterator<String> components = fieldPath.getSegments().iterator();
        value = fields.get(components.next());
        while (value != null && components.hasNext()) {
            if (value.getValueTypeCase() != Value.ValueTypeCase.MAP_VALUE) {
                return null;
            }
            value = value.getMapValue().getFieldsOrDefault(components.next(), null);
        }
    }
    return value;
}
Also used : Value(com.google.firestore.v1.Value) Nullable(javax.annotation.Nullable)

Aggregations

Test (org.junit.Test)126 Value (com.google.firestore.v1.Value)108 ArrayValue (com.google.firestore.v1.ArrayValue)73 LinkedHashSet (java.util.LinkedHashSet)71 ObjectValue (com.google.firebase.firestore.model.ObjectValue)53 NullValue (com.google.protobuf.NullValue)50 MapValue (com.google.firestore.v1.MapValue)47 ArrayList (java.util.ArrayList)33 HashMap (java.util.HashMap)25 Value (com.google.datastore.v1.Value)20 Map (java.util.Map)20 List (java.util.List)18 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)17 Record (org.apache.avro.generic.GenericData.Record)16 SchemaAndRecord (org.apache.beam.sdk.io.gcp.bigquery.SchemaAndRecord)16 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)16 Set (java.util.Set)14 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)13 Nullable (androidx.annotation.Nullable)10 Value (com.google.privacy.dlp.v2.Value)9