use of com.google.api.codegen.config.ResourceNameConfig 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(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 {
SingleResourceNameConfig singleResourceNameConfig;
switch(fieldConfig.getResourceNameType()) {
case ANY:
// TODO(michaelbausor): handle case where there are no other resource names at all...
singleResourceNameConfig = Iterables.get(context.getProductConfig().getSingleResourceNameConfigs(), 0);
FieldConfig anyResourceNameFieldConfig = fieldConfig.withResourceNameConfig(singleResourceNameConfig);
initValue = createResourceNameInitValueView(context, anyResourceNameFieldConfig, item).build();
break;
case FIXED:
throw new UnsupportedOperationException("entity name invalid");
case ONEOF:
ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) fieldConfig.getResourceNameConfig();
singleResourceNameConfig = Iterables.get(oneofConfig.getSingleResourceNameConfigs(), 0);
FieldConfig singleResourceNameFieldConfig = fieldConfig.withResourceNameConfig(singleResourceNameConfig);
ResourceNameInitValueView initView = createResourceNameInitValueView(context, singleResourceNameFieldConfig, item).build();
initValue = ResourceNameOneofInitValueView.newBuilder().resourceOneofTypeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig)).specificResourceNameView(initView).build();
break;
case SINGLE:
initValue = createResourceNameInitValueView(context, fieldConfig, item).build();
break;
case NONE:
// Fall-through
default:
throw new UnsupportedOperationException("unexpected entity name type '" + fieldConfig.getResourceNameType() + "'");
}
}
} else if (initValueConfig.hasFormattingConfig() && !item.getType().isRepeated()) {
if (context.getFeatureConfig().enableStringFormatFunctions() || fieldConfig.getResourceNameConfig() == null) {
FormattedInitValueView.Builder formattedInitValue = FormattedInitValueView.newBuilder();
formattedInitValue.apiWrapperName(context.getNamer().getApiWrapperClassName(context.getInterfaceConfig()));
formattedInitValue.fullyQualifiedApiWrapperName(context.getNamer().getFullyQualifiedApiWrapperClassName(context.getInterfaceConfig()));
formattedInitValue.formatFunctionName(context.getNamer().getFormatFunctionName(context.getInterfaceConfig(), initValueConfig.getSingleResourceNameConfig()));
PathTemplate template = initValueConfig.getSingleResourceNameConfig().getNameTemplate();
String[] encodeArgs = new String[template.vars().size()];
Arrays.fill(encodeArgs, FORMAT_SPEC_PLACEHOLDER);
// Format spec usually contains reserved character, escaped by path template.
// So we first encode using FORMAT_SPEC_PLACEHOLDER, then do straight string replace.
formattedInitValue.formatSpec(template.withoutVars().encode(encodeArgs).replace(FORMAT_SPEC_PLACEHOLDER, context.getNamer().formatSpec()));
List<String> varList = Lists.newArrayList(initValueConfig.getSingleResourceNameConfig().getNameTemplate().vars());
formattedInitValue.formatArgs(getFormatFunctionArgs(context, varList, initValueConfig));
initValue = formattedInitValue.build();
} else {
initValue = createResourceNameInitValueView(context, fieldConfig, item).convertToString(true).build();
}
} 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 (generateUserFacingComments) {
surfaceLine.doc(context.getNamer().getDocLines(comment));
} else {
surfaceLine.doc(ImmutableList.of());
}
}
use of com.google.api.codegen.config.ResourceNameConfig in project toolkit by googleapis.
the class PathTemplateTransformer method generateResourceNames.
List<ResourceNameView> generateResourceNames(InterfaceContext context, Iterable<ResourceNameConfig> configs) {
List<ResourceNameView> resourceNames = new ArrayList<>();
int index = 1;
for (ResourceNameConfig config : configs) {
switch(config.getResourceNameType()) {
case SINGLE:
resourceNames.add(generateResourceNameSingle(context, index, (SingleResourceNameConfig) config));
break;
case ONEOF:
resourceNames.add(generateResourceNameOneof(context, index, (ResourceNameOneofConfig) config));
break;
case FIXED:
resourceNames.add(generateResourceNameFixed(context, index, (FixedResourceNameConfig) config));
break;
default:
throw new IllegalStateException("Unexpected resource-name type.");
}
index += 1;
}
return resourceNames;
}
Aggregations