Refine search
@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()); } }
@Override public B mergeFrom(P other) { excludeFromIndexes = other.excludeFromIndexes(); meaning = other.getMeaning(); set(other.get()); return self(); }
@SuppressWarnings({"unchecked", "deprecation"}) @Override public Blob getBlob(String name) { Value<?> value = getValue(name); if (value.getMeaning() == 18 && value instanceof StringValue) { return new Blob(ByteString.copyFromUtf8(getString(name))); } return ((Value<Blob>) value).get(); }
@SuppressWarnings("deprecation") @Override public final com.google.datastore.v1.Value toProto(P value) { com.google.datastore.v1.Value.Builder builder = com.google.datastore.v1.Value.newBuilder(); builder.setExcludeFromIndexes(value.excludeFromIndexes()); builder.setMeaning(value.getMeaning()); setValue(value, builder); return builder.build(); }
@Test public void testGet() throws Exception { for (Map.Entry<ValueType, Value<?>> entry : typeToValue.entrySet()) { ValueType valueType = entry.getKey(); Value<?> value = entry.getValue(); assertEquals(TYPES.get(valueType)[1], value.get()); } TestBuilder builder = new TestBuilder(); Set<String> value = Collections.singleton("bla"); assertEquals(value, builder.set(value).build().get()); }
@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 }
@SuppressWarnings("unchecked") public <T> T getPropertyValue(String fieldName, EmbeddedType embeddedType, TypeInformation targetTypeInformation) { if (!this.entity.contains(fieldName)) { return null; } try { return this.conversion.convertOnRead(this.entity.getValue(fieldName).get(), embeddedType, targetTypeInformation); } catch (ConversionException | DatastoreDataException ex) { throw new DatastoreDataException("Unable to read property " + fieldName, ex); } } }
private static Object getNonEntityObjectFromRow(Object x) { Object mappedResult; if (x instanceof Key) { mappedResult = x; } else { BaseEntity entity = (BaseEntity) x; Set<String> colNames = entity.getNames(); if (colNames.size() > 1) { throw new DatastoreDataException( "The query method returns non-entity types, but the query result has " + "more than one column. Use a Projection entity type instead."); } mappedResult = entity.getValue((String) colNames.toArray()[0]).get(); } return mappedResult; }
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); }
@Test public void testGetList() throws Exception { BaseEntity<Key> entity = builder.build(); List<? extends Value<?>> list = entity.getList("list1"); assertEquals(3, list.size()); assertEquals(NullValue.of(), list.get(0)); assertEquals("foo", list.get(1).get()); assertEquals(LAT_LNG, list.get(2).get()); list = entity.getList("list2"); assertEquals(2, list.size()); assertEquals(Long.valueOf(10), list.get(0).get()); assertEquals(Double.valueOf(2), list.get(1).get()); list = entity.getList("list3"); assertEquals(1, list.size()); assertEquals(Boolean.TRUE, list.get(0).get()); entity = builder.set("list1", ListValue.of(list)).build(); assertEquals(list, entity.getList("list1")); List<Value<?>> stringList = entity.getList("stringList"); assertEquals( ImmutableList.of(StringValue.of("s1"), StringValue.of("s2"), StringValue.of("s3")), stringList); List<Value<Double>> doubleList = entity.getList("doubleList"); assertEquals( ImmutableList.of(DoubleValue.of(12.3), DoubleValue.of(4.56), DoubleValue.of(.789)), doubleList); List<EntityValue> entityList = entity.getList("entityList"); assertEquals( ImmutableList.of(EntityValue.of(ENTITY), EntityValue.of(PARTIAL_ENTITY)), entityList); }
/** @return a value that has the specified index flag (possibly the original itself) */ @SuppressWarnings("unchecked") public static <D> Value<D> index(final Value<D> original, final boolean index) { if (original.excludeFromIndexes() == !index) { return original; } else { return (Value<D>)original.toBuilder().setExcludeFromIndexes(!index).build(); } }
@Test public void testExcludeFromIndexes() throws Exception { for (Map.Entry<ValueType, Value<?>> entry : typeToValue.entrySet()) { assertFalse(entry.getValue().excludeFromIndexes()); } TestBuilder builder = new TestBuilder(); assertFalse(builder.build().excludeFromIndexes()); assertTrue(builder.setExcludeFromIndexes(true).build().excludeFromIndexes()); assertFalse(builder.setExcludeFromIndexes(false).build().excludeFromIndexes()); }
@Override @SuppressWarnings("unchecked") public void write(Object source, BaseEntity.Builder sink) { DatastorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(source.getClass()); PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(source); persistentEntity.doWithColumnBackedProperties( (DatastorePersistentProperty persistentProperty) -> { try { Object val = accessor.getProperty(persistentProperty); Value convertedVal = this.conversions.convertOnWrite(val, persistentProperty); if (persistentProperty.isUnindexed()) { ValueBuilder valueBuilder = convertedVal.toBuilder(); valueBuilder.setExcludeFromIndexes(true); convertedVal = valueBuilder.build(); } sink.set(persistentProperty.getFieldName(), convertedVal); } catch (DatastoreDataException ex) { throw new DatastoreDataException( "Unable to write " + persistentEntity.kindName() + "." + persistentProperty.getFieldName(), ex); } }); } }
@SuppressWarnings("deprecation") @Test public void testMeaning() throws Exception { TestBuilder builder = new TestBuilder(); assertEquals(10, builder.setMeaning(10).build().getMeaning()); }
@Override protected LatLng toPojo(final Value<LatLng> value) { return value.get(); }
@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 }
@SuppressWarnings("unchecked") public <T> T getPropertyValue(String fieldName, EmbeddedType embeddedType, TypeInformation targetTypeInformation) { if (!this.entity.contains(fieldName)) { return null; } try { return this.conversion.convertOnRead(this.entity.getValue(fieldName).get(), embeddedType, targetTypeInformation); } catch (ConversionException | DatastoreDataException ex) { throw new DatastoreDataException("Unable to read property " + fieldName, ex); } } }
private static Object getNonEntityObjectFromRow(Object x) { Object mappedResult; if (x instanceof Key) { mappedResult = x; } else { BaseEntity entity = (BaseEntity) x; Set<String> colNames = entity.getNames(); if (colNames.size() > 1) { throw new DatastoreDataException( "The query method returns non-entity types, but the query result has " + "more than one column. Use a Projection entity type instead."); } mappedResult = entity.getValue((String) colNames.toArray()[0]).get(); } return mappedResult; }
@SuppressWarnings({"unchecked", "deprecation"}) @Override public Timestamp getTimestamp(String name) { Value<?> value = getValue(name); if (value.getMeaning() == 18 && value instanceof LongValue) { return Timestamp.ofTimeMicroseconds(getLong(name)); } return ((Value<Timestamp>) value).get(); }