Search in sources :

Example 36 with Value

use of com.spotify.ffwd.v1.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 37 with Value

use of com.spotify.ffwd.v1.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 38 with Value

use of com.spotify.ffwd.v1.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 39 with Value

use of com.spotify.ffwd.v1.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)

Example 40 with Value

use of com.spotify.ffwd.v1.Value in project googleads-java-lib by googleads.

the class Pql method getApiValue.

/**
 * Gets the underlying value of the {@code Value} object that's comparable to what would be
 * returned in any other API object (i.e. DateTimeValue will return an API DateTime, not a Joda
 * DateTime).
 *
 * @param value the value to convert
 * @return the native value of {@code Value} or {@code null} if the underlying value is null
 * @throws IllegalArgumentException if value cannot be converted
 */
public static Object getApiValue(Value value) {
    if (value instanceof BooleanValue) {
        return ((BooleanValue) value).isValue();
    } else if (value instanceof NumberValue) {
        if (((NumberValue) value).getValue() == null) {
            return null;
        } else {
            try {
                return NumberFormat.getInstance().parse(((NumberValue) value).getValue());
            } catch (ParseException e) {
                throw new IllegalStateException("Received invalid number format from API: " + ((NumberValue) value).getValue());
            }
        }
    } else if (value instanceof TextValue) {
        return ((TextValue) value).getValue();
    } else if (value instanceof DateTimeValue) {
        return ((DateTimeValue) value).getValue();
    } else if (value instanceof DateValue) {
        return ((DateValue) value).getValue();
    } else if (value instanceof TargetingValue) {
        return ((TargetingValue) value).getValue();
    } else if (value instanceof SetValue) {
        List<Value> setValues = ((SetValue) value).getValues();
        Set<Object> apiValue = new LinkedHashSet<Object>();
        if (setValues != null) {
            for (Value setValue : setValues) {
                validateSetValueEntryForSet(getApiValue(setValue), apiValue);
                apiValue.add(getApiValue(setValue));
            }
        }
        return apiValue;
    } else {
        throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DateTimeValue(com.google.api.ads.admanager.jaxws.v202105.DateTimeValue) NumberValue(com.google.api.ads.admanager.jaxws.v202105.NumberValue) TextValue(com.google.api.ads.admanager.jaxws.v202105.TextValue) DateValue(com.google.api.ads.admanager.jaxws.v202105.DateValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202105.BooleanValue) NumberValue(com.google.api.ads.admanager.jaxws.v202105.NumberValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202105.BooleanValue) Value(com.google.api.ads.admanager.jaxws.v202105.Value) SetValue(com.google.api.ads.admanager.jaxws.v202105.SetValue) DateTimeValue(com.google.api.ads.admanager.jaxws.v202105.DateTimeValue) TargetingValue(com.google.api.ads.admanager.jaxws.v202105.TargetingValue) DateValue(com.google.api.ads.admanager.jaxws.v202105.DateValue) TextValue(com.google.api.ads.admanager.jaxws.v202105.TextValue) ParseException(java.text.ParseException) TargetingValue(com.google.api.ads.admanager.jaxws.v202105.TargetingValue) SetValue(com.google.api.ads.admanager.jaxws.v202105.SetValue)

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)24 Value (com.google.datastore.v1.Value)20 Map (java.util.Map)19 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