@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()); }
@Test public void sort_commands_by_timestamp() { final Command cmd1 = requestFactory.createCommand(StringValue.getDefaultInstance(), minutesAgo(1)); final Command cmd2 = requestFactory.createCommand(Int64Value.getDefaultInstance(), secondsAgo(30)); final Command cmd3 = requestFactory.createCommand(BoolValue.getDefaultInstance(), secondsAgo(5)); final List<Command> sortedCommands = newArrayList(cmd1, cmd2, cmd3); final List<Command> commandsToSort = newArrayList(cmd3, cmd1, cmd2); assertFalse(sortedCommands.equals(commandsToSort)); Commands.sort(commandsToSort); assertEquals(sortedCommands, commandsToSort); }
@Test public void create_wereBetween_predicate() { final Command command1 = requestFactory.createCommand(StringValue.getDefaultInstance(), minutesAgo(5)); final Command command2 = requestFactory.createCommand(Int64Value.getDefaultInstance(), minutesAgo(2)); final Command command3 = requestFactory.createCommand(BoolValue.getDefaultInstance(), secondsAgo(30)); final Command command4 = requestFactory.createCommand(BoolValue.getDefaultInstance(), secondsAgo(20)); final Command command5 = requestFactory.createCommand(BoolValue.getDefaultInstance(), secondsAgo(5)); final ImmutableList<Command> commands = ImmutableList.of(command1, command2, command3, command4, command5); final Iterable<Command> filter = Iterables.filter( commands, Commands.wereWithinPeriod(minutesAgo(3), secondsAgo(10)) ); assertEquals(3, FluentIterable.from(filter) .size()); }
Int64ValueMarshaller() { super(Int64Value.getDefaultInstance()); }
@Override protected final void doMerge(JsonParser parser, int unused, Message.Builder messageBuilder) throws IOException { Int64Value.Builder builder = (Int64Value.Builder) messageBuilder; builder.setValue(ParseSupport.parseInt64(parser)); }
@Override protected final void doWrite(Int64Value message, JsonGenerator gen) throws IOException { SerializeSupport.printSignedInt64(message.getValue(), gen); }
@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()); }
/** * Convert a protocol buffer field value to {@link Any} with the below rule: * <ul> * <li> If the field is a primitive type and can be mapped to a wrapper message type * defined in //tech/type/proto/wrappers.proto, its value is boxed into a wrapper * and goes to Any.value, the type name of the wrapper goes to Any.type_url. * <li> If the field is already a message, its type name goes to Any.type_url, * its value directly goes to Any.value * <li> If the field is an enum value, the name of the enum value is boxed into * tech.type.String and put into Any.value, and tech.type.String goes to Any.type_url. */ private static Any toAnyType(FieldDescriptor.Type protobufType, Object value) { Any.Builder builder = Any.newBuilder(); java.lang.String typeFullName; Message wrapperMessage; switch (protobufType) { case MESSAGE: wrapperMessage = (Message) value; typeFullName = wrapperMessage.getDescriptorForType().getFullName(); break; case ENUM: // NOTE: Erasing the enum type to the String wrapper is currently intentional, to avoid // the need to add an enum wrapper type. This may change in the future. typeFullName = StringValue.getDescriptor().getFullName(); wrapperMessage = StringValue.newBuilder().setValue(((EnumValueDescriptor) value).getName()).build(); break; case BOOL: typeFullName = BoolValue.getDescriptor().getFullName(); wrapperMessage = BoolValue.newBuilder().setValue((Boolean) value).build(); break; case DOUBLE: typeFullName = DoubleValue.getDescriptor().getFullName(); wrapperMessage = DoubleValue.newBuilder().setValue((java.lang.Double) value).build(); break; case FLOAT: typeFullName = FloatValue.getDescriptor().getFullName(); wrapperMessage = FloatValue.newBuilder().setValue((java.lang.Float) value).build(); break; case STRING: typeFullName = StringValue.getDescriptor().getFullName(); wrapperMessage = StringValue.newBuilder().setValue((java.lang.String) value).build(); break; case SINT32: case SFIXED32: case INT32: typeFullName = Int32Value.getDescriptor().getFullName(); wrapperMessage = Int32Value.newBuilder().setValue((Integer) value).build(); break; case SINT64: case SFIXED64: case INT64: typeFullName = Int64Value.getDescriptor().getFullName(); wrapperMessage = Int64Value.newBuilder().setValue((Long) value).build(); break; case UINT32: case FIXED32: typeFullName = UInt32Value.getDescriptor().getFullName(); wrapperMessage = UInt32Value.newBuilder().setValue((Integer) value).build(); break; case UINT64: case FIXED64: typeFullName = UInt64Value.getDescriptor().getFullName(); wrapperMessage = UInt64Value.newBuilder().setValue((Long) value).build(); break; case BYTES: typeFullName = BytesValue.getDescriptor().getFullName(); wrapperMessage = BytesValue.newBuilder().setValue(ByteString.copyFrom((byte[]) value)) .build(); break; default: throw new IllegalArgumentException("Type " + protobufType.name() + " cannot be converted to Any type."); } return builder.setTypeUrl(TYPE_SERVICE_BASE_URL + "/" + typeFullName) .setValue(wrapperMessage.toByteString()).build(); }
@SuppressWarnings("unused") // Used for the Model construction @Assign Iterable<Message> handle(Int64Value command) { // NoOp for test return Collections.emptySet(); }
@Test public void return_long_msg_field_class_by_descriptor() { assertReturnsFieldClass(Long.class, Int64Value.getDescriptor()); }
public void testAnyInMaps() throws Exception { JsonFormat.TypeRegistry registry = JsonFormat.TypeRegistry.newBuilder().add(TestAllTypes.getDescriptor()).build(); JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry); 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()); assertEquals( "{\n" + " \"anyMap\": {\n" + " \"int32_wrapper\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.Int32Value\",\n" + " \"value\": 123\n" + " },\n" + " \"int64_wrapper\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.Int64Value\",\n" + " \"value\": \"456\"\n" + " },\n" + " \"timestamp\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\",\n" + " \"value\": \"1969-12-31T23:59:59Z\"\n" + " },\n" + " \"duration\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n" + " \"value\": \"12345.100s\"\n" + " },\n" + " \"field_mask\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\",\n" + " \"value\": \"foo.bar,baz\"\n" + " },\n" + " \"struct\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\n" + " \"value\": {\n" + " \"number\": 1.125\n" + " }\n" + " },\n" + " \"list_value\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.ListValue\",\n" + " \"value\": [1.125, null]\n" + " },\n" + " \"number_value\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n" + " \"value\": 1.125\n" + " },\n" + " \"any_value_number\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n" + " \"value\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n" + " \"value\": 1.125\n" + " }\n" + " },\n" + " \"any_value_default\": {\n" + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n" + " \"value\": {}\n" + " },\n" + " \"default\": {}\n" + " }\n" + "}", printer.print(testAny.build())); assertRoundTripEquals(testAny.build(), registry); }