Search in sources :

Example 36 with Value

use of com.google.firestore.v1beta1.Value in project cel-java by projectnessie.

the class ConformanceServiceImpl method valueToRefValue.

/**
 * ValueToRefValue converts between exprpb.Value and ref.Val.
 */
static Val valueToRefValue(TypeAdapter adapter, Value v) {
    switch(v.getKindCase()) {
        case NULL_VALUE:
            return NullT.NullValue;
        case BOOL_VALUE:
            return boolOf(v.getBoolValue());
        case INT64_VALUE:
            return intOf(v.getInt64Value());
        case UINT64_VALUE:
            return uintOf(v.getUint64Value());
        case DOUBLE_VALUE:
            return doubleOf(v.getDoubleValue());
        case STRING_VALUE:
            return stringOf(v.getStringValue());
        case BYTES_VALUE:
            return bytesOf(v.getBytesValue().toByteArray());
        case OBJECT_VALUE:
            Any any = v.getObjectValue();
            return adapter.nativeToValue(any);
        case MAP_VALUE:
            MapValue m = v.getMapValue();
            Map<Val, Val> entries = new HashMap<>();
            for (Entry entry : m.getEntriesList()) {
                Val key = valueToRefValue(adapter, entry.getKey());
                Val pb = valueToRefValue(adapter, entry.getValue());
                entries.put(key, pb);
            }
            return adapter.nativeToValue(entries);
        case LIST_VALUE:
            ListValue l = v.getListValue();
            List<Val> elts = l.getValuesList().stream().map(el -> valueToRefValue(adapter, el)).collect(Collectors.toList());
            return adapter.nativeToValue(elts);
        case TYPE_VALUE:
            String typeName = v.getTypeValue();
            Type tv = Types.getTypeByName(typeName);
            if (tv != null) {
                return tv;
            }
            return newObjectTypeValue(typeName);
        default:
            throw new IllegalArgumentException("unknown value " + v.getKindCase());
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) MapValue(com.google.api.expr.v1alpha1.MapValue) Lister(org.projectnessie.cel.common.types.traits.Lister) EnvOption.container(org.projectnessie.cel.EnvOption.container) CheckRequest(com.google.api.expr.v1alpha1.CheckRequest) Err(org.projectnessie.cel.common.types.Err) True(org.projectnessie.cel.common.types.BoolT.True) Err.isError(org.projectnessie.cel.common.types.Err.isError) Ast(org.projectnessie.cel.Ast) CheckResponse(com.google.api.expr.v1alpha1.CheckResponse) Map(java.util.Map) Value(com.google.api.expr.v1alpha1.Value) UnknownSet(com.google.api.expr.v1alpha1.UnknownSet) Val(org.projectnessie.cel.common.types.ref.Val) PrintWriter(java.io.PrintWriter) IteratorT(org.projectnessie.cel.common.types.IteratorT) DoubleT.doubleOf(org.projectnessie.cel.common.types.DoubleT.doubleOf) Status(com.google.rpc.Status) EnvOption.clearMacros(org.projectnessie.cel.EnvOption.clearMacros) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) EnvOption(org.projectnessie.cel.EnvOption) ExprValue(com.google.api.expr.v1alpha1.ExprValue) Collectors(java.util.stream.Collectors) IntT.intOf(org.projectnessie.cel.common.types.IntT.intOf) UintT.uintOf(org.projectnessie.cel.common.types.UintT.uintOf) ByteString(com.google.protobuf.ByteString) List(java.util.List) ErrorSet(com.google.api.expr.v1alpha1.ErrorSet) UnknownT.unknownOf(org.projectnessie.cel.common.types.UnknownT.unknownOf) Type(org.projectnessie.cel.common.types.ref.Type) Any(com.google.protobuf.Any) EvalResult(org.projectnessie.cel.Program.EvalResult) ParseRequest(com.google.api.expr.v1alpha1.ParseRequest) Err.newErr(org.projectnessie.cel.common.types.Err.newErr) Env.newEnv(org.projectnessie.cel.Env.newEnv) Entry(com.google.api.expr.v1alpha1.MapValue.Entry) ParseResponse(com.google.api.expr.v1alpha1.ParseResponse) StdLib(org.projectnessie.cel.Library.StdLib) EnvOption.declarations(org.projectnessie.cel.EnvOption.declarations) ConformanceServiceImplBase(com.google.api.expr.v1alpha1.ConformanceServiceGrpc.ConformanceServiceImplBase) HashMap(java.util.HashMap) Timestamp(com.google.protobuf.Timestamp) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Mapper(org.projectnessie.cel.common.types.traits.Mapper) Types(org.projectnessie.cel.common.types.Types) IssueDetails(com.google.api.expr.v1alpha1.IssueDetails) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) EvalRequest(com.google.api.expr.v1alpha1.EvalRequest) Code(com.google.rpc.Code) StringWriter(java.io.StringWriter) EnvOption.types(org.projectnessie.cel.EnvOption.types) NullT(org.projectnessie.cel.common.types.NullT) Types.boolOf(org.projectnessie.cel.common.types.Types.boolOf) TypeAdapter(org.projectnessie.cel.common.types.ref.TypeAdapter) UnknownT.isUnknown(org.projectnessie.cel.common.types.UnknownT.isUnknown) ListValue(com.google.api.expr.v1alpha1.ListValue) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) BytesT.bytesOf(org.projectnessie.cel.common.types.BytesT.bytesOf) CELError(org.projectnessie.cel.common.CELError) SourcePosition(com.google.api.expr.v1alpha1.SourcePosition) TypeT(org.projectnessie.cel.common.types.TypeT) Duration(com.google.protobuf.Duration) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) Program(org.projectnessie.cel.Program) EvalResponse(com.google.api.expr.v1alpha1.EvalResponse) TypeT.newObjectTypeValue(org.projectnessie.cel.common.types.TypeT.newObjectTypeValue) Message(com.google.protobuf.Message) Env(org.projectnessie.cel.Env) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) Entry(com.google.api.expr.v1alpha1.MapValue.Entry) Type(org.projectnessie.cel.common.types.ref.Type) HashMap(java.util.HashMap) ListValue(com.google.api.expr.v1alpha1.ListValue) MapValue(com.google.api.expr.v1alpha1.MapValue) ByteString(com.google.protobuf.ByteString) Any(com.google.protobuf.Any)

