Search in sources :

Example 1 with DiscoveryMethodModel

use of com.google.api.codegen.config.DiscoveryMethodModel 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();
}
Also used : Schema(com.google.api.codegen.discovery.Schema) SymbolTable(com.google.api.codegen.util.SymbolTable) Method(com.google.api.codegen.discovery.Method) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) LinkedList(java.util.LinkedList) StaticLangApiMessageView(com.google.api.codegen.viewmodel.StaticLangApiMessageView) FieldModel(com.google.api.codegen.config.FieldModel)

Example 2 with DiscoveryMethodModel

use of com.google.api.codegen.config.DiscoveryMethodModel in project toolkit by googleapis.

the class ApiCallableTransformer method generateHttpFields.

private HttpMethodView generateHttpFields(MethodContext context) {
    if (context.getProductConfig().getTransportProtocol().equals(TransportProtocol.HTTP)) {
        Method method = ((DiscoveryMethodModel) context.getMethodModel()).getDiscoMethod();
        HttpMethodView.Builder httpMethodView = HttpMethodView.newBuilder();
        httpMethodView.fullMethodName(method.id());
        httpMethodView.httpMethod(method.httpMethod());
        List<String> pathParams = new ArrayList<>(method.pathParams().keySet());
        List<String> queryParams = new ArrayList<>(method.queryParams().keySet());
        Collections.sort(pathParams);
        Collections.sort(queryParams);
        httpMethodView.pathParams(pathParams);
        httpMethodView.queryParams(queryParams);
        httpMethodView.pathTemplate(method.path());
        // TODO(andrealin): handle multiple resource names.
        DiscoGapicInterfaceConfig interfaceConfig = (DiscoGapicInterfaceConfig) context.getSurfaceInterfaceContext().getInterfaceConfig();
        SingleResourceNameConfig nameConfig = interfaceConfig.methodToResourceNameMap().get(context.getMethodConfig());
        httpMethodView.resourceNameTypeName(context.getNamer().publicClassName(DiscoGapicParser.getResourceNameName(nameConfig)));
        // Find the field with the resource name config.
        for (FieldConfig fieldConfig : context.getMethodConfig().getRequiredFieldConfigs()) {
            if (fieldConfig.getResourceNameConfig() != null && fieldConfig.getResourceNameConfig().equals(nameConfig)) {
                httpMethodView.resourceNameFieldName(context.getNamer().privateFieldName(Name.anyCamel(fieldConfig.getField().getNameAsParameter())));
            }
        }
        return httpMethodView.build();
    } else {
        return null;
    }
}
Also used : FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList) HttpMethodView(com.google.api.codegen.viewmodel.HttpMethodView) DiscoGapicInterfaceConfig(com.google.api.codegen.config.DiscoGapicInterfaceConfig) Method(com.google.api.codegen.discovery.Method) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 3 with DiscoveryMethodModel

use of com.google.api.codegen.config.DiscoveryMethodModel 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();
}
Also used : DiscoveryField(com.google.api.codegen.config.DiscoveryField) Schema(com.google.api.codegen.discovery.Schema) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) Nullable(javax.annotation.Nullable)

Example 4 with DiscoveryMethodModel

use of com.google.api.codegen.config.DiscoveryMethodModel 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.newBuilder().enableStringFormatFunctions(true).build());
    // 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), 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;
}
Also used : SchemaTransformationContext(com.google.api.codegen.discogapic.SchemaTransformationContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) Method(com.google.api.codegen.discovery.Method) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) StaticLangApiResourceNameView(com.google.api.codegen.viewmodel.StaticLangApiResourceNameView) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) MethodConfig(com.google.api.codegen.config.MethodConfig) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) DiscoGapicInterfaceContext(com.google.api.codegen.transformer.DiscoGapicInterfaceContext) HashSet(java.util.HashSet)

Aggregations

DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)4 Method (com.google.api.codegen.discovery.Method)3 SingleResourceNameConfig (com.google.api.codegen.config.SingleResourceNameConfig)2 Schema (com.google.api.codegen.discovery.Schema)2 ArrayList (java.util.ArrayList)2 DiscoGapicInterfaceConfig (com.google.api.codegen.config.DiscoGapicInterfaceConfig)1 DiscoveryField (com.google.api.codegen.config.DiscoveryField)1 FieldConfig (com.google.api.codegen.config.FieldConfig)1 FieldModel (com.google.api.codegen.config.FieldModel)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 SchemaTransformationContext (com.google.api.codegen.discogapic.SchemaTransformationContext)1 DiscoGapicInterfaceContext (com.google.api.codegen.transformer.DiscoGapicInterfaceContext)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1 JavaSurfaceNamer (com.google.api.codegen.transformer.java.JavaSurfaceNamer)1 SymbolTable (com.google.api.codegen.util.SymbolTable)1 HttpMethodView (com.google.api.codegen.viewmodel.HttpMethodView)1 StaticLangApiMessageView (com.google.api.codegen.viewmodel.StaticLangApiMessageView)1 StaticLangApiResourceNameView (com.google.api.codegen.viewmodel.StaticLangApiResourceNameView)1 ViewModel (com.google.api.codegen.viewmodel.ViewModel)1 HashMap (java.util.HashMap)1