/** Sets the service config from a proto. */ @Deprecated public void setServiceConfig(Service config) { this.serviceConfig = ConfigSource.newBuilder(config).build(); }
/** Sets the service config from a proto. */ @Deprecated public void setServiceConfig(Service config) { this.serviceConfig = ConfigSource.newBuilder(config).build(); }
/** * Reads a configuration from a file containing a binary-encoded google.api.Service * proto message, reporting errors to the diag collector. * <p>Returns proto {@link ConfigSource} representing the config, or null if * errors were detected while unmarshaling the binary proto contents. */ @Nullable public static ConfigSource readBinaryConfig( DiagCollector diag, String filename, ByteString fileContents) { try { return ConfigSource.newBuilder(Service.parser().parseFrom(fileContents)).build(); } catch (InvalidProtocolBufferException e) { diag.addDiag(Diag.error( new SimpleLocation(filename), "Failed to parse google.api.Service proto message from the binary proto file.")); return null; } }
@Test public void nestedSimpleField() { ConfigSource.Builder builder = ConfigSource.newBuilder(SomeConfig.getDefaultInstance()); builder.setValue(STRING_VALUE_FIELD, null, "Hello World", L1); builder.withBuilder( NESTED_CONFIG_FIELD, null, new BuildAction() { @Override public void accept(ConfigSource.Builder subBuilder) { subBuilder.setValue(NESTED_STRING_VALUE_FIELD, null, "Sub World", L2); } }); ConfigSource source = builder.build(); SomeConfig config = (SomeConfig) source.getConfig(); Truth.assertThat(config.getStringValue()).isEqualTo("Hello World"); Truth.assertThat(source.getLocation(config, STRING_VALUE_FIELD.getName(), null)).isEqualTo(L1); Truth.assertThat(config.getNestedConfig().getNestedStringValue()).isEqualTo("Sub World"); Truth.assertThat( source.getLocation(config.getNestedConfig(), NESTED_STRING_VALUE_FIELD.getName(), null)) .isEqualTo(L2); }
/** * Creates a model from a normalized service config, rather than from descriptor and .yaml files. */ public static Model create(Service normalizedConfig) { FileDescriptorSet regeneratedDescriptor = DescriptorGenerator.generate(normalizedConfig); Model model = create(regeneratedDescriptor); // Configured with a stripped Service Service.Builder builder = normalizedConfig.toBuilder(); ImmutableList.Builder<Api> strippedApis = ImmutableList.builder(); for (Api api : normalizedConfig.getApisList()) { strippedApis.add( Api.newBuilder().setName(api.getName()).setVersion(api.getVersion()).build()); } // NOTE: Documentation may still contain text from the original protos. builder.clearEnums(); builder.clearTypes(); builder.clearApis(); builder.addAllApis(strippedApis.build()); ConfigSource strippedConfig = ConfigSource.newBuilder(builder.build()).build(); model.setConfigSources(ImmutableList.of(strippedConfig)); return model; }
/** * Constructs a sub-builder for given field and calls the action on it. After the action's * processing, the sub-message will be build and stored into the field of this builder. * Moreover, update locations of the sub-builder are integrated into this builder. */ public Builder withBuilder(FieldDescriptor field, BuildAction action) { // Construct a fresh builder for the given field and merge in the current value. As we depend // on message identity, we need to control this builder directly, so can't use implicit // building via getFieldBuilder. A builder created by getFieldBuilder as build() called // when the parent is called, resulting in a different message identity. Message currentValue = (Message) configBuilder.getField(field); Message.Builder protoBuilder = configBuilder.newBuilderForField(field); protoBuilder.mergeFrom(currentValue); // Construct config builder, and let the action process it. Builder fieldConfigBuilder = new Builder(currentValue, protoBuilder, locations); action.accept(fieldConfigBuilder); // Build config, which updates the location mapping, and update proto builder. ConfigSource fieldConfig = fieldConfigBuilder.build(); configBuilder.setField(field, fieldConfig.getConfig()); return this; }
@Test public void simpleRepeatedField() { ConfigSource.Builder builder = ConfigSource.newBuilder(SomeConfig.getDefaultInstance()); builder.addValue(REPEATED_STRING_VALUE_FIELD, "a", L1); builder.addValue(REPEATED_STRING_VALUE_FIELD, "b", L2); ConfigSource source = builder.build(); SomeConfig config = (SomeConfig) source.getConfig(); Truth.assertThat(config.getRepeatedStringValueList()).containsExactly("a", "b"); Truth.assertThat(source.getLocation(config, REPEATED_STRING_VALUE_FIELD.getName(), 0)) .isEqualTo(L1); Truth.assertThat(source.getLocation(config, REPEATED_STRING_VALUE_FIELD.getName(), 1)) .isEqualTo(L2); builder = source.toBuilder(); builder.addValue(REPEATED_STRING_VALUE_FIELD, "c", L3); source = builder.build(); config = (SomeConfig) source.getConfig(); Truth.assertThat(config.getRepeatedStringValueList()).containsExactly("a", "b", "c"); Truth.assertThat(source.getLocation(config, REPEATED_STRING_VALUE_FIELD.getName(), 0)) .isEqualTo(L1); Truth.assertThat(source.getLocation(config, REPEATED_STRING_VALUE_FIELD.getName(), 1)) .isEqualTo(L2); Truth.assertThat(source.getLocation(config, REPEATED_STRING_VALUE_FIELD.getName(), 2)) .isEqualTo(L3); }
/** * Constructs a sub-builder for given field and calls the action on it. After the action's * processing, the sub-message will be build and stored into the field of this builder. * Moreover, update locations of the sub-builder are integrated into this builder. */ public Builder withBuilder(FieldDescriptor field, BuildAction action) { // Construct a fresh builder for the given field and merge in the current value. As we depend // on message identity, we need to control this builder directly, so can't use implicit // building via getFieldBuilder. A builder created by getFieldBuilder as build() called // when the parent is called, resulting in a different message identity. Message currentValue = (Message) configBuilder.getField(field); Message.Builder protoBuilder = configBuilder.newBuilderForField(field); protoBuilder.mergeFrom(currentValue); // Construct config builder, and let the action process it. Builder fieldConfigBuilder = new Builder(currentValue, protoBuilder, locations); action.accept(fieldConfigBuilder); // Build config, which updates the location mapping, and update proto builder. ConfigSource fieldConfig = fieldConfigBuilder.build(); configBuilder.setField(field, fieldConfig.getConfig()); return this; }
/** * Creates a model from a normalized service config, rather than from descriptor and .yaml files. */ public static Model create(Service normalizedConfig) { FileDescriptorSet regeneratedDescriptor = DescriptorGenerator.generate(normalizedConfig); Model model = create(regeneratedDescriptor); // Configured with a stripped Service Service.Builder builder = normalizedConfig.toBuilder(); ImmutableList.Builder<Api> strippedApis = ImmutableList.builder(); for (Api api : normalizedConfig.getApisList()) { strippedApis.add( Api.newBuilder().setName(api.getName()).setVersion(api.getVersion()).build()); } // NOTE: Documentation may still contain text from the original protos. builder.clearEnums(); builder.clearTypes(); builder.clearApis(); builder.addAllApis(strippedApis.build()); ConfigSource strippedConfig = ConfigSource.newBuilder(builder.build()).build(); model.setConfigSources(ImmutableList.of(strippedConfig)); return model; }
/** * Reads a configuration from a file containing a binary-encoded google.api.Service * proto message, reporting errors to the diag collector. * <p>Returns proto {@link ConfigSource} representing the config, or null if * errors were detected while unmarshaling the binary proto contents. */ @Nullable public static ConfigSource readBinaryConfig( DiagCollector diag, String filename, ByteString fileContents) { try { return ConfigSource.newBuilder(Service.parser().parseFrom(fileContents)).build(); } catch (InvalidProtocolBufferException e) { diag.addDiag(Diag.error( new SimpleLocation(filename), "Failed to parse google.api.Service proto message from the binary proto file.")); return null; } }
@Nullable public static ConfigSource readTextConfig( DiagCollector diag, String filename, ByteString fileContents) { try { Service.Builder builder = Service.newBuilder(); TextFormat.getParser().merge(fileContents.toStringUtf8(), builder); return ConfigSource.newBuilder(builder.build()).build(); } catch (final TextFormat.ParseException e) { diag.addDiag(Diag.error( new SimpleLocation( String.format("%s:%d:%d", filename, e.getLine(), e.getColumn()), filename), "Failed to parse google.api.Service proto message from the text proto file.")); return null; } } }
@Nullable public static ConfigSource readTextConfig( DiagCollector diag, String filename, ByteString fileContents) { try { Service.Builder builder = Service.newBuilder(); TextFormat.getParser().merge(fileContents.toStringUtf8(), builder); return ConfigSource.newBuilder(builder.build()).build(); } catch (final TextFormat.ParseException e) { diag.addDiag(Diag.error( new SimpleLocation( String.format("%s:%d:%d", filename, e.getLine(), e.getColumn()), filename), "Failed to parse google.api.Service proto message from the text proto file.")); return null; } } }
/** * Constructs a sub-builder for a element at given index of the repeated message field, and * calls action on it. */ public Builder withBuilderAt(FieldDescriptor field, int index, BuildAction action) { Message.Builder repeatedFieldBuilder = configBuilder.newBuilderForField(field); repeatedFieldBuilder.mergeFrom((Message) configBuilder.getRepeatedField(field, index)); Builder elementBuilder = new Builder(repeatedFieldBuilder.build(), repeatedFieldBuilder, locations); action.accept(elementBuilder); // Call build so locations map is updated. ConfigSource configSource = elementBuilder.build(); // Update the list with the built element configBuilder.setRepeatedField(field, index, configSource.configMessage); return this; }
/** * Constructs a sub-builder for a element at given index of the repeated message field, and * calls action on it. */ public Builder withBuilderAt(FieldDescriptor field, int index, BuildAction action) { Message.Builder repeatedFieldBuilder = configBuilder.newBuilderForField(field); repeatedFieldBuilder.mergeFrom((Message) configBuilder.getRepeatedField(field, index)); Builder elementBuilder = new Builder(repeatedFieldBuilder.build(), repeatedFieldBuilder, locations); action.accept(elementBuilder); // Call build so locations map is updated. ConfigSource configSource = elementBuilder.build(); // Update the list with the built element configBuilder.setRepeatedField(field, index, configSource.configMessage); return this; }
/** * Constructs a sub-builder for an added element of the repeated message field, and calls action * on it. */ public Builder withAddedBuilder(FieldDescriptor field, BuildAction action) { Message.Builder repeatedFieldBuilder = configBuilder.newBuilderForField(field); Builder elementBuilder = new Builder(repeatedFieldBuilder.build(), repeatedFieldBuilder, locations); action.accept(elementBuilder); // Call build so locations map is updated. ConfigSource configSource = elementBuilder.build(); // Update the list with the built element configBuilder.addRepeatedField(field, configSource.configMessage); return this; }
@Test public void simpleField() { ConfigSource.Builder builder = ConfigSource.newBuilder(SomeConfig.getDefaultInstance()); builder.setValue(STRING_VALUE_FIELD, null, "Hello World", L1); ConfigSource source = builder.build(); SomeConfig config = (SomeConfig) source.getConfig(); Truth.assertThat(config.getStringValue()).isEqualTo("Hello World"); Truth.assertThat(source.getLocation(config, STRING_VALUE_FIELD.getName(), null)).isEqualTo(L1); builder = source.toBuilder(); builder.setValue(STRING_VALUE_FIELD, null, "", L2); source = builder.build(); config = (SomeConfig) source.getConfig(); Truth.assertThat(config.getStringValue()).isEqualTo(""); Truth.assertThat(source.getLocation(config, STRING_VALUE_FIELD.getName(), null)).isEqualTo(L2); }
/** * Constructs a sub-builder for an added element of the repeated message field, and calls action * on it. */ public Builder withAddedBuilder(FieldDescriptor field, BuildAction action) { Message.Builder repeatedFieldBuilder = configBuilder.newBuilderForField(field); Builder elementBuilder = new Builder(repeatedFieldBuilder.build(), repeatedFieldBuilder, locations); action.accept(elementBuilder); // Call build so locations map is updated. ConfigSource configSource = elementBuilder.build(); // Update the list with the built element configBuilder.addRepeatedField(field, configSource.configMessage); return this; }
@Test public void mapField() { ConfigSource.Builder builder = ConfigSource.newBuilder(SomeConfig.getDefaultInstance()); builder.setValue(MAP_VALUE_FIELD, "X", "A", L1); builder.setValue(MAP_VALUE_FIELD, "Y", "B", L2); ConfigSource source = builder.build(); SomeConfig config = (SomeConfig) source.getConfig(); Truth.assertThat(config.getMapValue().get("X")).isEqualTo("A"); Truth.assertThat(config.getMapValue().get("Y")).isEqualTo("B"); Truth.assertThat(source.getLocation(config, MAP_VALUE_FIELD.getName(), "X")).isEqualTo(L1); Truth.assertThat(source.getLocation(config, MAP_VALUE_FIELD.getName(), "Y")).isEqualTo(L2); }
@Override public ConfigSource apply(Message input) { return ConfigSource.newBuilder(input).build(); } }));
@Override public ConfigSource apply(Message input) { return ConfigSource.newBuilder(input).build(); } }));