Java Code Examples for com.google.cloud.datastore.StringValue

The following examples show how to use com.google.cloud.datastore.StringValue. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void writeTestSubtypes() {
	DiscrimEntityD entityD = new DiscrimEntityD();
	entityD.stringField = "item D";
	entityD.intField = 10;
	entityD.enumField = TestDatastoreItem.Color.BLACK;

	Entity.Builder builder = getEntityBuilder();
	ENTITY_CONVERTER.write(entityD, builder);

	Entity entity = builder.build();


	assertThat(entity.getString("stringField")).as("validate string field").isEqualTo("item D");
	assertThat(entity.getLong("intField")).as("validate int field").isEqualTo(10L);
	assertThat(entity.getString("enumField")).as("validate enum field").isEqualTo("BLACK");
	assertThat(entity.getList("discrimination_column")).as("validate discrimination field")
			.containsExactly(StringValue.of("D"), StringValue.of("B"), StringValue.of("X"));
}
 
Example 2
Source Project: styx   Source File: DatastoreStorage.java    License: Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static Entity workflowToEntity(Workflow workflow, WorkflowState state, Optional<Entity> existing, Key key)
    throws JsonProcessingException {
  var json = OBJECT_MAPPER.writeValueAsString(workflow);

  var builder = asBuilderOrNew(existing, key)
      .set(PROPERTY_COMPONENT, workflow.componentId())
      .set(PROPERTY_WORKFLOW_JSON, StringValue.newBuilder(json).setExcludeFromIndexes(true).build());

  state.enabled()
      .ifPresent(x -> builder.set(PROPERTY_WORKFLOW_ENABLED, x));
  state.nextNaturalTrigger()
      .ifPresent(x -> builder.set(PROPERTY_NEXT_NATURAL_TRIGGER, instantToTimestamp(x)));
  state.nextNaturalOffsetTrigger()
      .ifPresent(x -> builder.set(PROPERTY_NEXT_NATURAL_OFFSET_TRIGGER, instantToTimestamp(x)));

  return builder.build();
}
 
Example 3
Source Project: java-docs-samples   Source File: ConceptsTest.java    License: Apache License 2.0 6 votes vote down vote up
private void setUpQueryTests() {
  Key taskKey = datastore.newKeyFactory()
      .setKind("Task")
      .addAncestors(PathElement.of("TaskList", "default"))
      .newKey("someTask");
  datastore.put(Entity.newBuilder(taskKey)
      .set("category", "Personal")
      .set("done", false)
      .set("completed", false)
      .set("priority", 4)
      .set("created", includedDate)
      .set("percent_complete", 10.0)
      .set("description",
          StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build())
      .set("tag", "fun", "l", "programming")
      .build());
}
 
Example 4
Source Project: data-transfer-project   Source File: GoogleJobStore.java    License: Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> getProperties(Entity entity)
    throws IOException, ClassNotFoundException {
  if (entity == null) {
    return null;
  }
  ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
  for (String property : entity.getNames()) {
    // builder.put(property, entity.getValue(property));
    if (entity.getValue(property) instanceof StringValue) {
      builder.put(property, (String) entity.getString(property));
    } else if (entity.getValue(property) instanceof LongValue) {
      // This conversion is safe because of integer to long conversion above
      builder.put(property, new Long(entity.getLong(property)).intValue());
    } else if (entity.getValue(property) instanceof DoubleValue) {
      builder.put(property, (Double) entity.getDouble(property));
    } else if (entity.getValue(property) instanceof BooleanValue) {
      builder.put(property, (Boolean) entity.getBoolean(property));
    } else if (entity.getValue(property) instanceof TimestampValue) {
      builder.put(property, (Timestamp) entity.getTimestamp(property));
    } else {
      Blob blob = entity.getBlob(property);
      Object obj = null;
      try (ObjectInputStream in = new ObjectInputStream(blob.asInputStream())) {
        obj = in.readObject();
      }
      builder.put(property, obj); // BlobValue
    }
  }

  return builder.build();
}
 
Example 5
@Override
@SuppressWarnings("unchecked")
public void write(Object source, BaseEntity.Builder sink) {
	DatastorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(source.getClass());

	String discriminationFieldName = persistentEntity.getDiscriminationFieldName();
	List<String> discriminationValues = persistentEntity.getCompatibleDiscriminationValues();
	if (!discriminationValues.isEmpty() || discriminationFieldName != null) {
		sink.set(discriminationFieldName,
				discriminationValues.stream().map(StringValue::of).collect(Collectors.toList()));
	}
	PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(source);
	persistentEntity.doWithColumnBackedProperties(
			(DatastorePersistentProperty persistentProperty) -> {
				// Datastore doesn't store its Key as a regular field.
				if (persistentProperty.isIdProperty()) {
					return;
				}
				try {
					Object val = accessor.getProperty(persistentProperty);
					Value convertedVal = this.conversions.convertOnWrite(val, persistentProperty);

					if (persistentProperty.isUnindexed()) {
						convertedVal = setExcludeFromIndexes(convertedVal);
					}
					sink.set(persistentProperty.getFieldName(), convertedVal);
				}
				catch (DatastoreDataException ex) {
					throw new DatastoreDataException(
							"Unable to write "
									+ persistentEntity.kindName() + "." + persistentProperty.getFieldName(),
							ex);
				}
			});
}
 
