return withBuilder(field, action); Builder fieldBuilder = new Builder(currentValue, valueBuilder, locations); action.accept(fieldBuilder); ConfigSource configMessage = fieldBuilder.build(); setValue(field, key, configMessage.configMessage, null); return this;
/** * Sets the given scalar value on the field. If optional key is provided, the field must * represent a map, and the value under the key is set. */ public Builder setValue( FieldDescriptor field, @Nullable Object key, Object value, @Nullable Location location) { if (key == null) { configBuilder.setField(field, value); } else { putMapEntry(configBuilder, field, key, value); } addLocation(field, key, location); return this; }
return helper.getDiag().getErrorCount() == initialErrorCount ? builder.build() : null;
return helper.getDiag().getErrorCount() == initialErrorCount ? builder.build() : null;
if (field.isMapField()) { for (final MapEntry<Object, Message> entry : (List<MapEntry<Object, Message>>) value) { withBuilder( field, entry.getKey(), withBuilderAt( field, i++, withBuilder( field, null,
if (field.isMapField()) { for (final MapEntry<Object, Message> entry : (List<MapEntry<Object, Message>>) value) { withBuilder( field, entry.getKey(), withBuilderAt( field, i++, withBuilder( field, null,
Descriptor messageType = builder.getDescriptorForType();
Descriptor messageType = builder.getDescriptorForType();
String nestedPath = appendToPath(path, keyObj); helper.checkAndAddPath(nestedPath, value, field); builder.withBuilder(field, keyObj, new ReadNodeBuildAction(helper, entry.getValueNode(), appendToPath(nestedPath, keyObj))); } else { Object valueObj = NodeConverterUtils.convert(helper, valueField, entry.getValueNode()); if (valueObj != null) { builder.setValue(field, keyObj, valueObj, helper.getLocation(entry.getValueNode())); for (Node elem : list.getValue()) { String indexedPath = String.format("%s[%s]", path, index++); builder.withAddedBuilder(field, new ReadNodeBuildAction(helper, elem, indexedPath)); builder.withBuilder(field, new ReadNodeBuildAction(helper, value, path));
String nestedPath = appendToPath(path, keyObj); helper.checkAndAddPath(nestedPath, value, field); builder.withBuilder(field, keyObj, new ReadNodeBuildAction(helper, entry.getValueNode(), appendToPath(nestedPath, keyObj))); } else { Object valueObj = NodeConverterUtils.convert(helper, valueField, entry.getValueNode()); if (valueObj != null) { builder.setValue(field, keyObj, valueObj, helper.getLocation(entry.getValueNode())); for (Node elem : list.getValue()) { String indexedPath = String.format("%s[%s]", path, index++); builder.withAddedBuilder(field, new ReadNodeBuildAction(helper, elem, indexedPath)); builder.withBuilder(field, new ReadNodeBuildAction(helper, value, path));
@Test public void mergeTest() { ConfigSource.Builder builder = ConfigSource.newBuilder(SomeConfig.getDefaultInstance()); builder.setValue(STRING_VALUE_FIELD, null, "A", L1); builder.withBuilder( NESTED_CONFIG_FIELD, null, builder.addValue(REPEATED_STRING_VALUE_FIELD, "a", L1); ConfigSource source = builder.build(); builder.setValue(STRING_VALUE_FIELD, null, "", L2); builder.withBuilder( NESTED_CONFIG_FIELD, null, builder.addValue(REPEATED_STRING_VALUE_FIELD, "b", L2); ConfigSource source2 = builder.build(); source = source.toBuilder().mergeFrom(source2).build();
@Test public void nestedRepeatedField() { ConfigSource.Builder builder = ConfigSource.newBuilder(SomeConfig.getDefaultInstance()); builder.withAddedBuilder( REPEATED_NESTED_CONFIG_FIELD, new BuildAction() { builder.withAddedBuilder( REPEATED_NESTED_CONFIG_FIELD, new BuildAction() { ConfigSource source = builder.build(); SomeConfig config = (SomeConfig) source.getConfig(); Truth.assertThat(config.getRepeatedNestedConfigCount()).isEqualTo(2);
@Test public void nestedMapField() { ConfigSource.Builder builder = ConfigSource.newBuilder(SomeConfig.getDefaultInstance()); builder.withBuilder( NESTED_MAP_VALUE_FIELD, "X", builder.withBuilder( NESTED_MAP_VALUE_FIELD, "Y", ConfigSource source = builder.build(); SomeConfig config = (SomeConfig) source.getConfig();
return withBuilder(field, action); Builder fieldBuilder = new Builder(currentValue, valueBuilder, locations); action.accept(fieldBuilder); ConfigSource configMessage = fieldBuilder.build(); setValue(field, key, configMessage.configMessage, null); return this;
mergedConfigs.put(descriptor, config.toBuilder()); } else if (experiments.isExperimentEnabled(PROTO3_CONFIG_MERGING_EXPERIMENT)) { builder.mergeFromWithProto3Semantics(config); } else { builder.mergeFrom(config); setServiceConfig(serviceConfig.build()); } else { UInt32Value.newBuilder().setValue(Model.getDefaultConfigVersion())) .build()) .build());
mergedConfigs.put(descriptor, config.toBuilder()); } else if (experiments.isExperimentEnabled(PROTO3_CONFIG_MERGING_EXPERIMENT)) { builder.mergeFromWithProto3Semantics(config); } else { builder.mergeFrom(config); setServiceConfig(serviceConfig.build()); } else { UInt32Value.newBuilder().setValue(Model.getDefaultConfigVersion())) .build()) .build());
/** * 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; }
@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); }