@Test public void testNames() throws Exception { Set<String> names = ImmutableSet.<String>builder() .add("string", "stringValue", "boolean", "double", "long", "list1", "list2", "list3") .add("entity", "partialEntity", "null", "timestamp", "blob", "key", "blobList") .add( "booleanList", "timestampList", "doubleList", "keyList", "entityList", "stringList") .add("longList", "latLng", "latLngList") .build(); BaseEntity<Key> entity = builder.build(); assertEquals(names, entity.getNames()); }
/** * The problem is ProjectionEntity; there's no way to create an EntityValue with a ProjectionEntity * so we can't use the standard translation system for {@code Value<Entity>}. Instead of making the * translation API really complicated, just convert it to a FullEntity. */ private EntityValue makeLoadEntityValue(final BaseEntity<?> ent) { if (ent instanceof FullEntity<?>) { return EntityValue.of((FullEntity<?>)ent); } else { // Sadly there's no more graceful way of doing this final Builder<?> builder = FullEntity.newBuilder(ent.getKey()); for (final String name : ent.getNames()) { final Value<?> value = ent.getValue(name); builder.set(name, value); } return EntityValue.of(builder.build()); } }
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 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; }
@Override public <T, R> Map<T, R> readAsMap(Class<T> keyType, TypeInformation<R> componentType, BaseEntity entity) { if (entity == null) { return null; } Map<T, R> result = new HashMap<>(); EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider( entity, this.conversions); Set<String> fieldNames = entity.getNames(); for (String field : fieldNames) { result.put(this.conversions.convertOnRead(field, null, keyType), propertyValueProvider.getPropertyValue(field, EmbeddedType.of(componentType), componentType)); } return result; }
@Override public <T, R> Map<T, R> readAsMap(Class<T> keyType, TypeInformation<R> componentType, BaseEntity entity) { if (entity == null) { return null; } Map<T, R> result = new HashMap<>(); EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider( entity, this.conversions); Set<String> fieldNames = entity.getNames(); for (String field : fieldNames) { result.put(this.conversions.convertOnRead(field, null, keyType), propertyValueProvider.getPropertyValue(field, EmbeddedType.of(componentType), componentType)); } return result; }