use of com.google.api.codegen.discovery.Schema in project toolkit by googleapis.
the class JavaDiscoGapicRequestToViewTransformer method generateRequestClass.
private StaticLangApiMessageView generateRequestClass(SchemaTransformationContext context, MethodModel method, RequestObjectParamView resourceNameView) {
StaticLangApiMessageView.Builder requestView = StaticLangApiMessageView.newBuilder();
SymbolTable symbolTable = SymbolTable.fromSeed(reservedKeywords);
String requestClassId = context.getNamer().privateFieldName(DiscoGapicParser.getRequestName(((DiscoveryMethodModel) method).getDiscoMethod()));
String requestName = nameFormatter.privateFieldName(Name.anyCamel(symbolTable.getNewSymbol(requestClassId)));
boolean hasRequiredProperties = false;
requestView.name(requestName);
requestView.description(method.getDescription());
String requestTypeName = nameFormatter.publicClassName(Name.anyCamel(requestClassId));
requestView.typeName(requestTypeName);
requestView.innerTypeName(requestTypeName);
List<StaticLangApiMessageView> properties = new LinkedList<>();
// Add the standard query parameters.
for (String param : STANDARD_QUERY_PARAMS.keySet()) {
if (method.getInputField(param) != null) {
continue;
}
StaticLangApiMessageView.Builder paramView = StaticLangApiMessageView.newBuilder();
paramView.description(STANDARD_QUERY_PARAMS.get(param));
paramView.name(symbolTable.getNewSymbol(param));
paramView.typeName("String");
paramView.innerTypeName("String");
paramView.isRequired(false);
paramView.canRepeat(false);
paramView.fieldGetFunction(context.getNamer().getFieldGetFunctionName(DiscoGapicParser.stringToName(param), SurfaceNamer.MapType.NOT_MAP, SurfaceNamer.Cardinality.NOT_REPEATED));
paramView.fieldSetFunction(context.getDiscoGapicNamer().getResourceSetterName(param, SurfaceNamer.Cardinality.NOT_REPEATED, context.getNamer()));
paramView.properties(Collections.emptyList());
paramView.isRequestMessage(false);
paramView.hasRequiredProperties(false);
properties.add(paramView.build());
}
for (FieldModel entry : method.getInputFields()) {
if (entry.mayBeInResourceName()) {
hasRequiredProperties = true;
continue;
}
String parameterName = entry.getNameAsParameter();
properties.add(schemaToParamView(context, entry, parameterName, symbolTable, EscapeName.ESCAPE_NAME));
if (entry.isRequired()) {
hasRequiredProperties = true;
}
}
StaticLangApiMessageView.Builder paramView = StaticLangApiMessageView.newBuilder();
Method discoMethod = ((DiscoveryMethodModel) method).getDiscoMethod();
String resourceName = DiscoGapicParser.getResourceIdentifier(discoMethod.path()).toLowerCamel();
StringBuilder description = new StringBuilder(discoMethod.parameters().get(resourceName).description());
description.append(String.format("\nIt must have the format `%s`. ", discoMethod.path()));
description.append(String.format("\\`{%s}\\` must start with a letter,\n", resourceName));
description.append("and contain only letters (\\`[A-Za-z]\\`), numbers (\\`[0-9]\\`), dashes (\\`-\\`),\n" + " * underscores (\\`_\\`), periods (\\`.\\`), tildes (\\`~\\`), plus (\\`+\\`) or percent\n" + " * signs (\\`%\\`). It must be between 3 and 255 characters in length, and it\n" + " * must not start with \\`\"goog\"\\`.");
paramView.description(description.toString());
paramView.name(symbolTable.getNewSymbol(resourceNameView.name()));
paramView.typeName("String");
paramView.innerTypeName("String");
paramView.isRequired(true);
paramView.canRepeat(false);
paramView.fieldGetFunction(resourceNameView.getCallName());
paramView.fieldSetFunction(resourceNameView.setCallName());
paramView.properties(new LinkedList<>());
paramView.isRequestMessage(false);
paramView.hasRequiredProperties(false);
properties.add(paramView.build());
Collections.sort(properties);
requestView.canRepeat(false);
requestView.isRequired(true);
requestView.properties(properties);
requestView.hasRequiredProperties(hasRequiredProperties);
requestView.isRequestMessage(true);
requestView.pathAsResourceName(resourceNameView);
Schema requestBodyDef = ((DiscoveryMethodModel) method).getDiscoMethod().request();
if (requestBodyDef != null && !Strings.isNullOrEmpty(requestBodyDef.reference())) {
FieldModel requestBody = DiscoveryField.create(requestBodyDef, context.getDocContext().getApiModel());
requestView.requestBodyType(schemaToParamView(context, requestBody, DiscoGapicParser.getSchemaNameAsParameter(requestBodyDef).toLowerCamel(), symbolTable, EscapeName.NO_ESCAPE_NAME));
}
return requestView.build();
}
use of com.google.api.codegen.discovery.Schema in project toolkit by googleapis.
the class JavaDiscoGapicResourceNameToViewTransformer method generateResourceNameClass.
private StaticLangApiResourceNameView generateResourceNameClass(SchemaTransformationContext context, Method method, SingleResourceNameConfig nameConfig) {
StaticLangApiResourceNameView.Builder resourceNameView = StaticLangApiResourceNameView.newBuilder();
SymbolTable symbolTable = SymbolTable.fromSeed(reservedKeywords);
String resourceName = nameFormatter.localVarName(DiscoGapicParser.getResourceNameName(nameConfig));
resourceNameView.name(resourceName);
resourceNameView.typeName(nameFormatter.publicClassName(Name.anyCamel(resourceName)));
resourceNameView.pathTemplate(nameConfig.getNamePattern());
List<StaticLangMemberView> properties = new LinkedList<>();
for (Map.Entry<String, Schema> entry : method.parameters().entrySet()) {
if (entry.getValue().isPathParam()) {
Schema param = entry.getValue();
properties.add(schemaToParamView(context, param, symbolTable));
}
}
Collections.sort(properties);
resourceNameView.pathParams(properties);
return resourceNameView.build();
}
use of com.google.api.codegen.discovery.Schema in project toolkit by googleapis.
the class PageStreamingConfig method createPageStreaming.
/**
* Creates an instance of PageStreamingConfig based on Discovery Doc, linking it up with the
* provided method. On errors, null will be returned, and diagnostics are reported to the diag
* collector.
*
* @param method Method descriptor for the method to create config for.
*/
@Nullable
static // TODO(andrealin): Merge this function into the createPageStreaming(... Method protoMethod) function.
PageStreamingConfig createPageStreaming(DiscoApiModel apiModel, com.google.api.codegen.discovery.Method method) {
Schema requestTokenField = method.parameters().get(PARAMETER_PAGE_TOKEN);
DiagCollector diagCollector = apiModel.getDiagCollector();
if (requestTokenField == null) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Request field missing for page streaming: method = %s, message type = %s, field = %s", method.id(), method.id(), PARAMETER_PAGE_TOKEN));
}
Schema pageSizeField = method.parameters().get(PARAMETER_MAX_RESULTS);
if (pageSizeField != null) {
pageSizeField = method.parameters().get(PARAMETER_MAX_RESULTS);
if (pageSizeField == null) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Request field missing for page streaming: method = %s, message type = %s, field = %s", method.id(), method.id(), PARAMETER_MAX_RESULTS));
}
}
Schema responseTokenField = null;
Schema responseSchema = method.response().dereference();
if (responseSchema.hasProperty(PARAMETER_NEXT_PAGE_TOKEN)) {
responseTokenField = responseSchema.properties().get(PARAMETER_NEXT_PAGE_TOKEN);
}
if (responseTokenField == null) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Response field missing for page streaming: method = %s, message type = %s, field = %s", method.id(), method.id(), PARAMETER_NEXT_PAGE_TOKEN));
}
Schema responseField = method.response().dereference();
ImmutableList<FieldModel> resourcesFieldPath = ImmutableList.copyOf(getResourcesGetterPath(responseField, apiModel));
FieldConfig resourcesFieldConfig;
if (resourcesFieldPath.isEmpty()) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Resources field missing for page streaming: method = %s, message type = %s, response field = %s", method.id(), method.id(), method.response() == null ? "null" : method.response().toString()));
resourcesFieldConfig = null;
} else {
FieldModel resourcesField = resourcesFieldPath.get(resourcesFieldPath.size() - 1);
resourcesFieldConfig = FieldConfig.createFieldConfig(resourcesField, ImmutableList.copyOf(resourcesFieldPath));
}
if (requestTokenField == null || responseTokenField == null || resourcesFieldConfig == null) {
return null;
}
return new AutoValue_PageStreamingConfig(DiscoveryField.create(requestTokenField, apiModel), DiscoveryField.create(pageSizeField, apiModel), DiscoveryField.create(responseTokenField, apiModel), resourcesFieldConfig);
}
use of com.google.api.codegen.discovery.Schema in project toolkit by googleapis.
the class DiscoveryMethodTransformer method generatePageStreamingResponse.
/**
* Returns a non-null PageStreamingResponseView iff the method has a response object that contains
* a nextPageToken child property.
*/
@Nullable
@Override
public PageStreamingResponseView generatePageStreamingResponse(MethodModel methodModel) {
DiscoveryMethodModel method = (DiscoveryMethodModel) methodModel;
String resourcesField = null;
boolean hasNextPageToken = false;
for (DiscoveryField field : method.getOutputFields()) {
String fieldName = field.getSimpleName();
if (!fieldName.equals(pagingParameters.getNameForNextPageToken())) {
for (Schema property : field.getDiscoveryField().properties().values()) {
if (property.getIdentifier().equals(pagingParameters.getNameForNextPageToken())) {
hasNextPageToken = true;
resourcesField = Name.anyCamel(fieldName).toUpperCamel();
break;
}
}
}
}
if (resourcesField == null || !hasNextPageToken) {
return null;
}
return PageStreamingResponseView.newBuilder().tokenField(pagingParameters.getNameForNextPageToken()).resourcesField(resourcesField).build();
}
use of com.google.api.codegen.discovery.Schema in project toolkit by googleapis.
the class JavaSchemaTypeNameConverter method getTypeNameForElementType.
/**
* Returns the Java representation of a type, without cardinality. If the type is a Java
* primitive, basicTypeName returns it in unboxed form.
*
* @param schema The Schema to generate a TypeName from.
* <p>This method will be recursively called on the given schema's children.
*/
private TypeName getTypeNameForElementType(Schema schema, BoxingBehavior boxingBehavior) {
if (schema == null) {
return new TypeName("java.lang.Void", "Void");
}
String primitiveTypeName = getPrimitiveTypeName(schema);
if (primitiveTypeName != null) {
if (primitiveTypeName.contains(".")) {
// because those types are already boxed.
return typeNameConverter.getTypeName(primitiveTypeName);
} else {
switch(boxingBehavior) {
case BOX_PRIMITIVES:
return new TypeName(JavaTypeTable.getBoxedTypeName(primitiveTypeName));
case NO_BOX_PRIMITIVES:
return new TypeName(primitiveTypeName);
default:
throw new IllegalArgumentException(String.format("Unhandled boxing behavior: %s", boxingBehavior.name()));
}
}
}
if (schema.type().equals(Type.ARRAY)) {
String packageName = implicitPackageName;
Schema element = schema.items();
if (!Strings.isNullOrEmpty(element.reference())) {
String shortName = element.reference() != null ? element.reference() : element.getIdentifier();
String longName = packageName + "." + shortName;
return new TypeName(longName, shortName);
} else {
return getTypeName(schema.items(), BoxingBehavior.BOX_PRIMITIVES);
}
} else {
String packageName = !implicitPackageName.isEmpty() ? implicitPackageName : DEFAULT_JAVA_PACKAGE_PREFIX;
String shortName = "";
if (!schema.id().isEmpty()) {
shortName = schema.id();
} else if (!schema.reference().isEmpty()) {
shortName = schema.reference();
} else if (schema.additionalProperties() != null && !schema.additionalProperties().reference().isEmpty()) {
shortName = schema.additionalProperties().reference();
} else {
// This schema has a parent Schema.
shortName = nameFormatter.publicClassName(Name.anyCamel(schema.key()));
}
String longName = packageName + "." + shortName;
return new TypeName(longName, shortName);
}
}
Aggregations