use of com.google.api.codegen.config.FieldModel 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;
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class StaticLangResourceObjectTransformer method generateRequestObjectParam.
public RequestObjectParamView generateRequestObjectParam(MethodContext context, FieldConfig fieldConfig) {
SurfaceNamer namer = context.getNamer();
FeatureConfig featureConfig = context.getFeatureConfig();
ImportTypeTable typeTable = context.getTypeTable();
FieldModel field = fieldConfig.getField();
Iterable<FieldModel> requiredFields = context.getMethodConfig().getRequiredFields();
boolean isRequired = false;
for (FieldModel f : requiredFields) {
if (f.getSimpleName().equals(field.getSimpleName())) {
isRequired = true;
break;
}
}
String typeName = namer.getNotImplementedString("StaticLangApiMethodTransformer.generateRequestObjectParam - typeName");
String elementTypeName = namer.getNotImplementedString("StaticLangApiMethodTransformer.generateRequestObjectParam - elementTypeName");
if (context.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
if (namer.shouldImportRequestObjectParamType(field)) {
typeName = namer.getAndSaveResourceTypeName(typeTable, fieldConfig);
}
if (namer.shouldImportRequestObjectParamElementType(field)) {
// Use makeOptional to remove repeated property from type
elementTypeName = namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig);
}
} else {
if (namer.shouldImportRequestObjectParamType(field)) {
typeName = typeTable.getAndSaveNicknameFor(field);
if (!isRequired) {
typeName = namer.makePrimitiveTypeNullable(typeName, field);
}
}
if (namer.shouldImportRequestObjectParamElementType(field)) {
elementTypeName = typeTable.getAndSaveNicknameForElementType(field);
}
}
String setCallName = namer.getFieldSetFunctionName(featureConfig, fieldConfig);
String addCallName = namer.getFieldAddFunctionName(field);
String getCallName = namer.getFieldGetFunctionName(field);
String transformParamFunctionName = null;
String formatMethodName = null;
if (context.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
if (fieldConfig.requiresParamTransformation() && !fieldConfig.requiresParamTransformationFromAny() && !featureConfig.useInheritanceForOneofs()) {
transformParamFunctionName = namer.getResourceOneofCreateMethod(typeTable, fieldConfig);
}
if (context.getFeatureConfig().useResourceNameConverters(fieldConfig)) {
if (field.isRepeated()) {
// TODO support repeated one-ofs (in Java: Any* classes)
transformParamFunctionName = namer.getResourceTypeFormatListMethodName(context.getTypeTable(), fieldConfig);
} else {
formatMethodName = namer.getResourceNameFormatMethodName();
}
}
}
RequestObjectParamView.Builder param = RequestObjectParamView.newBuilder();
param.name(namer.getVariableName(field));
param.keyName(namer.getFieldKey(field));
param.nameAsMethodName(namer.getFieldGetFunctionName(featureConfig, fieldConfig));
param.typeName(typeName);
param.elementTypeName(elementTypeName);
param.setCallName(setCallName);
param.addCallName(addCallName);
param.getCallName(getCallName);
param.transformParamFunctionName(transformParamFunctionName);
param.formatMethodName(formatMethodName);
param.isMap(field.isMap());
param.isArray(!field.isMap() && field.isRepeated());
param.isPrimitive(namer.isPrimitive(field));
param.isOptional(!isRequired);
if (!isRequired) {
param.optionalDefault(namer.getOptionalFieldDefaultValue(fieldConfig, context));
}
List<StaticLangMemberView> fieldViews = new ArrayList<>();
for (FieldModel child : context.getMethodModel().getResourceNameInputFields()) {
StaticLangMemberView.Builder staticMember = StaticLangMemberView.newBuilder();
staticMember.fieldGetFunction(namer.getFieldGetFunctionName(child));
staticMember.fieldSetFunction(namer.getFieldSetFunctionName(child));
staticMember.name(child.getNameAsParameter());
staticMember.typeName(child.getTypeFullName());
fieldViews.add(staticMember.build());
}
Collections.sort(fieldViews);
param.fieldCopyMethods(fieldViews);
return param.build();
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class SurfaceNamer method getResourceNameFieldSetFunctionName.
/**
* The function name to set a field that is a resource name class.
*/
public String getResourceNameFieldSetFunctionName(FieldConfig fieldConfig) {
FieldModel type = fieldConfig.getField();
Name identifier = fieldConfig.getField().getNameAsParameterName();
Name resourceName = getResourceTypeNameObject(fieldConfig.getResourceNameConfig());
if (type.isMap()) {
return getNotImplementedString("SurfaceNamer.getResourceNameFieldSetFunctionName:map-type");
} else if (type.isRepeated()) {
return publicMethodName(Name.from("add", "all").join(identifier).join("with").join(resourceName).join("list"));
} else {
return publicMethodName(Name.from("set").join(identifier).join("with").join(resourceName));
}
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class CSharpSurfaceNamer method getOptionalFieldDefaultValue.
@Override
public String getOptionalFieldDefaultValue(FieldConfig fieldConfig, MethodContext context) {
// Need to provide defaults for primitives, enums, strings, and repeated (including maps)
FieldModel type = fieldConfig.getField();
if (context.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
if (type.isRepeated()) {
TypeName elementTypeName = new TypeName(getResourceTypeNameObject(fieldConfig.getResourceNameConfig()).toUpperCamel());
TypeNameConverter typeNameConverter = getTypeNameConverter();
TypeName enumerableTypeName = typeNameConverter.getTypeName("System.Linq.Enumerable");
TypeName emptyTypeName = new TypeName(enumerableTypeName.getFullName(), enumerableTypeName.getNickname(), "%s.Empty<%i>", elementTypeName);
return TypedValue.create(emptyTypeName, "%s()").getValueAndSaveTypeNicknameIn((CSharpTypeTable) typeNameConverter);
} else {
return null;
}
} else {
if (type.isPrimitive() || type.isEnum() || type.isRepeated()) {
return context.getTypeTable().getImplZeroValueAndSaveNicknameFor(type);
} else {
return null;
}
}
}
use of com.google.api.codegen.config.FieldModel in project toolkit by googleapis.
the class CSharpSurfaceNamer method getReturnDocLines.
@Override
public List<String> getReturnDocLines(TransformationContext context, MethodContext methodContext, Synchronicity synchronicity) {
MethodConfig methodConfig = methodContext.getMethodConfig();
if (methodConfig.isPageStreaming()) {
FieldModel resourceType = methodConfig.getPageStreaming().getResourcesField();
String resourceTypeName = context.getImportTypeTable().getAndSaveNicknameForElementType(resourceType);
switch(synchronicity) {
case Sync:
return ImmutableList.of("A pageable sequence of <see cref=\"" + resourceTypeName + "\"/> resources.");
case Async:
return ImmutableList.of("A pageable asynchronous sequence of <see cref=\"" + resourceTypeName + "\"/> resources.");
}
} else if (methodConfig.isGrpcStreaming()) {
switch(methodConfig.getGrpcStreamingType()) {
case ServerStreaming:
return ImmutableList.of("The server stream.");
case BidiStreaming:
return ImmutableList.of("The client-server stream.");
default:
throw new IllegalStateException("Invalid streaming: " + methodConfig.getGrpcStreamingType());
}
} else {
boolean hasReturn = !methodConfig.getMethodModel().isOutputTypeEmpty();
switch(synchronicity) {
case Sync:
return hasReturn ? ImmutableList.of("The RPC response.") : ImmutableList.of();
case Async:
return ImmutableList.of(hasReturn ? "A Task containing the RPC response." : "A Task that completes when the RPC has completed.");
}
}
throw new IllegalStateException("Invalid Synchronicity: " + synchronicity);
}
Aggregations