Search in sources :

Example 1 with Value

use of com.google.protobuf.Value in project core-java by SpineEventEngine.

the class EventException method eventTypeAttribute.

/**
 * Returns a map with an event type attribute.
 *
 * @param eventMessage an event message to get the type from
 */
public static Map<String, Value> eventTypeAttribute(Message eventMessage) {
    final String type = TypeName.of(eventMessage).value();
    final Value value = Value.newBuilder().setStringValue(type).build();
    final Map<String, Value> result = ImmutableMap.of(ATTR_EVENT_TYPE_NAME, value);
    return result;
}
Also used : Value(com.google.protobuf.Value)

Example 2 with Value

use of com.google.protobuf.Value in project core-java by SpineEventEngine.

the class CommandException method commandTypeAttribute.

/**
 * Returns a map with a command type attribute.
 *
 * @param commandMessage a command message to get the type from
 */
public static Map<String, Value> commandTypeAttribute(Message commandMessage) {
    final String commandType = TypeName.of(commandMessage).value();
    final Value value = Value.newBuilder().setStringValue(commandType).build();
    return ImmutableMap.of(ATTR_COMMAND_TYPE_NAME, value);
}
Also used : Value(com.google.protobuf.Value)

Example 3 with Value

use of com.google.protobuf.Value in project curiostack by curioswitch.

the class MessageMarshallerTest method anyFields.

@Test
public void anyFields() throws Exception {
    TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).build();
    TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build();
    assertMatchesUpstream(message, TestAllTypes.getDefaultInstance());
    TestAny messageWithDefaultAnyValue = TestAny.newBuilder().setAnyValue(Any.getDefaultInstance()).build();
    assertMatchesUpstream(messageWithDefaultAnyValue);
    // Well-known types have a special formatting when embedded in Any.
    // 
    // 1. Any in Any.
    Any anyMessage = Any.pack(Any.pack(content));
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 2. Wrappers in Any.
    anyMessage = Any.pack(Int32Value.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(UInt32Value.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(Int64Value.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(UInt64Value.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(FloatValue.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(DoubleValue.newBuilder().setValue(12345).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(BoolValue.newBuilder().setValue(true).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(StringValue.newBuilder().setValue("Hello").build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    anyMessage = Any.pack(BytesValue.newBuilder().setValue(ByteString.copyFrom(new byte[] { 1, 2 })).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 3. Timestamp in Any.
    anyMessage = Any.pack(Timestamps.parse("1969-12-31T23:59:59Z"));
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 4. Duration in Any
    anyMessage = Any.pack(Durations.parse("12345.10s"));
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 5. FieldMask in Any
    anyMessage = Any.pack(FieldMaskUtil.fromString("foo.bar,baz"));
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 6. Struct in Any
    Struct.Builder structBuilder = Struct.newBuilder();
    structBuilder.putFields("number", Value.newBuilder().setNumberValue(1.125).build());
    anyMessage = Any.pack(structBuilder.build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 7. Value (number type) in Any
    Value.Builder valueBuilder = Value.newBuilder();
    valueBuilder.setNumberValue(1);
    anyMessage = Any.pack(valueBuilder.build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
    // 8. Value (null type) in Any
    anyMessage = Any.pack(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
    assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
}
Also used : TestAny(com.google.protobuf.util.JsonTestProto.TestAny) Int64Value(com.google.protobuf.Int64Value) BoolValue(com.google.protobuf.BoolValue) UInt64Value(com.google.protobuf.UInt64Value) StringValue(com.google.protobuf.StringValue) NullValue(com.google.protobuf.NullValue) Value(com.google.protobuf.Value) DoubleValue(com.google.protobuf.DoubleValue) Int32Value(com.google.protobuf.Int32Value) FloatValue(com.google.protobuf.FloatValue) UInt32Value(com.google.protobuf.UInt32Value) BytesValue(com.google.protobuf.BytesValue) ListValue(com.google.protobuf.ListValue) TestAllTypes(com.google.protobuf.util.JsonTestProto.TestAllTypes) TestAny(com.google.protobuf.util.JsonTestProto.TestAny) Any(com.google.protobuf.Any) TestStruct(com.google.protobuf.util.JsonTestProto.TestStruct) Struct(com.google.protobuf.Struct) Test(org.junit.Test)

Example 4 with Value

use of com.google.protobuf.Value in project curiostack by curioswitch.

the class MessageMarshallerTest method anyInMaps.

@Test
public void anyInMaps() throws Exception {
    TestAny.Builder testAny = TestAny.newBuilder();
    testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue(123).build()));
    testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue(456).build()));
    testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")));
    testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
    testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz")));
    Value numberValue = Value.newBuilder().setNumberValue(1.125).build();
    Struct.Builder struct = Struct.newBuilder();
    struct.putFields("number", numberValue);
    testAny.putAnyMap("struct", Any.pack(struct.build()));
    Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
    testAny.putAnyMap("list_value", Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).build()));
    testAny.putAnyMap("number_value", Any.pack(numberValue));
    testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue)));
    testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance()));
    testAny.putAnyMap("default", Any.getDefaultInstance());
    assertMatchesUpstream(testAny.build(), TestAllTypes.getDefaultInstance());
}
Also used : TestAny(com.google.protobuf.util.JsonTestProto.TestAny) Int64Value(com.google.protobuf.Int64Value) BoolValue(com.google.protobuf.BoolValue) UInt64Value(com.google.protobuf.UInt64Value) StringValue(com.google.protobuf.StringValue) NullValue(com.google.protobuf.NullValue) Value(com.google.protobuf.Value) DoubleValue(com.google.protobuf.DoubleValue) Int32Value(com.google.protobuf.Int32Value) FloatValue(com.google.protobuf.FloatValue) UInt32Value(com.google.protobuf.UInt32Value) BytesValue(com.google.protobuf.BytesValue) ListValue(com.google.protobuf.ListValue) TestStruct(com.google.protobuf.util.JsonTestProto.TestStruct) Struct(com.google.protobuf.Struct) Test(org.junit.Test)