Example 6
Source Project: catatumbo   Source File: UpperCaseStringIndexer.java    License: Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 7
Source Project: catatumbo   Source File: LowerCaseStringIndexer.java    License: Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 8
Source Project: catatumbo   Source File: LocalDateMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return StringValue.newBuilder(input.toString());
}
 
Example 9
Source Project: catatumbo   Source File: LocalDateMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  try {
    return LocalDate.parse(((StringValue) input).get());
  } catch (ClassCastException exp) {
    String pattern = "Mapping of type %s to %s is not supported";
    throw new MappingException(
        String.format(pattern, input.getClass().getName(), LocalDate.class.getName()), exp);
  }
}
 
Example 10
Source Project: catatumbo   Source File: StringMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return StringValue.newBuilder((String) input);
}
 
Example 11
Source Project: catatumbo   Source File: StringMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  return ((StringValue) input).get();
}
 
Example 12
Source Project: catatumbo   Source File: CharMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return StringValue.newBuilder(String.valueOf((char) input));
}
 
Example 13
Source Project: catatumbo   Source File: CharMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  String str = ((StringValue) input).get();
  if (str.length() != 1) {
    throw new MappingException(String.format("Unable to convert %s to char", str));
  }
  return str.charAt(0);
}
 
Example 14
Source Project: catatumbo   Source File: LocalTimeMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return StringValue.newBuilder(((LocalTime) input).format(FORMATTER));
}
 
Example 15
Source Project: catatumbo   Source File: CharArrayMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return StringValue.newBuilder(new String((char[]) input));
}
 
Example 16
Source Project: catatumbo   Source File: CharArrayMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  return ((StringValue) input).get().toCharArray();
}
 
Example 17
Source Project: catatumbo   Source File: EnumMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return StringValue.newBuilder(input.toString());
}
 
Example 18
Source Project: catatumbo   Source File: EnumMapper.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  String value = ((StringValue) input).get();
  return Enum.valueOf(enumClass, value);
}
 
Example 19
Source Project: catatumbo   Source File: CatchAllMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  ValueBuilder<?, ?, ?> builder;
  if (input == null) {
    builder = NullValue.newBuilder();
  } else if (input instanceof Long) {
    builder = LongValue.newBuilder((long) input);
  } else if (input instanceof Double) {
    builder = DoubleValue.newBuilder((double) input);
  } else if (input instanceof Boolean) {
    builder = BooleanValue.newBuilder((boolean) input);
  } else if (input instanceof String) {
    builder = StringValue.newBuilder((String) input);
  } else if (input instanceof DatastoreKey) {
    builder = KeyValue.newBuilder(((DatastoreKey) input).nativeKey());
  } else if (input instanceof GeoLocation) {
    GeoLocation geoLocation = (GeoLocation) input;
    builder = LatLngValue
        .newBuilder(LatLng.of(geoLocation.getLatitude(), geoLocation.getLongitude()));
  } else if (input instanceof Map) {
    @SuppressWarnings("unchecked")
    Map<String, ?> map = (Map<String, ?>) input;
    FullEntity.Builder<IncompleteKey> entityBuilder = FullEntity.newBuilder();
    for (Map.Entry<String, ?> entry : map.entrySet()) {
      String key = entry.getKey();
      entityBuilder.set(key, toDatastore(entry.getValue()).build());
    }
    builder = EntityValue.newBuilder(entityBuilder.build());
  } else {
    throw new MappingException(String.format("Unsupported type: %s", input.getClass().getName()));
  }
  return builder;
}
 
Example 20
Source Project: catatumbo   Source File: CatchAllMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  Object javaValue;
  if (input instanceof NullValue) {
    javaValue = null;
  } else if (input instanceof StringValue) {
    javaValue = input.get();
  } else if (input instanceof LongValue) {
    javaValue = input.get();
  } else if (input instanceof DoubleValue) {
    javaValue = input.get();
  } else if (input instanceof BooleanValue) {
    javaValue = input.get();
  } else if (input instanceof KeyValue) {
    javaValue = new DefaultDatastoreKey(((KeyValue) input).get());
  } else if (input instanceof LatLngValue) {
    LatLng latLong = ((LatLngValue) input).get();
    javaValue = new GeoLocation(latLong.getLatitude(), latLong.getLongitude());
  } else if (input instanceof EntityValue) {
    EntityValue entityValue = (EntityValue) input;
    FullEntity<?> entity = entityValue.get();
    Map<String, Object> map = new HashMap<>();
    for (String property : entity.getNames()) {
      map.put(property, toModel(entity.getValue(property)));
    }
    javaValue = map;
  } else {
    throw new MappingException(String.format("Unsupported type %s", input.getClass().getName()));
  }
  return javaValue;
}
 