Example 37 with Value

use of com.google.firestore.v1beta1.Value in project auto-data-tokenize by GoogleCloudPlatform.

the class RecordUnflattener method unflatten.

/**
 * Returns an AVRO record by unflattening the FlatRecord through JSON unflattener.
 */
public GenericRecord unflatten(FlatRecord flatRecord) {
    Map<String, Object> jsonValueMap = Maps.newHashMap();
    for (Map.Entry<String, Value> entry : flatRecord.getValuesMap().entrySet()) {
        var valueProcessor = new ValueProcessor(entry);
        jsonValueMap.put(valueProcessor.cleanKey(), valueProcessor.convertedValue());
    }
    String unflattenedRecordJson = new JsonUnflattener(jsonValueMap).unflatten();
    return convertJsonToAvro(schema, unflattenedRecordJson);
}
Also used : JsonUnflattener(com.github.wnameless.json.unflattener.JsonUnflattener) Value(com.google.privacy.dlp.v2.Value) Map(java.util.Map)

Example 38 with Value

use of com.google.firestore.v1beta1.Value in project auto-data-tokenize by GoogleCloudPlatform.

the class ContactsFlatRecordSampleGenerator method buildContactRecords.

public ImmutableList<FlatRecord> buildContactRecords(int count) {
    ImmutableList.Builder<FlatRecord> recordListBuilder = ImmutableList.builder();
    for (int i = 0; i < count; i++) {
        HashMap<String, Value> valuesMap = Maps.newHashMap();
        HashMap<String, String> flatKeyMap = Maps.newHashMap();
        valuesMap.put("$.name", Value.newBuilder().setStringValue(randomName()).build());
        flatKeyMap.put("$.name", "$.name");
        final int numbers = new Random().nextInt(10);
        for (int n = 0; n < numbers; n++) {
            String key = "$.contacts[" + n + "].contact.number";
            flatKeyMap.put(key, "$.contacts.contact.number");
            valuesMap.put(key, Value.newBuilder().setStringValue(randomPhoneNumber(10)).build());
        }
        final int emails = new Random().nextInt(5);
        for (int n = 0; n < emails; n++) {
            String key = "$.emails[" + n + "]";
            flatKeyMap.put(key, "$.emails");
            valuesMap.put(key, Value.newBuilder().setStringValue(randomName()).build());
        }
        recordListBuilder.add(flatRecordBuilder().putAllValues(valuesMap).putAllFlatKeySchema(flatKeyMap).build());
    }
    return recordListBuilder.build();
}
Also used : Random(java.util.Random) ImmutableList(com.google.common.collect.ImmutableList) Value(com.google.privacy.dlp.v2.Value) FlatRecord(com.google.cloud.solutions.autotokenize.AutoTokenizeMessages.FlatRecord)

Example 39 with Value

use of com.google.firestore.v1beta1.Value in project firebase-android-sdk by firebase.

the class Target method getLowerBound.

/**
 * Returns a lower bound of field values that can be used as a starting point to scan the index
 * defined by {@code fieldIndex}. Returns {@code null} if no lower bound exists.
 */
