Java Code Examples for com.google.protobuf.Value#getKindCase()

The following examples show how to use com.google.protobuf.Value#getKindCase() . 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
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void itReadsMixedListValue() throws IOException {
  String json = "{\"value\":[null,1.5,\"test\",true,{\"key\":\"nested\"},[\"nested\"]]}";
  HasValue message = camelCase().readValue(json, HasValue.class);
  assertThat(message.hasValue()).isTrue();
  Value value = message.getValue();
  switch (value.getKindCase()) {
    case LIST_VALUE:
      ListValue list = value.getListValue();
      Value nested = Value.newBuilder().setStringValue("nested").build();
      Struct struct = Struct.newBuilder().putFields("key", nested).build();
      ListValue nestedList = ListValue.newBuilder().addValues(nested).build();
      assertThat(list.getValuesCount()).isEqualTo(6);
      assertThat(list.getValues(0)).isEqualTo(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
      assertThat(list.getValues(1)).isEqualTo(Value.newBuilder().setNumberValue(1.5).build());
      assertThat(list.getValues(2)).isEqualTo(Value.newBuilder().setStringValue("test").build());
      assertThat(list.getValues(3)).isEqualTo(Value.newBuilder().setBoolValue(true).build());
      assertThat(list.getValues(4)).isEqualTo(Value.newBuilder().setStructValue(struct).build());
      assertThat(list.getValues(5)).isEqualTo(Value.newBuilder().setListValue(nestedList).build());
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example 2
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void itReadsListValue() throws IOException {
  String json = "{\"value\":[\"test\"]}";
  HasValue valueWrapper = camelCase().readValue(json, HasValue.class);
  assertThat(valueWrapper.hasValue()).isTrue();

  Value value = valueWrapper.getValue();
  ListValue list = ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("test").build()).build();
  switch (value.getKindCase()) {
    case LIST_VALUE:
      assertThat(value.getListValue()).isEqualTo(list);
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example 3
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void itReadsStructValue() throws IOException {
  String json = "{\"value\":{\"key\":\"value\"}}";
  HasValue valueWrapper = camelCase().readValue(json, HasValue.class);
  assertThat(valueWrapper.hasValue()).isTrue();

  Value value = valueWrapper.getValue();
  switch (value.getKindCase()) {
    case STRUCT_VALUE:
      Entry<String, Value> entry = entry("key", Value.newBuilder().setStringValue("value").build());
      assertThat(value.getStructValue().getFieldsMap()).containsExactly(entry);
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example 4
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void itReadsMixedStruct() throws IOException {
  String json = "{\"value\":{\"null\":null,\"number\":1.5,\"string\":\"test\",\"boolean\":true,\"struct\":{\"key\":\"nested\"},\"list\":[\"nested\"]}}";
  HasValue message = camelCase().readValue(json, HasValue.class);
  assertThat(message.hasValue()).isTrue();
  Value value = message.getValue();
  switch (value.getKindCase()) {
    case STRUCT_VALUE:
      Map<String, Value> map = value.getStructValue().getFieldsMap();
      Value nested = Value.newBuilder().setStringValue("nested").build();
      Struct nestedStruct = Struct.newBuilder().putFields("key", nested).build();
      ListValue list = ListValue.newBuilder().addValues(nested).build();
      assertThat(map.size()).isEqualTo(6);
      assertThat(map.get("null")).isEqualTo(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
      assertThat(map.get("number")).isEqualTo(Value.newBuilder().setNumberValue(1.5).build());
      assertThat(map.get("string")).isEqualTo(Value.newBuilder().setStringValue("test").build());
      assertThat(map.get("boolean")).isEqualTo(Value.newBuilder().setBoolValue(true).build());
      assertThat(map.get("struct")).isEqualTo(Value.newBuilder().setStructValue(nestedStruct).build());
      assertThat(map.get("list")).isEqualTo(Value.newBuilder().setListValue(list).build());
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example 5
Source File: WellKnownTypeMarshaller.java    From curiostack with MIT License 6 votes vote down vote up
@Override
public void doWrite(Value message, JsonGenerator gen) throws IOException {
  switch (message.getKindCase()) {
    case NULL_VALUE:
      SerializeSupport.printNull(0, gen);
      break;
    case NUMBER_VALUE:
      SerializeSupport.printDouble(message.getNumberValue(), gen);
      break;
    case STRING_VALUE:
      SerializeSupport.printString(message.getStringValue(), gen);
      break;
    case BOOL_VALUE:
      SerializeSupport.printBool(message.getBoolValue(), gen);
      break;
    case STRUCT_VALUE:
      StructMarshaller.INSTANCE.writeValue(message.getStructValue(), gen);
      break;
    case LIST_VALUE:
      ListValueMarshaller.INSTANCE.writeValue(message.getListValue(), gen);
      break;
    case KIND_NOT_SET:
      SerializeSupport.printNull(0, gen);
      break;
  }
}
 
Example 6
Source File: ListValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void itReadsNestedListValues() throws IOException {
  String json = "{\"listValue\":[[\"nested\"]]}";
  HasListValue message = camelCase().readValue(json, HasListValue.class);
  assertThat(message.hasListValue()).isTrue();
  assertThat(message.getListValue().getValuesList()).hasSize(1);
  Value value = message.getListValue().getValues(0);
  ListValue list = ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("nested")).build();
  switch (value.getKindCase()) {
    case LIST_VALUE:
      assertThat(value.getListValue()).isEqualToComparingFieldByField(list);
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example 7
Source File: ValueUtils.java    From cloud-spanner-r2dbc with Apache License 2.0 5 votes vote down vote up
private static LocalDate parseDate(Value proto) {
  String date = proto.getStringValue();
  if (proto.getKindCase() == KindCase.NULL_VALUE || date == null) {
    return null;
  }
  Matcher matcher = FORMAT_REGEXP.matcher(date);
  if (!matcher.matches()) {
    throw new IllegalArgumentException("Invalid date: " + date);
  } else {
    int year = Integer.parseInt(matcher.group(1));
    int month = Integer.parseInt(matcher.group(2));
    int dayOfMonth = Integer.parseInt(matcher.group(3));
    return LocalDate.of(year, month, dayOfMonth);
  }
}
 
Example 8
Source File: ValueUtils.java    From cloud-spanner-r2dbc with Apache License 2.0 5 votes vote down vote up
private static LocalDateTime parseTimestamp(Value proto) {
  if (proto.getKindCase() == KindCase.NULL_VALUE || proto.getStringValue() == null) {
    return null;
  }
  TemporalAccessor temporalAccessor = TIMESTAMP_FORMATTER.parse(proto.getStringValue());
  return LocalDateTime.ofInstant(Instant.from(temporalAccessor), ZoneOffset.UTC);
}
 
Example 9
Source File: VariantToVcf.java    From genomewarp with Apache License 2.0 5 votes vote down vote up
private static Object parseObject(String key, Value in, VCFHeaderLineType type) {
  // Case on type
  switch (in.getKindCase()) {
      case NULL_VALUE: throw new IllegalStateException(String.format("field %s contained "
          + "a null value", key));
      case NUMBER_VALUE: return getNumberValue(in.getNumberValue(), type);
      case STRING_VALUE: return in.getStringValue();
      case BOOL_VALUE: return (Boolean) in.getBoolValue();
      default: throw new IllegalStateException(String.format("field %s contained a %s type, which"
          + " is not supported by VariantToVcf", key, getKind(in.getKindCase())));
  }
}
 
Example 10
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void itReadsNullValue() throws IOException {
  String json = "{\"value\":null}";
  HasValue valueWrapper = camelCase().readValue(json, HasValue.class);
  assertThat(valueWrapper.hasValue()).isTrue();

  Value value = valueWrapper.getValue();
  switch (value.getKindCase()) {
    case NULL_VALUE:
      assertThat(value.getNullValue()).isEqualTo(NullValue.NULL_VALUE);
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example 11
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void itReadsIntegralValue() throws IOException {
  String json = "{\"value\":1}";
  HasValue valueWrapper = camelCase().readValue(json, HasValue.class);
  assertThat(valueWrapper.hasValue()).isTrue();

  Value value = valueWrapper.getValue();
  switch (value.getKindCase()) {
    case NUMBER_VALUE:
      assertThat(value.getNumberValue()).isEqualTo(1.0d);
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example 12
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void itReadsFloatingPointValue() throws IOException {
  String json = "{\"value\":1.5}";
  HasValue valueWrapper = camelCase().readValue(json, HasValue.class);
  assertThat(valueWrapper.hasValue()).isTrue();

  Value value = valueWrapper.getValue();
  switch (value.getKindCase()) {
    case NUMBER_VALUE:
      assertThat(value.getNumberValue()).isEqualTo(1.5d);
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example 13
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void itReadsStringValue() throws IOException {
  String json = "{\"value\":\"test\"}";
  HasValue valueWrapper = camelCase().readValue(json, HasValue.class);
  assertThat(valueWrapper.hasValue()).isTrue();

  Value value = valueWrapper.getValue();
  switch (value.getKindCase()) {
    case STRING_VALUE:
      assertThat(value.getStringValue()).isEqualTo("test");
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example 14
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void itReadsBooleanValue() throws IOException {
  String json = "{\"value\":true}";
  HasValue valueWrapper = camelCase().readValue(json, HasValue.class);
  assertThat(valueWrapper.hasValue()).isTrue();

  Value value = valueWrapper.getValue();
  switch (value.getKindCase()) {
    case BOOL_VALUE:
      assertThat(value.getBoolValue()).isTrue();
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example 15
Source File: PartialResultRowExtractor.java    From cloud-spanner-r2dbc with Apache License 2.0 4 votes vote down vote up
private void initializeIncompletePiece(Value lastVal) {
  this.incompletePieceKind = lastVal.getKindCase();
  this.incompletePiece =
          lastVal.getKindCase() == KindCase.STRING_VALUE ? lastVal.getStringValue() :
                  new ArrayList<>(lastVal.getListValue().getValuesList());
}
 
Example 16
Source File: ValueUtils.java    From cloud-spanner-r2dbc with Apache License 2.0 4 votes vote down vote up
private static Long parseLong(Value proto) {
  return proto.getKindCase() == KindCase.NULL_VALUE ? null
      : Long.parseLong(proto.getStringValue());
}
 
Example 17
Source File: ValueUtils.java    From cloud-spanner-r2dbc with Apache License 2.0 4 votes vote down vote up
private static ByteBuffer parseBytes(Value value) {
  if (value.getKindCase() == KindCase.NULL_VALUE || value.getStringValue() == null) {
    return null;
  }
  return ByteBuffer.wrap(value.getStringValueBytes().toByteArray());
}
 
Example 18
Source File: ExperimentDAORdbImpl.java    From modeldb with Apache License 2.0 4 votes vote down vote up
@Override
public List<Experiment> getExperiments(List<KeyValue> keyValues)
    throws InvalidProtocolBufferException {
  try (Session session = ModelDBHibernateUtil.getSessionFactory().openSession()) {
    StringBuilder stringQueryBuilder = new StringBuilder("From ExperimentEntity ee where ");
    Map<String, Object> paramMap = new HashMap<>();
    for (int index = 0; index < keyValues.size(); index++) {
      KeyValue keyValue = keyValues.get(index);
      Value value = keyValue.getValue();
      String key = keyValue.getKey();

      switch (value.getKindCase()) {
        case NUMBER_VALUE:
          paramMap.put(key, value.getNumberValue());
          break;
        case STRING_VALUE:
          paramMap.put(key, value.getStringValue());
          break;
        case BOOL_VALUE:
          paramMap.put(key, value.getBoolValue());
          break;
        default:
          Status invalidValueTypeError =
              Status.newBuilder()
                  .setCode(Code.UNIMPLEMENTED_VALUE)
                  .setMessage(
                      "Unknown 'Value' type recognized, valid 'Value' type are NUMBER_VALUE, STRING_VALUE, BOOL_VALUE")
                  .build();
          throw StatusProto.toStatusRuntimeException(invalidValueTypeError);
      }
      stringQueryBuilder.append(" ee." + key + " = :" + key);
      if (index < keyValues.size() - 1) {
        stringQueryBuilder.append(" AND ");
      }
    }
    Query query =
        session.createQuery(
            stringQueryBuilder.toString() + " AND ee." + ModelDBConstants.DELETED + " = false ");
    for (Entry<String, Object> paramEntry : paramMap.entrySet()) {
      query.setParameter(paramEntry.getKey(), paramEntry.getValue());
    }
    List<ExperimentEntity> experimentObjList = query.list();
    return RdbmsUtils.convertExperimentsFromExperimentEntityList(experimentObjList);
  } catch (Exception ex) {
    if (ModelDBUtils.needToRetry(ex)) {
      return getExperiments(keyValues);
    } else {
      throw ex;
    }
  }
}
 
Example 19
Source File: ValueUtils.java    From cloud-spanner-r2dbc with Apache License 2.0 4 votes vote down vote up
private static Boolean parseBoolean(Value input) {
  return input.getKindCase() == KindCase.NULL_VALUE ? null : input.getBoolValue();
}
 
Example 20
Source File: ValueUtils.java    From cloud-spanner-r2dbc with Apache License 2.0 4 votes vote down vote up
private static String parseString(Value input) {
  return input.getKindCase() == KindCase.NULL_VALUE ? null : input.getStringValue();
}