use of com.google.api.codegen.config.ResourceNameOneofConfig in project toolkit by googleapis.
the class PathTemplateTransformer method getSingleResourceNameConfigsUsedByInterface.
private static List<SingleResourceNameConfig> getSingleResourceNameConfigsUsedByInterface(InterfaceContext context) {
TreeMap<String, SingleResourceNameConfig> resources = new TreeMap<>();
for (ResourceNameConfig resourceNameConfig : getResourceNameConfigsUsedByInterface(context)) {
if (resourceNameConfig.getResourceNameType() == ResourceNameType.SINGLE) {
resources.put(resourceNameConfig.getEntityId(), (SingleResourceNameConfig) resourceNameConfig);
}
if (resourceNameConfig.getResourceNameType() == ResourceNameType.ONEOF) {
// Add SingleResourceNameConfigs derived from gapic config v1 and v2
ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) resourceNameConfig;
for (SingleResourceNameConfig resource : oneofConfig.getSingleResourceNameConfigs()) {
resources.put(resource.getEntityId(), resource);
}
// those derived from patterns will win
if (context.getFeatureConfig().enableStringFormatFunctionsForOneofs()) {
for (SingleResourceNameConfig resource : oneofConfig.getPatternsAsSingleResourceNameConfigs()) {
resources.put(resource.getEntityId(), resource);
}
}
// Add the SingleResourceNameConfig derived from the first pattern last as if it is a
// single-pattern resource name for backward-compatibility.
// We need this for all dynamic languages.
Optional<SingleResourceNameConfig> firstPattern = oneofConfig.getFirstPatternAsSingleResourceNameConfig();
if (firstPattern.isPresent()) {
resources.put(firstPattern.get().getEntityId(), firstPattern.get());
}
}
}
return ImmutableList.copyOf(resources.values());
}
use of com.google.api.codegen.config.ResourceNameOneofConfig 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;
case ANY:
// ignore AnyResourceNameConfigs
break;
default:
throw new IllegalStateException("Unexpected resource-name type.");
}
index += 1;
}
return resourceNames;
}
use of com.google.api.codegen.config.ResourceNameOneofConfig in project toolkit by googleapis.
the class PathTemplateTransformer method addResourceNameOneofConfigsUsedByInterface.
private static void addResourceNameOneofConfigsUsedByInterface(InterfaceContext context, String fieldNamePattern, Set<String> foundSet, List<ResourceNameConfig> resourceNameConfigs) {
ResourceNameConfig resourceNameConfig = context.getProductConfig().getResourceNameConfigs().get(fieldNamePattern);
if (resourceNameConfig != null && resourceNameConfig.getResourceNameType() == ResourceNameType.ONEOF) {
ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) resourceNameConfig;
if (!foundSet.contains(resourceNameConfig.getEntityId())) {
resourceNameConfigs.add(resourceNameConfig);
foundSet.add(resourceNameConfig.getEntityId());
}
}
}
use of com.google.api.codegen.config.ResourceNameOneofConfig in project toolkit by googleapis.
the class InitCodeTransformer method createFormattedInitValueView.
private InitValueView createFormattedInitValueView(MethodContext context, FieldConfig fieldConfig, InitCodeNode item, InitValueConfig initValueConfig) {
FormattedInitValueView.Builder formattedInitValue = FormattedInitValueView.newBuilder();
formattedInitValue.apiVariableName(context.getNamer().getApiWrapperVariableName(context.getInterfaceConfig()));
formattedInitValue.apiWrapperName(context.getNamer().getApiWrapperClassName(context.getInterfaceConfig()));
formattedInitValue.fullyQualifiedApiWrapperName(context.getNamer().getFullyQualifiedApiWrapperClassName(context.getInterfaceConfig()));
// Use the single resource name config that matches binding values
// specified in sample config
SingleResourceNameConfig singleResourceNameConfig;
if (fieldConfig.getResourceNameType() == ResourceNameType.ONEOF) {
ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) fieldConfig.getResourceNameConfig();
singleResourceNameConfig = getMatchingSingleResourceNameConfig(context, item, oneofConfig);
} else {
singleResourceNameConfig = initValueConfig.getSingleResourceNameConfig();
}
formattedInitValue.formatFunctionName(context.getNamer().getFormatFunctionName(context.getInterfaceConfig(), singleResourceNameConfig));
PathTemplate template = singleResourceNameConfig.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(singleResourceNameConfig.getNameTemplate().vars());
formattedInitValue.formatArgs(getFormatFunctionArgs(context, varList, initValueConfig));
return formattedInitValue.build();
}
use of com.google.api.codegen.config.ResourceNameOneofConfig in project toolkit by googleapis.
the class InitCodeTransformer method createResourceNameOneofInitValueView.
private ResourceNameOneofInitValueView createResourceNameOneofInitValueView(MethodContext context, FieldConfig fieldConfig, InitCodeNode item, boolean convertToString) {
ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) fieldConfig.getResourceNameConfig();
SurfaceNamer namer = context.getNamer();
ImportTypeTable typeTable = context.getTypeTable();
if (context.getFeatureConfig().useStaticCreateMethodForOneofs()) {
ResourceNamePatternConfig pattern = getMatchingResourceNamePattern(item, oneofConfig);
String createMethod = convertToString ? pattern.getFormatMethodName() : pattern.getCreateMethodName();
ImmutableList<String> formatArgs = getFormatFunctionArgs(context, ImmutableList.copyOf(pattern.getBindingVariables()), item.getInitValueConfig());
return ResourceNameOneofInitValueView.newBuilder().resourceOneofTypeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig)).createMethodName(createMethod).formatArgs(formatArgs).build();
} else {
SingleResourceNameConfig singleResourceNameConfig = getMatchingSingleResourceNameConfig(context, item, oneofConfig);
FieldConfig singleResourceNameFieldConfig = fieldConfig.withResourceNameConfig(singleResourceNameConfig);
ResourceNameInitValueView initView = createResourceNameInitValueView(context, singleResourceNameFieldConfig, item).convertToString(convertToString).build();
return ResourceNameOneofInitValueView.newBuilder().resourceOneofTypeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig)).specificResourceNameView(initView).build();
}
}
Aggregations