@Nullable
public Bound getLowerBound(FieldIndex fieldIndex) {
    List<Value> values = new ArrayList<>();
    boolean inclusive = true;
    // For each segment, retrieve a lower bound if there is a suitable filter or startAt.
    for (FieldIndex.Segment segment : fieldIndex.getDirectionalSegments()) {
        Value segmentValue = null;
        boolean segmentInclusive = true;
        // Process all filters to find a value for the current field segment
        for (FieldFilter fieldFilter : getFieldFiltersForPath(segment.getFieldPath())) {
            Value filterValue = null;
            boolean filterInclusive = true;
            switch(fieldFilter.getOperator()) {
                case LESS_THAN:
                case LESS_THAN_OR_EQUAL:
                    filterValue = Values.getLowerBound(fieldFilter.getValue().getValueTypeCase());
                    break;
                case EQUAL:
                case IN:
                case GREATER_THAN_OR_EQUAL:
                    filterValue = fieldFilter.getValue();
                    break;
                case GREATER_THAN:
                    filterValue = fieldFilter.getValue();
                    filterInclusive = false;
                    break;
                case NOT_EQUAL:
                case NOT_IN:
                    filterValue = Values.MIN_VALUE;
                    break;
                default:
            }
            if (max(segmentValue, filterValue) == filterValue) {
                segmentValue = filterValue;
                segmentInclusive = filterInclusive;
            }
        }
        // if we can narrow the scope.
        if (startAt != null) {
            for (int i = 0; i < orderBys.size(); ++i) {
                OrderBy orderBy = this.orderBys.get(i);
                if (orderBy.getField().equals(segment.getFieldPath())) {
                    Value cursorValue = startAt.getPosition().get(i);
                    if (max(segmentValue, cursorValue) == cursorValue) {
                        segmentValue = cursorValue;
                        segmentInclusive = startAt.isInclusive();
                    }
                    break;
                }
            }
        }
        if (segmentValue == null) {
            // No lower bound exists
            return null;
        }
        values.add(segmentValue);
        inclusive &= segmentInclusive;
    }
    return new Bound(values, inclusive);
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) Value(com.google.firestore.v1.Value) ArrayList(java.util.ArrayList) Nullable(androidx.annotation.Nullable)

Example 40 with Value

use of com.google.firestore.v1beta1.Value in project firebase-android-sdk by firebase.

the class Target method getUpperBound.

/**
 * Returns an upper bound of field values that can be used as an ending point when scanning the
 * index defined by {@code fieldIndex}. Returns {@code null} if no upper bound exists.
 */
@Nullable
public Bound getUpperBound(FieldIndex fieldIndex) {
    List<Value> values = new ArrayList<>();
    boolean inclusive = true;
    // For each segment, retrieve an upper bound if there is a suitable filter or endAt.
    for (FieldIndex.Segment segment : fieldIndex.getDirectionalSegments()) {
        @Nullable Value segmentValue = null;
        boolean segmentInclusive = true;
        // Process all filters to find a value for the current field segment
        for (FieldFilter fieldFilter : getFieldFiltersForPath(segment.getFieldPath())) {
            Value filterValue = null;
            boolean filterInclusive = true;
            switch(fieldFilter.getOperator()) {
                case GREATER_THAN_OR_EQUAL:
                case GREATER_THAN:
                    filterValue = Values.getUpperBound(fieldFilter.getValue().getValueTypeCase());
                    filterInclusive = false;
                    break;
                case EQUAL:
                case IN:
                case LESS_THAN_OR_EQUAL:
                    filterValue = fieldFilter.getValue();
                    break;
                case LESS_THAN:
                    filterValue = fieldFilter.getValue();
                    filterInclusive = false;
                    break;
                case NOT_EQUAL:
                case NOT_IN:
                    filterValue = Values.MAX_VALUE;
                    break;
                default:
            }
            if (min(segmentValue, filterValue) == filterValue) {
                segmentValue = filterValue;
                segmentInclusive = filterInclusive;
            }
        }
        // if we can narrow the scope.
        if (endAt != null) {
            for (int i = 0; i < orderBys.size(); ++i) {
                OrderBy orderBy = this.orderBys.get(i);
                if (orderBy.getField().equals(segment.getFieldPath())) {
                    Value cursorValue = endAt.getPosition().get(i);
                    if (min(segmentValue, cursorValue) == cursorValue) {
                        segmentValue = cursorValue;
                        segmentInclusive = endAt.isInclusive();
                    }
                    break;
                }
            }
        }
        if (segmentValue == null) {
            // No upper bound exists
            return null;
        }
        values.add(segmentValue);
        inclusive &= segmentInclusive;
    }
    return new Bound(values, inclusive);
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) Value(com.google.firestore.v1.Value) ArrayList(java.util.ArrayList) Nullable(androidx.annotation.Nullable) Nullable(androidx.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)30 HashMap (java.util.HashMap)25 Value (com.google.datastore.v1.Value)20 Map (java.util.Map)20 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)17 List (java.util.List)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