@java.lang.Override public Builder newBuilderForType() { return newBuilder(); } public static Builder newBuilder() {
private static Object valueToObject(Value value) { switch (value.getKindCase()) { case NULL_VALUE: return null; case NUMBER_VALUE: return value.getNumberValue(); case STRING_VALUE: return value.getStringValue(); case BOOL_VALUE: return value.getBoolValue(); case STRUCT_VALUE: return new StructMap(value.getStructValue()); case LIST_VALUE: return Lists.transform(value.getListValue().getValuesList(), VALUE_TO_OBJECT); default: throw new IllegalArgumentException(String.format("Unsupported protobuf value %s", value)); } }
hash = (19 * hash) + getDescriptor().hashCode(); switch (kindCase_) { case 1: hash = (37 * hash) + NULL_VALUE_FIELD_NUMBER; hash = (53 * hash) + getNullValueValue(); break; case 2: hash = (37 * hash) + NUMBER_VALUE_FIELD_NUMBER; hash = (53 * hash) + com.google.protobuf.Internal.hashLong( java.lang.Double.doubleToLongBits(getNumberValue())); break; case 3: hash = (37 * hash) + STRING_VALUE_FIELD_NUMBER; hash = (53 * hash) + getStringValue().hashCode(); break; case 4: hash = (37 * hash) + BOOL_VALUE_FIELD_NUMBER; hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean( getBoolValue()); break; case 5: hash = (37 * hash) + STRUCT_VALUE_FIELD_NUMBER; hash = (53 * hash) + getStructValue().hashCode(); break; case 6: hash = (37 * hash) + LIST_VALUE_FIELD_NUMBER; hash = (53 * hash) + getListValue().hashCode(); break; case 0:
@Override public Boolean apply(com.google.protobuf.Value input) { return input.getKindCase() == KindCase.NULL_VALUE ? null : input.getBoolValue(); } });
@Override public String apply(com.google.protobuf.Value input) { return input.getKindCase() == KindCase.NULL_VALUE ? null : input.getStringValue(); } });
com.google.protobuf.Value last = a.get(a.size() - 1); com.google.protobuf.Value first = b.get(0); KindCase lastKind = last.getKindCase(); KindCase firstKind = first.getKindCase(); if (isMergeable(lastKind) && lastKind == firstKind) { com.google.protobuf.Value merged = null; if (lastKind == KindCase.STRING_VALUE) { String lastStr = last.getStringValue(); String firstStr = first.getStringValue(); merged = com.google.protobuf.Value.newBuilder().setStringValue(lastStr + firstStr).build(); } else { // List List<com.google.protobuf.Value> mergedList = new ArrayList<>(); mergedList.addAll(last.getListValue().getValuesList()); concatLists(mergedList, first.getListValue().getValuesList()); merged = com.google.protobuf.Value.newBuilder() .setListValue(ListValue.newBuilder().addAllValues(mergedList)) .build();
private static double valueProtoToFloat64(com.google.protobuf.Value proto) { if (proto.getKindCase() == KindCase.STRING_VALUE) { switch (proto.getStringValue()) { case "-Infinity": return Double.NEGATIVE_INFINITY; case "Infinity": return Double.POSITIVE_INFINITY; case "NaN": return Double.NaN; default: // Fall-through to handling below to produce an error. } } if (proto.getKindCase() != KindCase.NUMBER_VALUE) { throw newSpannerException( ErrorCode.INTERNAL, "Invalid value for column type " + Type.float64() + " expected NUMBER_VALUE or STRING_VALUE with value one of" + " \"Infinity\", \"-Infinity\", or \"NaN\" but was " + proto.getKindCase() + (proto.getKindCase() == KindCase.STRING_VALUE ? " with value \"" + proto.getStringValue() + "\"" : "")); } return proto.getNumberValue(); }
@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()); } }
public Builder mergeFrom(com.google.protobuf.Value other) { if (other == com.google.protobuf.Value.getDefaultInstance()) return this; switch (other.getKindCase()) { case NULL_VALUE: { setNullValueValue(other.getNullValueValue()); break; setNumberValue(other.getNumberValue()); break; setBoolValue(other.getBoolValue()); break; mergeStructValue(other.getStructValue()); break; mergeListValue(other.getListValue()); break;
if (json instanceof JsonNull) { if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE && field.getMessageType().getFullName().equals(Value.getDescriptor().getFullName())) { Value value = Value.newBuilder().setNullValueValue(0).build(); return builder.newBuilderForField(field).mergeFrom(value.toByteString()).build(); } else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM && field.getEnumType().getFullName().equals(NullValue.getDescriptor().getFullName())) {
private static JsonElement valueToJsonElement(Value value) { if (value.hasStructValue()) { return structToJsonObject(value.getStructValue()); } else if (value.hasListValue()) { JsonArray values = new JsonArray(); for (Value listElement : value.getListValue().getValuesList()) { values.add(valueToJsonElement(listElement)); } return values; } else if (value.getKindCase().equals(Value.KindCase.NULL_VALUE)) { return JsonNull.INSTANCE; } else if (value.getKindCase().equals(Value.KindCase.BOOL_VALUE)) { return new JsonPrimitive(value.getBoolValue()); } else if (value.getKindCase().equals(Value.KindCase.STRING_VALUE)) { return new JsonPrimitive(value.getStringValue()); } else if (value.getKindCase().equals(Value.KindCase.NUMBER_VALUE)) { return new JsonPrimitive(value.getNumberValue()); } else { throw new ClarifaiException("Unknown metadata Value field type."); } } }
private static Object decodeValue(Type fieldType, com.google.protobuf.Value proto) { if (proto.getKindCase() == KindCase.NULL_VALUE) { return null; case BOOL: checkType(fieldType, proto, KindCase.BOOL_VALUE); return proto.getBoolValue(); case INT64: checkType(fieldType, proto, KindCase.STRING_VALUE); return Long.parseLong(proto.getStringValue()); case FLOAT64: return valueProtoToFloat64(proto); case STRING: checkType(fieldType, proto, KindCase.STRING_VALUE); return proto.getStringValue(); case BYTES: checkType(fieldType, proto, KindCase.STRING_VALUE); return ByteArray.fromBase64(proto.getStringValue()); case TIMESTAMP: checkType(fieldType, proto, KindCase.STRING_VALUE); return Timestamp.parseTimestamp(proto.getStringValue()); case DATE: checkType(fieldType, proto, KindCase.STRING_VALUE); return Date.parseDate(proto.getStringValue()); case ARRAY: checkType(fieldType, proto, KindCase.LIST_VALUE); ListValue listValue = proto.getListValue(); return decodeArrayValue(fieldType.getArrayElementType(), listValue); case STRUCT: checkType(fieldType, proto, KindCase.LIST_VALUE);
@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()); } }
for (com.google.protobuf.Value value : listValue.getValuesList()) { list.add( value.getKindCase() == KindCase.NULL_VALUE ? null : ByteArray.fromBase64(value.getStringValue())); for (com.google.protobuf.Value value : listValue.getValuesList()) { list.add( value.getKindCase() == KindCase.NULL_VALUE ? null : Timestamp.parseTimestamp(value.getStringValue())); for (com.google.protobuf.Value value : listValue.getValuesList()) { list.add( value.getKindCase() == KindCase.NULL_VALUE ? null : Date.parseDate(value.getStringValue())); if (value.getKindCase() == KindCase.NULL_VALUE) { list.add(null); } else { ListValue structValue = value.getListValue(); list.add(decodeStructValue(elementType, structValue));
@Override void setProto(long[] array, int i, com.google.protobuf.Value protoValue) { array[i] = Long.parseLong(protoValue.getStringValue()); }
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()))); } }
@Test public void testSimpleMapping() throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper m = new JsonMapperProvider().get(); assertTrue(m.canSerialize(Any.class)); Struct struct1 = Struct.newBuilder().putFields( "some-key", Value.newBuilder().setStringValue("some-value").build() ).build(); Any source = Any.pack(struct1); StringWriter buf = new StringWriter(); m.writer().writeValue(buf, source); Any dest = m.reader().forType(Any.class).readValue(buf.toString()); assertEquals(source.getTypeUrl(), dest.getTypeUrl()); Struct struct2 = dest.unpack(Struct.class); assertTrue(struct2.containsFields("some-key")); assertEquals( struct1.getFieldsOrThrow("some-key").getStringValue(), struct2.getFieldsOrThrow("some-key").getStringValue() ); } }
@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()); } }
private static void checkType( Type fieldType, com.google.protobuf.Value proto, KindCase expected) { if (proto.getKindCase() != expected) { throw newSpannerException( ErrorCode.INTERNAL, "Invalid value for column type " + fieldType + " expected " + expected + " but was " + proto.getKindCase()); } }
additionalMessages.put(Struct.getDescriptor().getFullName(), Struct.getDescriptor().toProto()); additionalMessages.put(Value.getDescriptor().getFullName(), Value.getDescriptor().toProto()); additionalMessages.put(ListValue.getDescriptor().getFullName(), ListValue.getDescriptor().toProto());