/** * Sets the service config based on a sequence of sources of heterogeneous types. Sources of the * same type will be merged together, and those applicable to the framework will be attached to * it. */ public void setConfigSources(Iterable<ConfigSource> configs) { // Merge configs of same type. Map<Descriptor, ConfigSource.Builder> mergedConfigs = Maps.newHashMap(); for (ConfigSource config : configs) { Descriptor descriptor = config.getConfig().getDescriptorForType(); ConfigSource.Builder builder = mergedConfigs.get(descriptor); if (builder == null) { mergedConfigs.put(descriptor, config.toBuilder()); } else if (experiments.isExperimentEnabled(PROTO3_CONFIG_MERGING_EXPERIMENT)) { builder.mergeFromWithProto3Semantics(config); } else { builder.mergeFrom(config); } } // Pick the configs we know and care about (currently, Service and Legacy). ConfigSource.Builder serviceConfig = mergedConfigs.get(Service.getDescriptor()); if (serviceConfig != null) { setServiceConfig(serviceConfig.build()); } else { // Set empty config. setServiceConfig( ConfigSource.newBuilder( Service.newBuilder() .setConfigVersion( UInt32Value.newBuilder().setValue(Model.getDefaultConfigVersion())) .build()) .build()); } }
/** Sets special configuration needed for 3rd party Endpoints APIs. */ private void applyThirdPartyApiSettings(Service.Builder serviceBuilder) { serviceBuilder.getControlBuilder().setEnvironment(ControlConfigUtil.PROD_SERVICE_CONTROL); // Set the config version to 3. serviceBuilder.setConfigVersion( UInt32Value.newBuilder().setValue(TOOLS_CONFIG_VERSION).build()); }
/** Rejects a negative value via command rejection. */ @Assign Timestamp on(UInt32Value value) { if (value.getValue() < 0) { throw new IllegalArgumentException("Negative value passed"); } return Time.getCurrentTime(); }
@Override protected TimerCounter createAggregatePart() { final TimerCounterRoot root = new TimerCounterRoot(boundedContext, Identifier.newUuid()); final TimerCounter result = Given.aggregatePartOfClass(TimerCounter.class) .withRoot(root) .withId(getClass().getName()) .withVersion(5) .withState(UInt32Value.newBuilder() .setValue(42) .build()) .build(); return result; }
UInt32ValueMarshaller() { super(UInt32Value.getDefaultInstance()); }
@Override protected final void doMerge(JsonParser parser, int unused, Message.Builder messageBuilder) throws IOException { UInt32Value.Builder builder = (UInt32Value.Builder) messageBuilder; builder.setValue(ParseSupport.parseUInt32(parser)); }
@Override protected final void doWrite(UInt32Value message, JsonGenerator gen) throws IOException { SerializeSupport.printUnsignedInt32(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(); }
private void restify(MethodKind httpKind, String simpleName, String template) { Model model = Model.create(FileDescriptorSet.getDefaultInstance()); model.setServiceConfig( ConfigSource.newBuilder(Service.getDefaultInstance()) .setValue( Service.getDescriptor().findFieldByNumber(Service.CONFIG_VERSION_FIELD_NUMBER), null, UInt32Value.newBuilder().setValue(configVersion).build(), new SimpleLocation("from test")) .build()); HttpConfigAspect aspect = HttpConfigAspect.create(model); ProtoFile file = ProtoFile.create( model, FileDescriptorProto.getDefaultInstance(), true, ExtensionPool.EMPTY); Interface iface = Interface.create(file, ServiceDescriptorProto.getDefaultInstance(), ""); Method method = Method.create(iface, MethodDescriptorProto.newBuilder().setName(simpleName).build(), ""); RestMethod restMethod; ImmutableList<PathSegment> path = parse(model, template); if (!model.getDiagReporter().getDiagCollector().getDiags().isEmpty()) { restMethod = RestMethod.create(method, RestKind.CUSTOM, "*error*", "*error*", null); } else { HttpRule httpRule = HttpRule.getDefaultInstance(); HttpAttribute httpConfig = new HttpAttribute( httpRule, httpKind, MessageType.create(file, Empty.getDescriptor().toProto(), "", ExtensionPool.EMPTY), path, "", false, ImmutableList.<HttpAttribute>of(), false); RestAnalyzer analyzer = new RestAnalyzer(aspect); restMethod = analyzer.analyzeMethod(method, httpConfig); } PrintWriter pw = testOutput(); pw.print(httpKind.toString()); pw.print(" "); pw.print(simpleName); pw.print(" "); pw.print(template.isEmpty() ? "(empty)" : template); pw.println(); pw.println(Strings.repeat("=", 70)); pw.printf("Rest Kind: %s\n", restMethod.getRestKind()); pw.printf( "Version: %s\n", restMethod.getVersion().isEmpty() ? "(empty)" : restMethod.getVersion()); pw.printf( "Version with default: %s\n", restMethod.getVersionWithDefault().isEmpty() ? "(empty)" : restMethod.getVersionWithDefault()); pw.printf( "Simple collection: %s\n", restMethod.getRestCollectionName().isEmpty() ? "(empty)" : restMethod.getSimpleRestCollectionName()); pw.printf( "Versioned collection: %s\n", restMethod.getRestCollectionName().isEmpty() ? "(empty)" : restMethod.getRestCollectionName()); pw.printf("Base collection: %s\n", restMethod.getBaseRestCollectionName().isEmpty() ? "(empty)" : restMethod.getBaseRestCollectionName()); pw.printf("Custom Name: %s\n", restMethod.getRestKind() == RestKind.CUSTOM ? restMethod.getRestMethodName() : "(null)"); List<Diag> diags = model.getDiagReporter().getDiagCollector().getDiags(); if (diags.size() > 0) { pw.println("Diagnostics:"); for (Diag d : diags) { pw.printf(" %s\n", DiagUtils.getDiagToPrint(d, true)); } } pw.println(); }
@Assign public List<UInt32Value> handle(UInt32Value command) { return singletonList(command); }
@Subscribe public void on(UInt32Value message) { // Do nothing. Just expose the method. }
@Assign StringValue handle(UInt32Value value) { final String digitalPart = String.valueOf(value.getValue()); return logItem(digitalPart); }