use of com.google.api.codegen.config.FieldConfig in project toolkit by googleapis.
the class PageStreamingTransformer method generateFactoryClass.
private PagedListResponseFactoryClassView generateFactoryClass(MethodContext context) {
SurfaceNamer namer = context.getNamer();
MethodModel method = context.getMethodModel();
ImportTypeTable typeTable = context.getTypeTable();
PageStreamingConfig pageStreaming = context.getMethodConfig().getPageStreaming();
FieldModel resourceField = pageStreaming.getResourcesField();
FieldConfig resourceFieldConfig = pageStreaming.getResourcesFieldConfig();
PagedListResponseFactoryClassView.Builder factory = PagedListResponseFactoryClassView.newBuilder();
factory.name(namer.getPagedListResponseFactoryConstName(method));
factory.requestTypeName(method.getAndSaveRequestTypeName(typeTable, namer));
factory.responseTypeName(method.getAndSaveResponseTypeName(typeTable, namer));
factory.resourceTypeName(typeTable.getAndSaveNicknameForElementType(resourceField));
factory.pagedListResponseTypeName(namer.getAndSavePagedResponseTypeName(context, resourceFieldConfig));
factory.pageStreamingDescriptorName(namer.getPageStreamingDescriptorConstName(method));
return factory.build();
}
use of com.google.api.codegen.config.FieldConfig in project toolkit by googleapis.
the class PathTemplateTransformer method generateResourceProtos.
public List<ResourceProtoView> generateResourceProtos(GapicInterfaceContext context) {
SurfaceNamer namer = context.getNamer();
ResourceNameMessageConfigs resourceConfigs = context.getProductConfig().getResourceNameMessageConfigs();
ListMultimap<String, FieldModel> fieldsByMessage = resourceConfigs.getFieldsWithResourceNamesByMessage();
Map<String, FieldConfig> fieldConfigMap = context.getProductConfig().getDefaultResourceNameFieldConfigMap();
List<ResourceProtoView> protos = new ArrayList<>();
for (Entry<String, Collection<FieldModel>> entry : fieldsByMessage.asMap().entrySet()) {
String msgName = entry.getKey();
Collection<FieldModel> fields = new ArrayList<>(entry.getValue());
ResourceProtoView.Builder protoBuilder = ResourceProtoView.newBuilder();
protoBuilder.protoClassName(namer.getTypeNameConverter().getTypeName(msgName).getNickname());
List<ResourceProtoFieldView> fieldViews = new ArrayList<>();
for (FieldModel field : fields) {
FieldConfig fieldConfig = fieldConfigMap.get(field.getFullName());
String fieldTypeSimpleName = namer.getResourceTypeName(fieldConfig.getResourceNameConfig());
String fieldTypeName = context.getImportTypeTable().getAndSaveNicknameForTypedResourceName(fieldConfig, fieldTypeSimpleName);
if (field.isRepeated()) {
fieldTypeName = fieldTypeName.replaceFirst("IEnumerable", "ResourceNameList");
}
String fieldDocTypeName = fieldTypeName.replace('<', '{').replace('>', '}');
String fieldElementTypeName = context.getImportTypeTable().getAndSaveNicknameForResourceNameElementType(fieldConfig, fieldTypeSimpleName);
ResourceProtoFieldView fieldView = ResourceProtoFieldView.newBuilder().typeName(fieldTypeName).parseMethodTypeName(namer.getPackageName() + "." + fieldTypeName).docTypeName(fieldDocTypeName).elementTypeName(fieldElementTypeName).isAny(fieldConfig.getResourceNameType() == ResourceNameType.ANY).isRepeated(field.isRepeated()).isOneof(fieldConfig.getResourceNameType() == ResourceNameType.ONEOF).propertyName(namer.getResourceNameFieldGetFunctionName(fieldConfig)).underlyingPropertyName(namer.publicMethodName(Name.from(field.getSimpleName()))).build();
fieldViews.add(fieldView);
}
protoBuilder.fields(fieldViews);
protos.add(protoBuilder.build());
}
Collections.sort(protos, new Comparator<ResourceProtoView>() {
@Override
public int compare(ResourceProtoView a, ResourceProtoView b) {
return a.protoClassName().compareTo(b.protoClassName());
}
});
return protos;
}
use of com.google.api.codegen.config.FieldConfig in project toolkit by googleapis.
the class StaticLangApiMethodTransformer method generateUnpagedListCallableMethod.
public StaticLangApiMethodView generateUnpagedListCallableMethod(MethodContext context) {
MethodModel method = context.getMethodModel();
SurfaceNamer namer = context.getNamer();
StaticLangApiMethodView.Builder methodViewBuilder = StaticLangApiMethodView.newBuilder();
setCommonFields(context, methodViewBuilder);
methodViewBuilder.name(namer.getCallableMethodName(method));
methodViewBuilder.exampleName(namer.getCallableMethodExampleName(method));
setListMethodFields(context, Synchronicity.Sync, methodViewBuilder);
setCallableMethodFields(context, namer.getCallableName(method), methodViewBuilder);
String getResourceListCallName = namer.getFieldGetFunctionName(context.getFeatureConfig(), context.getMethodConfig().getPageStreaming().getResourcesFieldConfig());
String resourceListParseFunction = "";
PageStreamingConfig pageStreaming = context.getMethodConfig().getPageStreaming();
FieldConfig resourceFieldConfig = pageStreaming.getResourcesFieldConfig();
if (context.getFeatureConfig().useResourceNameConverters(resourceFieldConfig)) {
resourceListParseFunction = namer.getResourceTypeParseListMethodName(context.getTypeTable(), resourceFieldConfig);
}
UnpagedListCallableMethodDetailView unpagedListCallableDetails = UnpagedListCallableMethodDetailView.newBuilder().resourceListGetFunction(getResourceListCallName).resourceListParseFunction(resourceListParseFunction).build();
methodViewBuilder.unpagedListCallableMethod(unpagedListCallableDetails);
methodViewBuilder.responseTypeName(context.getMethodModel().getAndSaveResponseTypeName(context.getTypeTable(), context.getNamer()));
return methodViewBuilder.type(ClientMethodType.UnpagedListCallableMethod).build();
}
use of com.google.api.codegen.config.FieldConfig in project toolkit by googleapis.
the class StaticLangApiMethodTransformer method setListMethodFields.
private void setListMethodFields(MethodContext context, Synchronicity synchronicity, StaticLangApiMethodView.Builder methodViewBuilder) {
MethodModel method = context.getMethodModel();
ImportTypeTable typeTable = context.getTypeTable();
SurfaceNamer namer = context.getNamer();
PageStreamingConfig pageStreaming = context.getMethodConfig().getPageStreaming();
String requestTypeName = method.getAndSaveRequestTypeName(context.getTypeTable(), context.getNamer());
String responseTypeName = method.getAndSaveResponseTypeName(context.getTypeTable(), context.getNamer());
FieldConfig resourceFieldConfig = pageStreaming.getResourcesFieldConfig();
FieldModel resourceField = resourceFieldConfig.getField();
String resourceTypeName;
if (context.getFeatureConfig().useResourceNameFormatOption(resourceFieldConfig)) {
resourceTypeName = namer.getAndSaveElementResourceTypeName(typeTable, resourceFieldConfig);
} else {
resourceTypeName = typeTable.getAndSaveNicknameForElementType(resourceField);
}
String iterateMethodName = context.getNamer().getPagedResponseIterateMethod(context.getFeatureConfig(), resourceFieldConfig);
String resourceFieldName = context.getNamer().getFieldName(resourceField);
List<String> resourceFieldGetFunctionNames = resourceField.getPagedResponseResourceMethods(context.getFeatureConfig(), resourceFieldConfig, context.getNamer());
methodViewBuilder.listMethod(ListMethodDetailView.newBuilder().requestTypeName(requestTypeName).responseTypeName(responseTypeName).resourceTypeName(resourceTypeName).iterateMethodName(iterateMethodName).resourceFieldName(resourceFieldName).resourcesFieldGetFunctions(resourceFieldGetFunctionNames).build());
switch(synchronicity) {
case Sync:
methodViewBuilder.responseTypeName(namer.getAndSavePagedResponseTypeName(context, resourceFieldConfig));
break;
case Async:
methodViewBuilder.responseTypeName(namer.getAndSaveAsyncPagedResponseTypeName(context, resourceFieldConfig));
break;
}
}
use of com.google.api.codegen.config.FieldConfig in project toolkit by googleapis.
the class StaticLangApiMethodTransformer method generatePathTemplateChecks.
private List<PathTemplateCheckView> generatePathTemplateChecks(MethodContext context, Iterable<FieldConfig> fieldConfigs) {
List<PathTemplateCheckView> pathTemplateChecks = new ArrayList<>();
if (!context.getFeatureConfig().enableStringFormatFunctions()) {
return pathTemplateChecks;
}
for (FieldConfig fieldConfig : fieldConfigs) {
if (!fieldConfig.useValidation()) {
// Don't generate a path template check if fieldConfig is not configured to use validation.
continue;
}
FieldModel field = fieldConfig.getField();
ImmutableMap<String, String> fieldNamePatterns = context.getMethodConfig().getFieldNamePatterns();
String entityName = fieldNamePatterns.get(field.getSimpleName());
if (entityName != null) {
SingleResourceNameConfig resourceNameConfig = context.getSingleResourceNameConfig(entityName);
if (resourceNameConfig == null) {
String methodName = context.getMethodModel().getSimpleName();
throw new IllegalStateException("No collection config with id '" + entityName + "' required by configuration for method '" + methodName + "'");
}
PathTemplateCheckView.Builder check = PathTemplateCheckView.newBuilder();
check.pathTemplateName(context.getNamer().getPathTemplateName(context.getInterfaceConfig(), resourceNameConfig));
check.paramName(context.getNamer().getVariableName(field));
check.allowEmptyString(shouldAllowEmpty(context, field));
check.validationMessageContext(context.getNamer().getApiMethodName(context.getMethodModel(), context.getMethodConfig().getVisibility()));
pathTemplateChecks.add(check.build());
}
}
return pathTemplateChecks;
}
Aggregations