use of com.google.api.pathtemplate.PathTemplate 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.pathtemplate.PathTemplate in project toolkit by googleapis.
the class SingleResourceNameConfig method createSingleResourceName.
/**
* Creates an instance of SingleResourceNameConfig based on CollectionConfigProto. On errors, null
* will be returned, and diagnostics are reported to the diag collector.
*/
@Nullable
public static SingleResourceNameConfig createSingleResourceName(DiagCollector diagCollector, ConfigProto configProto, CollectionConfigProto collectionConfigProto, ProtoFile file) {
String namePattern = collectionConfigProto.getNamePattern();
PathTemplate nameTemplate;
try {
nameTemplate = PathTemplate.create(namePattern);
} catch (ValidationException e) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, e.getMessage()));
return null;
}
String entityId = collectionConfigProto.getEntityName();
String entityName = entityId;
String language = configProto.getLanguage();
if (language != null) {
for (CollectionLanguageOverridesProto override : collectionConfigProto.getLanguageOverridesList()) {
if (language.equals(override.getLanguage())) {
entityName = override.getEntityName();
}
}
}
return new AutoValue_SingleResourceNameConfig(namePattern, nameTemplate, entityId, entityName, file);
}
Aggregations