Search in sources :

Example 1 with StaticLangApiMessageView

use of com.google.api.codegen.viewmodel.StaticLangApiMessageView 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 StaticLangApiMessageView

use of com.google.api.codegen.viewmodel.StaticLangApiMessageView in project toolkit by googleapis.

the class JavaDiscoGapicSchemaToViewTransformer method transform.

@Override
public List<ViewModel> transform(DiscoApiModel model, GapicProductConfig productConfig) {
    List<ViewModel> surfaceSchemas = new ArrayList<>();
    String packageName = productConfig.getPackageName();
    JavaSurfaceNamer surfaceNamer = new JavaSurfaceNamer(packageName, packageName, nameFormatter);
    DiscoGapicInterfaceContext context = DiscoGapicInterfaceContext.createWithoutInterface(model, productConfig, createTypeTable(productConfig.getPackageName(), surfaceNamer), surfaceNamer, JavaFeatureConfig.newBuilder().enableStringFormatFunctions(true).build());
    for (Schema schema : context.getDocument().schemas().values()) {
        Map<SchemaTransformationContext, StaticLangApiMessageView> contextViews = new TreeMap<>(SchemaTransformationContext.comparator);
        generateSchemaClasses(contextViews, context, schema);
        for (Map.Entry<SchemaTransformationContext, StaticLangApiMessageView> contextView : contextViews.entrySet()) {
            surfaceSchemas.add(generateSchemaFile(contextView.getKey(), contextView.getValue()));
        }
    }
    Collections.sort(surfaceSchemas, new Comparator<ViewModel>() {

        @Override
        public int compare(ViewModel o1, ViewModel o2) {
            return String.CASE_INSENSITIVE_ORDER.compare(o1.outputPath(), o2.outputPath());
        }
    });
    return surfaceSchemas;
}
Also used : SchemaTransformationContext(com.google.api.codegen.discogapic.SchemaTransformationContext) Schema(com.google.api.codegen.discovery.Schema) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) TreeMap(java.util.TreeMap) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) StaticLangApiMessageView(com.google.api.codegen.viewmodel.StaticLangApiMessageView) Map(java.util.Map) TreeMap(java.util.TreeMap) DiscoGapicInterfaceContext(com.google.api.codegen.transformer.DiscoGapicInterfaceContext)

Example 3 with StaticLangApiMessageView

use of com.google.api.codegen.viewmodel.StaticLangApiMessageView in project toolkit by googleapis.

the class JavaDiscoGapicSchemaToViewTransformer method generateSchemaClasses.

private StaticLangApiMessageView generateSchemaClasses(Map<SchemaTransformationContext, StaticLangApiMessageView> messageViewAccumulator, DiscoGapicInterfaceContext documentContext, Schema schema) {
    FieldModel schemaModel = DiscoveryField.create(schema, documentContext.getApiModel());
    SchemaTypeTable schemaTypeTable = documentContext.getSchemaTypeTable().cloneEmpty();
    SchemaTransformationContext context = SchemaTransformationContext.create(schema.getIdentifier(), schemaTypeTable, documentContext);
    StaticLangApiMessageView.Builder schemaView = StaticLangApiMessageView.newBuilder();
    boolean hasRequiredProperties = false;
    // Child schemas cannot have the same symbols as parent schemas, but sibling schemas can have
    // the same symbols.
    SymbolTable symbolTableCopy = SymbolTable.fromSeed(reservedKeywords);
    String schemaId = Name.anyCamel(schema.getIdentifier()).toLowerCamel();
    String schemaName = nameFormatter.privateFieldName(Name.anyCamel(symbolTableCopy.getNewSymbol(schemaId)));
    schemaView.name(schemaName);
    schemaView.defaultValue(schema.defaultValue());
    schemaView.description(schema.description());
    schemaView.fieldGetFunction(context.getNamer().getFieldGetFunctionName(schemaModel));
    schemaView.fieldSetFunction(context.getNamer().getFieldSetFunctionName(schemaModel));
    schemaView.fieldAddFunction(context.getNamer().getFieldAddFunctionName(schemaModel));
    String schemaTypeName = schemaTypeTable.getAndSaveNicknameFor(schema);
    schemaView.typeName(schemaTypeName);
    if (schema.repeated() || schema.type() == Type.ARRAY) {
        schemaView.innerTypeName(schemaTypeTable.getInnerTypeNameFor(schema));
    } else {
        schemaView.innerTypeName(schemaTypeName);
    }
    // Generate a Schema view from each property.
    List<StaticLangApiMessageView> viewProperties = new LinkedList<>();
    List<Schema> schemaProperties = new LinkedList<>();
    schemaProperties.addAll(schema.properties().values());
    if (schema.items() != null) {
        schemaProperties.addAll(schema.items().properties().values());
    }
    for (Schema property : schemaProperties) {
        viewProperties.add(generateSchemaClasses(messageViewAccumulator, documentContext, property));
        if (!property.properties().isEmpty() || (property.items() != null)) {
            // Add non-primitive-type property to imports.
            schemaTypeTable.getAndSaveNicknameFor(property);
        }
        if (property.required()) {
            hasRequiredProperties = true;
        }
    }
    Collections.sort(viewProperties);
    schemaView.properties(viewProperties);
    schemaView.canRepeat(schema.repeated() || schema.type().equals(Type.ARRAY));
    schemaView.isRequired(schema.required());
    schemaView.isRequestMessage(false);
    schemaView.hasRequiredProperties(hasRequiredProperties);
    if (!schema.properties().isEmpty() || (schema.items() != null && !schema.items().properties().isEmpty())) {
        // This is a top-level Schema, so add it to list of file ViewModels for rendering.
        messageViewAccumulator.put(context, schemaView.build());
    }
    return schemaView.build();
}
Also used : SchemaTransformationContext(com.google.api.codegen.discogapic.SchemaTransformationContext) SchemaTypeTable(com.google.api.codegen.transformer.SchemaTypeTable) StaticLangApiMessageView(com.google.api.codegen.viewmodel.StaticLangApiMessageView) Schema(com.google.api.codegen.discovery.Schema) SymbolTable(com.google.api.codegen.util.SymbolTable) FieldModel(com.google.api.codegen.config.FieldModel) LinkedList(java.util.LinkedList)

