use of com.google.api.codegen.config.SingleResourceNameConfig 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.SingleResourceNameConfig in project toolkit by googleapis.
the class PathTemplateTransformer method generatePathTemplateGetterFunctions.
public List<PathTemplateGetterFunctionView> generatePathTemplateGetterFunctions(GapicInterfaceContext context) {
List<PathTemplateGetterFunctionView> functions = new ArrayList<>();
SurfaceNamer namer = context.getNamer();
InterfaceConfig interfaceConfig = context.getInterfaceConfig();
for (SingleResourceNameConfig resourceNameConfig : getSingleResourceNameConfigsUsedByInterface(context)) {
PathTemplateGetterFunctionView.Builder function = PathTemplateGetterFunctionView.newBuilder().name(namer.getPathTemplateNameGetter(interfaceConfig, resourceNameConfig)).resourceName(namer.getPathTemplateResourcePhraseName(resourceNameConfig)).entityName(namer.getEntityName(resourceNameConfig)).pathTemplateName(namer.getPathTemplateName(interfaceConfig, resourceNameConfig)).pattern(resourceNameConfig.getNamePattern());
functions.add(function.build());
}
return functions;
}
use of com.google.api.codegen.config.SingleResourceNameConfig in project toolkit by googleapis.
the class JavaDiscoGapicResourceNameToViewTransformer method transform.
@Override
public List<ViewModel> transform(DiscoApiModel apiModel, GapicProductConfig productConfig) {
List<ViewModel> surfaceRequests = new ArrayList<>();
String packageName = productConfig.getPackageName();
SurfaceNamer surfaceNamer = new JavaSurfaceNamer(packageName, packageName, nameFormatter);
DiscoGapicInterfaceContext context = DiscoGapicInterfaceContext.createWithoutInterface(apiModel, productConfig, createTypeTable(productConfig.getPackageName(), surfaceNamer), surfaceNamer, JavaFeatureConfig.create(productConfig));
// Keep track of which name patterns have been generated to avoid duplicate classes.
Set<String> namePatterns = new HashSet<>();
for (String interfaceName : productConfig.getInterfaceConfigMap().keySet()) {
SchemaTransformationContext requestContext = SchemaTransformationContext.create(interfaceName, context.getSchemaTypeTable(), context);
// Maps a canonical resource name pattern to any method that uses that pattern.
Map<String, Method> namePatternsToMethod = new HashMap<>();
for (MethodConfig methodConfig : productConfig.getInterfaceConfigMap().get(interfaceName).getMethodConfigs()) {
Method method = ((DiscoveryMethodModel) methodConfig.getMethodModel()).getDiscoMethod();
namePatternsToMethod.put(DiscoGapicParser.getCanonicalPath(method.flatPath()), method);
}
for (SingleResourceNameConfig nameConfig : productConfig.getInterfaceConfigMap().get(interfaceName).getSingleResourceNameConfigs()) {
String namePattern = nameConfig.getNamePattern();
if (namePatterns.contains(namePattern)) {
continue;
}
Method method = namePatternsToMethod.get(namePattern);
StaticLangApiResourceNameView resourceNameView = generateResourceNameClass(requestContext, method, nameConfig);
surfaceRequests.add(generateResourceNameFile(requestContext, resourceNameView));
namePatterns.add(nameConfig.getNamePattern());
}
}
Collections.sort(surfaceRequests, new Comparator<ViewModel>() {
@Override
public int compare(ViewModel o1, ViewModel o2) {
return String.CASE_INSENSITIVE_ORDER.compare(o1.outputPath(), o2.outputPath());
}
});
return surfaceRequests;
}
use of com.google.api.codegen.config.SingleResourceNameConfig 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.SingleResourceNameConfig 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