Search in sources :

Example 1 with EntityValue

use of com.google.cloud.datastore.EntityValue in project spring-cloud-gcp by spring-cloud.

the class TwoStepsConversions method applyEntityValueBuilder.

private EntityValue applyEntityValueBuilder(Object val, String kindName, Consumer<Builder> consumer, boolean createKey) {
    FullEntity.Builder<IncompleteKey> builder;
    if (!createKey) {
        builder = FullEntity.newBuilder();
    } else {
        /* The following does 3 sequential null checks. We only want an ID value if the object isn't null,
				has an ID property, and the ID property isn't null.
			* */
        Optional idProp = Optional.ofNullable(val).map(v -> this.datastoreMappingContext.getPersistentEntity(v.getClass())).map(datastorePersistentEntity -> datastorePersistentEntity.getIdProperty()).map(id -> this.datastoreMappingContext.getPersistentEntity(val.getClass()).getPropertyAccessor(val).getProperty(id));
        IncompleteKey key = idProp.isPresent() ? this.objectToKeyFactory.getKeyFromId(idProp.get(), kindName) : this.objectToKeyFactory.getIncompleteKey(kindName);
        builder = FullEntity.newBuilder(key);
    }
    consumer.accept(builder);
    return EntityValue.of(builder.build());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) Arrays(java.util.Arrays) CustomConversions(org.springframework.data.convert.CustomConversions) BiFunction(java.util.function.BiFunction) Builder(com.google.cloud.datastore.FullEntity.Builder) TypeInformation(org.springframework.data.util.TypeInformation) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ValueUtil(org.springframework.cloud.gcp.data.datastore.core.util.ValueUtil) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) ValueUtil.boxIfNeeded(org.springframework.cloud.gcp.data.datastore.core.util.ValueUtil.boxIfNeeded) Converter(org.springframework.core.convert.converter.Converter) ClassUtils(org.springframework.util.ClassUtils) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EntityValue(com.google.cloud.datastore.EntityValue) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) ConversionException(org.springframework.core.convert.ConversionException) Collectors(java.util.stream.Collectors) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Consumer(java.util.function.Consumer) List(java.util.List) Value(com.google.cloud.datastore.Value) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty) EmbeddedType(org.springframework.cloud.gcp.data.datastore.core.mapping.EmbeddedType) Optional(java.util.Optional) Blob(com.google.cloud.datastore.Blob) ListValue(com.google.cloud.datastore.ListValue) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Assert(org.springframework.util.Assert) Optional(java.util.Optional) FullEntity(com.google.cloud.datastore.FullEntity) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 2 with EntityValue

use of com.google.cloud.datastore.EntityValue in project spring-cloud-gcp by spring-cloud.

the class DefaultDatastoreEntityConverterTests method testUnindexedField.

@Test
public void testUnindexedField() {
    UnindexedTestDatastoreItem item = new UnindexedTestDatastoreItem();
    item.setIndexedField(1L);
    item.setUnindexedField(2L);
    item.setUnindexedStringListField(Arrays.asList("a", "b"));
    item.setUnindexedMapField(new MapBuilder<String, String>().put("c", "C").put("d", "D").build());
    item.setEmbeddedItem(new UnindexedTestDatastoreItem(2, new UnindexedTestDatastoreItem(3, null)));
    item.setUnindexedItems(Collections.singletonList(new UnindexedTestDatastoreItem(4, new UnindexedTestDatastoreItem(5, null))));
    DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new DatastoreServiceObjectToKeyFactory(() -> this.datastore));
    Entity.Builder builder = getEntityBuilder();
    entityConverter.write(item, builder);
    Entity entity = builder.build();
    assertThat(entity.getLong("indexedField")).as("validate indexed field value").isEqualTo(1L);
    assertThat(entity.getLong("unindexedField")).as("validate unindexed field value").isEqualTo(2L);
    assertThat(entity.getValue("indexedField").excludeFromIndexes()).as("validate excludeFromIndexes on indexed field").isFalse();
    assertThat(entity.getValue("unindexedField").excludeFromIndexes()).as("validate excludeFromIndexes on unindexed field").isTrue();
    // the ListValue itself must NOT be unindexed or it will cause exception. the contents
    // individually must be. Same for map.
    assertThat(entity.getValue("unindexedStringListField").excludeFromIndexes()).isFalse();
    assertThat(((ListValue) entity.getValue("unindexedStringListField")).get().get(0).excludeFromIndexes()).isTrue();
    assertThat(((ListValue) entity.getValue("unindexedStringListField")).get().get(1).excludeFromIndexes()).isTrue();
    assertThat(entity.getValue("unindexedMapField").excludeFromIndexes()).isFalse();
    assertThat(((EntityValue) entity.getValue("unindexedMapField")).get().getValue("c").excludeFromIndexes()).isTrue();
    assertThat(((EntityValue) entity.getValue("unindexedMapField")).get().getValue("d").excludeFromIndexes()).isTrue();
    // Multi-level embedded entities - exclusion from indexes
    testMultiLevelEmbeddedEntityUnindexed(((EntityValue) entity.getValue("embeddedItem")).get());
    // Multi-level embedded entities in a list - exclusion from indexes
    testMultiLevelEmbeddedEntityUnindexed(((EntityValue) ((ListValue) entity.getValue("unindexedItems")).get().get(0)).get());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) EmbeddedEntity(org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) ListValue(com.google.cloud.datastore.ListValue) EntityValue(com.google.cloud.datastore.EntityValue) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) Test(org.junit.Test)

Aggregations

BaseEntity (com.google.cloud.datastore.BaseEntity)2 EntityValue (com.google.cloud.datastore.EntityValue)2 FullEntity (com.google.cloud.datastore.FullEntity)2 ListValue (com.google.cloud.datastore.ListValue)2 DatastoreMappingContext (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext)2 Blob (com.google.cloud.datastore.Blob)1 Entity (com.google.cloud.datastore.Entity)1 Builder (com.google.cloud.datastore.FullEntity.Builder)1 IncompleteKey (com.google.cloud.datastore.IncompleteKey)1 Value (com.google.cloud.datastore.Value)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BiFunction (java.util.function.BiFunction)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1