Example 4 with StaticLangApiMessageView

use of com.google.api.codegen.viewmodel.StaticLangApiMessageView in project toolkit by googleapis.

the class JavaDiscoGapicRequestToViewTransformer method transform.

@Override
public List<ViewModel> transform(DiscoApiModel model, GapicProductConfig productConfig) {
    List<ViewModel> surfaceRequests = new ArrayList<>();
    String packageName = productConfig.getPackageName();
    SurfaceNamer surfaceNamer = new JavaSurfaceNamer(packageName, packageName, nameFormatter);
    for (InterfaceModel apiInterface : model.getInterfaces()) {
        boolean enableStringFormatFunctions = productConfig.getResourceNameMessageConfigs().isEmpty();
        DiscoGapicInterfaceContext context = JavaDiscoGapicSurfaceTransformer.newInterfaceContext(apiInterface, productConfig, surfaceNamer, createTypeTable(productConfig.getPackageName()), enableStringFormatFunctions);
        for (MethodModel method : context.getSupportedMethods()) {
            RequestObjectParamView params = getRequestObjectParams(context, method);
            SchemaTransformationContext requestContext = SchemaTransformationContext.create(method.getFullName(), context.getSchemaTypeTable(), context);
            StaticLangApiMessageView requestView = generateRequestClass(requestContext, method, params);
            surfaceRequests.add(generateRequestFile(requestContext, requestView));
        }
    }
    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) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) MethodModel(com.google.api.codegen.config.MethodModel) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) RequestObjectParamView(com.google.api.codegen.viewmodel.RequestObjectParamView) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) InterfaceModel(com.google.api.codegen.config.InterfaceModel) StaticLangApiMessageView(com.google.api.codegen.viewmodel.StaticLangApiMessageView) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) DiscoGapicInterfaceContext(com.google.api.codegen.transformer.DiscoGapicInterfaceContext)

Aggregations

StaticLangApiMessageView (com.google.api.codegen.viewmodel.StaticLangApiMessageView)4 SchemaTransformationContext (com.google.api.codegen.discogapic.SchemaTransformationContext)3 Schema (com.google.api.codegen.discovery.Schema)3 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)2 FieldModel (com.google.api.codegen.config.FieldModel)2 DiscoGapicInterfaceContext (com.google.api.codegen.transformer.DiscoGapicInterfaceContext)2 JavaSurfaceNamer (com.google.api.codegen.transformer.java.JavaSurfaceNamer)2 SymbolTable (com.google.api.codegen.util.SymbolTable)2 ViewModel (com.google.api.codegen.viewmodel.ViewModel)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 InterfaceModel (com.google.api.codegen.config.InterfaceModel)1 MethodModel (com.google.api.codegen.config.MethodModel)1 Method (com.google.api.codegen.discovery.Method)1 SchemaTypeTable (com.google.api.codegen.transformer.SchemaTypeTable)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1 RequestObjectParamView (com.google.api.codegen.viewmodel.RequestObjectParamView)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1