Example 21
Source Project: catatumbo   Source File: LocalDateTimeMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return StringValue.newBuilder(((LocalDateTime) input).format(FORMATTER));
}
 
Example 22
Source Project: catatumbo   Source File: LowerCaseStringIndexerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIndex_2() {
  StringValue input = StringValue.of("Hello World");
  StringValue output = (StringValue) indexer.index(input);
  assertEquals(input.get().toLowerCase(Locale.ENGLISH), output.get());
  assertNotEquals(input.get(), output.get());
}
 
Example 23
Source Project: catatumbo   Source File: UpperCaseStringIndexerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIndex_2() {
  StringValue input = StringValue.of("Hello World!");
  StringValue output = (StringValue) indexer.index(input);
  assertEquals(input.get().toUpperCase(Locale.ENGLISH), output.get());
  assertNotEquals(input.get(), output.get());
}
 
Example 24
Source Project: catatumbo   Source File: UpperCaseStringListIndexerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIndex_2() {
  Value<?>[] inputArray = { StringValue.of("ONE"), StringValue.of("Two"),
      StringValue.of("thRee") };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  ListValue output = (ListValue) indexer.index(input);
  List<StringValue> inputList = (List<StringValue>) input.get();
  List<StringValue> outputList = (List<StringValue>) output.get();
  assertEquals(inputList.get(0).get().toUpperCase(Locale.ENGLISH), outputList.get(0).get());
  assertEquals(inputList.get(1).get().toUpperCase(Locale.ENGLISH), outputList.get(1).get());
  assertEquals(inputList.get(2).get().toUpperCase(Locale.ENGLISH), outputList.get(2).get());
}
 
Example 25
Source Project: catatumbo   Source File: UpperCaseStringListIndexerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIndex_3() {
  Value<?>[] inputArray = { StringValue.of("Hello"), NullValue.of() };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  ListValue output = (ListValue) indexer.index(input);
  List<? extends Value> inputList = input.get();
  List<? extends Value> outputList = output.get();
  assertEquals(((StringValue) inputList.get(0)).get().toUpperCase(Locale.ENGLISH),
      outputList.get(0).get());
  assertEquals(inputList.get(1).get(), outputList.get(1).get());
}
 
Example 26
Source Project: catatumbo   Source File: UpperCaseStringListIndexerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = IndexingException.class)
public void testIndex_4() {
  StringValue input = StringValue.of("Hello World");
  try {
    Value<?> output = indexer.index(input);
  } catch (Exception exp) {
    LOGGER.log(Level.INFO, exp.toString());
    throw exp;
  }
}
 
Example 27
Source Project: catatumbo   Source File: UpperCaseStringListIndexerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = IndexingException.class)
public void testIndex_5() {
  Value<?>[] inputArray = { StringValue.of("Hello"), NullValue.of(), LongValue.of(5L) };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  try {
    ListValue output = (ListValue) indexer.index(input);
  } catch (Exception exp) {
    LOGGER.log(Level.INFO, exp.toString());
    throw exp;
  }
}
 
Example 28
Source Project: catatumbo   Source File: LowerCaseStringListIndexerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIndex_2() {
  Value<?>[] inputArray = { StringValue.of("ONE"), StringValue.of("Two"),
      StringValue.of("thRee") };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  ListValue output = (ListValue) indexer.index(input);
  List<StringValue> inputList = (List<StringValue>) input.get();
  List<StringValue> outputList = (List<StringValue>) output.get();
  assertEquals(inputList.get(0).get().toLowerCase(Locale.ENGLISH), outputList.get(0).get());
  assertEquals(inputList.get(1).get().toLowerCase(Locale.ENGLISH), outputList.get(1).get());
  assertEquals(inputList.get(2).get().toLowerCase(Locale.ENGLISH), outputList.get(2).get());
}
 
Example 29
Source Project: catatumbo   Source File: LowerCaseStringListIndexerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIndex_3() {
  Value<?>[] inputArray = { StringValue.of("Hello"), NullValue.of() };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  ListValue output = (ListValue) indexer.index(input);
  List<? extends Value> inputList = input.get();
  List<? extends Value> outputList = output.get();
  assertEquals(((StringValue) inputList.get(0)).get().toLowerCase(Locale.ENGLISH),
      outputList.get(0).get());
  assertEquals(inputList.get(1).get(), outputList.get(1).get());
}
 
Example 30
Source Project: catatumbo   Source File: LowerCaseStringListIndexerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = IndexingException.class)
public void testIndex_4() {
  StringValue input = StringValue.of("Hello World");
  try {
    Value<?> output = indexer.index(input);
  } catch (Exception exp) {
    LOGGER.log(Level.INFO, exp.toString());
    throw exp;
  }
}