use of com.google.api.codegen.config.SingleResourceNameConfig in project toolkit by googleapis.
the class InitCodeTransformer method createInitValueView.
private InitValueView createInitValueView(MethodContext context, FieldConfig fieldConfig, SurfaceNamer namer, ImportTypeTable typeTable, InitCodeNode item, boolean convertToString) {
SingleResourceNameConfig singleResourceNameConfig;
switch(fieldConfig.getResourceNameType()) {
case ANY:
singleResourceNameConfig = Iterables.get(context.getProductConfig().getSingleResourceNameConfigs(), 0);
InterfaceModel interfaceModel = context.getInterfaceModel();
if (interfaceModel instanceof ProtoInterfaceModel) {
ProtoFile protoFile = ((ProtoInterfaceModel) interfaceModel).getInterface().getFile();
singleResourceNameConfig = singleResourceNameConfig.toBuilder().setAssignedProtoFile(protoFile).build();
}
FieldConfig anyResourceNameFieldConfig = fieldConfig.withResourceNameConfig(singleResourceNameConfig);
return createResourceNameInitValueView(context, anyResourceNameFieldConfig, item).convertToString(convertToString).build();
case FIXED:
throw new UnsupportedOperationException("entity name invalid");
case ONEOF:
return createResourceNameOneofInitValueView(context, fieldConfig, item, convertToString);
case SINGLE:
return createResourceNameInitValueView(context, fieldConfig, item).convertToString(convertToString).build();
case NONE:
// Fall-through
default:
throw new UnsupportedOperationException("unexpected entity name type '" + fieldConfig.getResourceNameType() + "'");
}
}
use of com.google.api.codegen.config.SingleResourceNameConfig in project toolkit by googleapis.
the class InitCodeTransformer method createResourceNameInitValueView.
private ResourceNameInitValueView.Builder createResourceNameInitValueView(MethodContext context, FieldConfig fieldConfig, InitCodeNode item) {
String resourceName = context.getNamer().getAndSaveElementResourceTypeName(context.getTypeTable(), fieldConfig);
SingleResourceNameConfig singleResourceNameConfig = (SingleResourceNameConfig) fieldConfig.getResourceNameConfig();
List<String> varList = Lists.newArrayList(singleResourceNameConfig.getNameTemplate().vars());
return ResourceNameInitValueView.newBuilder().resourceTypeName(resourceName).formatArgs(getFormatFunctionArgs(context, varList, item.getInitValueConfig()));
}
use of com.google.api.codegen.config.SingleResourceNameConfig in project toolkit by googleapis.
the class PathTemplateTransformer method getResourceNameConfigsUsedByInterface.
private static List<ResourceNameConfig> getResourceNameConfigsUsedByInterface(InterfaceContext context) {
InterfaceConfig interfaceConfig = context.getInterfaceConfig();
Set<String> foundSet = new HashSet<>();
List<ResourceNameConfig> resourceNameConfigs = new ArrayList<>();
for (SingleResourceNameConfig config : interfaceConfig.getSingleResourceNameConfigs()) {
resourceNameConfigs.add(config);
foundSet.add(config.getEntityId());
}
for (MethodConfig methodConfig : interfaceConfig.getMethodConfigs()) {
MethodContext methodContext = context.asRequestMethodContext(methodConfig.getMethodModel());
for (String fieldNamePattern : methodConfig.getFieldNamePatterns().values()) {
addSingleResourceNameConfigsUsedByInterface(methodContext, fieldNamePattern, foundSet, resourceNameConfigs);
addResourceNameOneofConfigsUsedByInterface(context, fieldNamePattern, foundSet, resourceNameConfigs);
}
}
return resourceNameConfigs;
}
use of com.google.api.codegen.config.SingleResourceNameConfig in project toolkit by googleapis.
the class PathTemplateTransformer method generateParseResourceFunctions.
public List<ParseResourceFunctionView> generateParseResourceFunctions(InterfaceContext context) {
List<ParseResourceFunctionView> functions = new ArrayList<>();
if (!context.getFeatureConfig().enableStringFormatFunctions()) {
return functions;
}
SurfaceNamer namer = context.getNamer();
InterfaceConfig interfaceConfig = context.getInterfaceConfig();
for (SingleResourceNameConfig resourceNameConfig : getSingleResourceNameConfigsUsedByInterface(context)) {
for (String var : resourceNameConfig.getNameTemplate().vars()) {
ParseResourceFunctionView.Builder function = ParseResourceFunctionView.newBuilder().entityName(resourceNameConfig.getEntityName().toLowerUnderscore()).name(namer.getParseFunctionName(var, resourceNameConfig)).pathTemplateName(namer.getPathTemplateName(interfaceConfig, resourceNameConfig)).pathTemplateGetterName(namer.getPathTemplateNameGetter(interfaceConfig, resourceNameConfig)).entityNameTypeName(namer.getResourceTypeName(resourceNameConfig)).entityNameParamName(namer.getEntityNameParamName(resourceNameConfig)).isResourceNameDeprecated(resourceNameConfig.getDeprecated()).outputResourceId(var);
functions.add(function.build());
}
}
return functions;
}
use of com.google.api.codegen.config.SingleResourceNameConfig 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());
}
Aggregations