com.google.cloud.datastore.Value.getType()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(82)

本文整理了Java中com.google.cloud.datastore.Value.getType()方法的一些代码示例,展示了Value.getType()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Value.getType()方法的具体详情如下:
包路径:com.google.cloud.datastore.Value
类名称:Value
方法名:getType

Value.getType介绍

[英]Returns the type of this value.
[中]返回此值的类型。

代码示例

代码示例来源:origin: googleapis/google-cloud-java

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);
}

代码示例来源:origin: googleapis/google-cloud-java

@SuppressWarnings("unchecked")
com.google.datastore.v1.Value toPb() {
 return getType().getMarshaller().toProto(this);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testType() throws Exception {
 for (Map.Entry<ValueType, Value<?>> entry : typeToValue.entrySet()) {
  assertEquals(entry.getKey(), entry.getValue().getType());
 }
}

代码示例来源:origin: objectify/objectify

@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);
}

代码示例来源:origin: googleapis/google-cloud-java

@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());
}

代码示例来源:origin: googleapis/google-cloud-java

@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());
 }
}

代码示例来源:origin: googleapis/google-cloud-java

@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());
}

代码示例来源:origin: objectify/objectify

@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
}

代码示例来源:origin: objectify/objectify

@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
}

代码示例来源:origin: objectify/objectify

/**
 * 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();
}

代码示例来源:origin: objectify/objectify

@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);
}

代码示例来源:origin: com.google.cloud/google-cloud-datastore

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);
}

代码示例来源:origin: com.google.cloud/google-cloud-datastore

@SuppressWarnings("unchecked")
com.google.datastore.v1.Value toPb() {
 return getType().getMarshaller().toProto(this);
}

代码示例来源:origin: sai-pullabhotla/catatumbo

@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);
 }
}

代码示例来源:origin: sai-pullabhotla/catatumbo

@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);
 }
}

代码示例来源:origin: sai-pullabhotla/catatumbo

@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);
 }
}

代码示例来源:origin: sai-pullabhotla/catatumbo

@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);
 }
}

代码示例来源:origin: sai-pullabhotla/catatumbo

@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);
 }
}

相关文章

微信公众号

最新文章

更多