private void addValueHelper(Value<?> value) { // see datastore.proto definition for list_value Preconditions.checkArgument(value.getType() != ValueType.LIST, "Cannot contain another list"); listBuilder.add(value); }
@SuppressWarnings("unchecked") com.google.datastore.v1.Value toPb() { return getType().getMarshaller().toProto(this); }
@Test public void testType() throws Exception { for (Map.Entry<ValueType, Value<?>> entry : typeToValue.entrySet()) { assertEquals(entry.getKey(), entry.getValue().getType()); } }
@Override final public P load(final Value<D> node, final LoadContext ctx, final Path path) throws SkipException { if (node == null || node.getType() == ValueType.NULL) return null; else return loadSafe(node, ctx, path); }
@Test public void testGetArrayNoDeferredResults() { DATASTORE.put(ENTITY3); Iterator<Entity> result = DATASTORE.fetch(KEY1, Key.newBuilder(KEY1).setName("bla").build(), KEY2, KEY3).iterator(); assertEquals(ENTITY1, result.next()); assertNull(result.next()); assertEquals(ENTITY2, result.next()); Entity entity3 = result.next(); assertEquals(ENTITY3, entity3); assertTrue(entity3.isNull("null")); assertFalse(entity3.getBoolean("bool")); assertEquals(LIST_VALUE2.get(), entity3.getList("list")); FullEntity<IncompleteKey> partial1 = entity3.getEntity("partial1"); FullEntity<IncompleteKey> partial2 = entity3.getEntity("partial2"); assertEquals(PARTIAL_ENTITY2, partial1); assertEquals(ENTITY2, partial2); assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").getType()); assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng")); assertEquals(EMPTY_LIST_VALUE, entity3.getValue("emptyList")); assertEquals(8, entity3.getNames().size()); assertFalse(entity3.contains("bla")); try { entity3.getString("str"); fail("Expecting a failure"); } catch (DatastoreException expected) { // expected - no such property } assertFalse(result.hasNext()); }
@SuppressWarnings({"unchecked", "deprecation"}) @Test public void testToBuilder() throws Exception { Set<String> content = Collections.singleton("bla"); @SuppressWarnings("rawtypes") ValueBuilder builder = new TestBuilder(); builder.setMeaning(1).set(content).setExcludeFromIndexes(true); Value<?> value = builder.build(); builder = value.toBuilder(); assertEquals(1, value.getMeaning()); assertTrue(value.excludeFromIndexes()); assertEquals(ValueType.LIST, value.getType()); assertEquals(content, value.get()); assertEquals(value, builder.build()); } }
@Test public void testGetArrayNoDeferredResults() { datastore.put(ENTITY3); Iterator<Entity> result = datastore.fetch(KEY1, Key.newBuilder(KEY1).setName("bla").build(), KEY2, KEY3).iterator(); assertEquals(ENTITY1, result.next()); assertNull(result.next()); assertEquals(ENTITY2, result.next()); Entity entity3 = result.next(); assertEquals(ENTITY3, entity3); assertTrue(entity3.isNull("null")); assertFalse(entity3.getBoolean("bool")); assertEquals(LIST_VALUE2.get(), entity3.getList("list")); FullEntity<IncompleteKey> partial1 = entity3.getEntity("partial1"); FullEntity<IncompleteKey> partial2 = entity3.getEntity("partial2"); assertEquals(PARTIAL_ENTITY2, partial1); assertEquals(ENTITY2, partial2); assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").getType()); assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng")); assertEquals(EMPTY_LIST_VALUE, entity3.getValue("emptyList")); assertEquals(8, entity3.getNames().size()); assertFalse(entity3.contains("bla")); try { entity3.getString("str"); fail("Expecting a failure"); } catch (DatastoreException expected) { // expected - no such property } assertFalse(result.hasNext()); }
@Override protected Number loadValue(final Value<Double> value, final LoadContext ctx, final Path path) throws SkipException { if (value.getType() == ValueType.STRING) { try { return coerceNumber(Double.valueOf(((Value<String>)(Value)value).get()), clazz); } catch (NumberFormatException ex) {} } else { return coerceNumber(value.get(), clazz); } path.throwIllegalState("Don't know how to translate " + value + " to a number"); return null; // never gets here }
@Override protected Number loadValue(final Value<Long> value, final LoadContext ctx, final Path path) throws SkipException { // In theory it's a number, but maybe there was a string instead? We'll have to remove the typecheck // in the base ValueTranslator class for this to work. if (value.getType() == ValueType.STRING) { try { return coerceNumber(Long.valueOf(((Value<String>)(Value)value).get()), clazz); } catch (NumberFormatException ex) {} } else { return coerceNumber(value.get(), clazz); } path.throwIllegalState("Don't know how to translate " + value + " to a number"); return null; // never gets here }
/** * Get the contents of the @Parent field as a datastore key. * @return null if there was no @Parent field, or the field is null. */ private com.google.cloud.datastore.Key getParentRaw(P pojo) { if (parentMeta == null) return null; final Value<Object> value = parentMeta.getValue(pojo, new SaveContext(), Path.root()); return (value == null || value.getType() == ValueType.NULL) ? null : (com.google.cloud.datastore.Key)value.get(); }
@Override final protected P loadSafe(final Value<D> value, final LoadContext ctx, final Path path) throws SkipException { if (!isTypeExpected(value.getType())) { // Normally we would just throw an error here but there are some edge cases caused by projection queries. // For example, timestamps come back as LongValue and blobs come back as StringValue. We'll special-case them. // The downside is that a user who changes a field from 'long' to 'Date' will not trigger an error. // The exact logic here comes from com.google.cloud.datastore.ProjectionEntity if (value.getType() == ValueType.LONG && isTypeExpected(ValueType.TIMESTAMP)) { @SuppressWarnings("unchecked") final Value<D> timestampValue = (Value<D>)TimestampValue.of(Timestamp.ofTimeMicroseconds((Long)value.get())); return loadValue(timestampValue, ctx, path); } else if (value.getType() == ValueType.STRING && isTypeExpected(ValueType.BLOB)) { @SuppressWarnings("unchecked") final Value<D> blobValue = (Value<D>)BlobValue.of(Blob.copyFrom(((String)value.get()).getBytes(StandardCharsets.UTF_8))); return loadValue(blobValue, ctx, path); } else { path.throwIllegalState("Expected value of type " + Arrays.toString(expectedValueTypes) + ", got " + value.getType() + ": " + value); } } return loadValue(value, ctx, path); }
private void addValueHelper(Value<?> value) { // see datastore.proto definition for list_value Preconditions.checkArgument(value.getType() != ValueType.LIST, "Cannot contain another list"); listBuilder.add(value); }
@SuppressWarnings("unchecked") com.google.datastore.v1.Value toPb() { return getType().getMarshaller().toProto(this); }
@Override public Value<?> index(Value<?> input) { if (input.getType() == ValueType.NULL) { return NullValue.of(); } try { String str = ((StringValue) input).get(); return StringValue.of(str.toUpperCase(Locale.ENGLISH)); } catch (Exception exp) { throw new IndexingException(exp); } }
@Override public Value<?> index(Value<?> input) { if (input.getType() == ValueType.NULL) { return NullValue.of(); } try { String str = ((StringValue) input).get(); return StringValue.of(str.toLowerCase(Locale.ENGLISH)); } catch (Exception exp) { throw new IndexingException(exp); } }
@Override public Value<?> index(Value<?> input) { if (input.getType() == ValueType.NULL) { return NullValue.of(); } try { ListValue.Builder builder = ListValue.newBuilder(); List<? extends Value<?>> list = ((ListValue) input).get(); for (Value<?> item : list) { builder.addValue(ITEM_INDEXER.index(item)); } return builder.build(); } catch (Exception exp) { throw new IndexingException(exp); } }
@Override public Value<?> index(Value<?> input) { if (input.getType() == ValueType.NULL) { return NullValue.of(); } try { List<? extends Value<?>> list = ((ListValue) input).get(); ListValue.Builder builder = ListValue.newBuilder(); for (Value<?> item : list) { builder.addValue(ITEM_INDEXER.index(item)); } return builder.build(); } catch (Exception exp) { throw new IndexingException(exp); } }
@Override public Object toModel(Value<?> input) { if (input.getType() == ValueType.NULL) { return null; } try { FullEntity<?> entity = ((EntityValue) input).get(); ConstructorMetadata constructorMetadata = metadata.getConstructorMetadata(); Object embeddedObject = constructorMetadata.getConstructorMethodHandle().invoke(); for (PropertyMetadata propertyMetadata : metadata.getPropertyMetadataCollection()) { String mappedName = propertyMetadata.getMappedName(); if (entity.contains(mappedName)) { Value<?> propertyValue = entity.getValue(mappedName); Object fieldValue = propertyMetadata.getMapper().toModel(propertyValue); propertyMetadata.getWriteMethod().invoke(embeddedObject, fieldValue); } } if (constructorMetadata.isBuilderConstructionStrategy()) { embeddedObject = metadata.getConstructorMetadata().getBuildMethodHandle() .invoke(embeddedObject); } return embeddedObject; } catch (Throwable exp) { throw new MappingException(exp); } }