Example 5 with Value

use of com.google.protobuf.Value in project seldon-core by SeldonIO.

the class TestPredictionProto method defaultRequest.

@Test
public void defaultRequest() throws InvalidProtocolBufferException {
    String[] features = { "a", "b" };
    Double[] values = { 1.0, 2.0, 1.5, 2.2 };
    DefaultData.Builder defB = DefaultData.newBuilder();
    defB.addAllNames(Arrays.asList(features));
    defB.setTensor(Tensor.newBuilder().addShape(1).addShape(values.length).addAllValues(Arrays.asList(values)).build());
    SeldonMessage.Builder b = SeldonMessage.newBuilder();
    Value v;
    Value v1 = Value.newBuilder().setNumberValue(1.0).build();
    b.setData(defB.build()).setMeta(Meta.newBuilder().putTags("key", v1).build());
    SeldonMessage request = b.build();
    String json = ProtoBufUtils.toJson(request);
    System.out.println(json);
    SeldonMessage.Builder b2 = SeldonMessage.newBuilder();
    ProtoBufUtils.updateMessageBuilderFromJson(b2, json);
    SeldonMessage request2 = b2.build();
    String json2 = ProtoBufUtils.toJson(request2);
    System.out.println(json2);
    Assert.assertEquals(json, json2);
}
Also used : SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) Value(com.google.protobuf.Value) ByteString(com.google.protobuf.ByteString) DefaultData(io.seldon.protos.PredictionProtos.DefaultData) Test(org.junit.Test)

Aggregations

Value (com.google.protobuf.Value)5 Test (org.junit.Test)3 BoolValue (com.google.protobuf.BoolValue)2 BytesValue (com.google.protobuf.BytesValue)2 DoubleValue (com.google.protobuf.DoubleValue)2 FloatValue (com.google.protobuf.FloatValue)2 Int32Value (com.google.protobuf.Int32Value)2 Int64Value (com.google.protobuf.Int64Value)2 ListValue (com.google.protobuf.ListValue)2 NullValue (com.google.protobuf.NullValue)2 StringValue (com.google.protobuf.StringValue)2 Struct (com.google.protobuf.Struct)2 UInt32Value (com.google.protobuf.UInt32Value)2 UInt64Value (com.google.protobuf.UInt64Value)2 TestAny (com.google.protobuf.util.JsonTestProto.TestAny)2 TestStruct (com.google.protobuf.util.JsonTestProto.TestStruct)2 Any (com.google.protobuf.Any)1 ByteString (com.google.protobuf.ByteString)1 TestAllTypes (com.google.protobuf.util.JsonTestProto.TestAllTypes)1 DefaultData (io.seldon.protos.PredictionProtos.DefaultData)1