use of com.google.api.codegen.metacode.InitValueConfig in project toolkit by googleapis.
the class InitCodeTransformer method setInitValueAndComments.
private void setInitValueAndComments(SimpleInitCodeLineView.Builder surfaceLine, MethodContext context, InitCodeNode item, boolean isFirstItem) {
SurfaceNamer namer = context.getNamer();
ImportTypeTable typeTable = context.getTypeTable();
InitValueConfig initValueConfig = item.getInitValueConfig();
FieldConfig fieldConfig = item.getFieldConfig();
// Output variables
InitValueView initValue;
String comment = "";
if (context.getFeatureConfig().useResourceNameFormatOptionInSample(context, fieldConfig)) {
if (!context.isFlattenedMethodContext()) {
ResourceNameConfig messageResNameConfig = fieldConfig.getMessageResourceNameConfig();
if (messageResNameConfig == null || messageResNameConfig.getResourceNameType() != ResourceNameType.ANY) {
// In a non-flattened context, we always use the resource name type set on the message
// instead of set on the flattened method, unless the resource name type on message
// is ANY.
fieldConfig = fieldConfig.getMessageFieldConfig();
}
}
if (item.getType().isRepeated()) {
initValue = RepeatedResourceNameInitValueView.newBuilder().resourceTypeName(namer.getAndSaveElementResourceTypeName(context.getTypeTable(), fieldConfig)).build();
} else {
initValue = createInitValueView(context, fieldConfig, namer, typeTable, item, false);
}
} else if (initValueConfig.hasFormattingConfig() && !item.getType().isRepeated()) {
if (context.getFeatureConfig().enableStringFormatFunctions() || fieldConfig.getResourceNameConfig() == null) {
initValue = createFormattedInitValueView(context, fieldConfig, item, initValueConfig);
} else {
initValue = createInitValueView(context, fieldConfig, namer, typeTable, item, true);
}
} else {
SimpleInitValueView.Builder simpleInitValue = SimpleInitValueView.newBuilder();
if (initValueConfig.hasSimpleInitialValue()) {
String value = initValueConfig.getInitialValue().getValue();
switch(initValueConfig.getInitialValue().getType()) {
case Literal:
if (item.getType().isEnum()) {
value = context.getTypeTable().getEnumValue(item.getType(), value);
} else {
value = context.getTypeTable().renderPrimitiveValue(item.getType(), value);
}
break;
case Random:
value = context.getNamer().injectRandomStringGeneratorCode(value);
break;
case Variable:
value = context.getNamer().localVarReference(Name.anyLower(value));
break;
default:
throw new IllegalArgumentException("Unhandled init value type");
}
simpleInitValue.initialValue(value);
} else {
simpleInitValue.initialValue(context.getTypeTable().getSnippetZeroValueAndSaveNicknameFor(item.getType()));
simpleInitValue.isRepeated(item.getType().isRepeated());
if (isRequired(item.getFieldConfig(), context)) {
String name = getVariableName(context, item);
comment = String.format(UNINITIALIZED_REQUIRED_FIELD_COMMENT, name);
}
}
initValue = simpleInitValue.build();
}
surfaceLine.initValue(initValue);
surfaceLine.needsLeadingNewline(!isFirstItem);
if (generateStandardComments) {
surfaceLine.doc(context.getNamer().getDocLines(comment));
} else {
surfaceLine.doc(ImmutableList.of());
}
surfaceLine.descriptions(context.getNamer().getWrappedDocLines(item.getDescription(), false));
}
use of com.google.api.codegen.metacode.InitValueConfig in project toolkit by googleapis.
the class InitCodeTransformer method createCollectionMap.
/**
* A utility method which creates the InitValueConfig map that contains the collection config
* data.
*/
public static ImmutableMap<String, InitValueConfig> createCollectionMap(MethodContext context) {
ImmutableMap.Builder<String, InitValueConfig> mapBuilder = ImmutableMap.builder();
Map<String, String> fieldNamePatterns = context.getFieldResourceEntityMap();
for (Map.Entry<String, String> fieldNamePattern : fieldNamePatterns.entrySet()) {
SingleResourceNameConfig resourceNameConfig = context.getSingleResourceNameConfig(fieldNamePattern.getValue());
String apiWrapperClassName = context.getNamer().getApiWrapperClassName(context.getInterfaceConfig());
InitValueConfig initValueConfig = InitValueConfig.create(apiWrapperClassName, resourceNameConfig);
mapBuilder.put(fieldNamePattern.getKey(), initValueConfig);
}
return mapBuilder.build();
}
Aggregations