Search in sources :

Example 1 with FieldModel

use of com.google.api.codegen.config.FieldModel 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 FieldModel

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

the class InitCodeNode method getChildFieldConfig.

private static FieldConfig getChildFieldConfig(Map<String, FieldConfig> fieldConfigMap, FieldConfig parentFieldConfig, TypeModel parentType, String key) {
    if (parentType.isMap()) {
        return parentFieldConfig;
    } else if (parentType.isRepeated()) {
        return parentFieldConfig;
    } else if (parentType.isMessage()) {
        FieldModel childField = parentType.getField(key);
        if (childField == null) {
            throw new IllegalArgumentException("Message type " + parentType + " does not have field " + key);
        }
        FieldConfig fieldConfig = fieldConfigMap.get(childField.getFullName());
        if (fieldConfig == null) {
            fieldConfig = FieldConfig.createDefaultFieldConfig(childField);
        }
        return fieldConfig;
    } else {
        throw new IllegalArgumentException("Primitive type " + parentType + " cannot have children. Child key: " + key);
    }
}
Also used : FieldConfig(com.google.api.codegen.config.FieldConfig) FieldModel(com.google.api.codegen.config.FieldModel)

Example 3 with FieldModel

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

the class BatchingTransformer method generateDescriptorClass.

private BatchingDescriptorClassView generateDescriptorClass(MethodContext context) {
    SurfaceNamer namer = context.getNamer();
    MethodModel method = context.getMethodModel();
    BatchingConfig batching = context.getMethodConfig().getBatching();
    FieldModel batchedField = batching.getBatchedField();
    FieldModel subresponseField = batching.getSubresponseField();
    BatchingDescriptorClassView.Builder desc = BatchingDescriptorClassView.newBuilder();
    desc.name(context.getNamer().getBatchingDescriptorConstName(context.getMethodModel()));
    desc.requestTypeName(method.getAndSaveRequestTypeName(context.getTypeTable(), context.getNamer()));
    desc.responseTypeName(method.getAndSaveResponseTypeName(context.getTypeTable(), context.getNamer()));
    desc.batchedFieldTypeName(context.getTypeTable().getAndSaveNicknameFor(batchedField));
    desc.partitionKeys(generatePartitionKeys(context));
    desc.discriminatorFieldCopies(generateDiscriminatorFieldCopies(context));
    desc.batchedFieldGetFunction(namer.getFieldGetFunctionName(batchedField));
    desc.batchedFieldSetFunction(namer.getFieldSetFunctionName(batchedField));
    desc.batchedFieldCountGetFunction(namer.getFieldCountGetFunctionName(batchedField));
    if (subresponseField != null) {
        desc.subresponseTypeName(context.getTypeTable().getAndSaveNicknameFor(subresponseField));
        desc.subresponseByIndexGetFunction(namer.getByIndexGetFunctionName(subresponseField));
        desc.subresponseSetFunction(namer.getFieldSetFunctionName(subresponseField));
    }
    return desc.build();
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) BatchingDescriptorClassView(com.google.api.codegen.viewmodel.BatchingDescriptorClassView) BatchingConfig(com.google.api.codegen.config.BatchingConfig) FieldModel(com.google.api.codegen.config.FieldModel)

Example 4 with FieldModel

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

the class GrpcElementDocTransformer method generateMessagePropertyDocs.

private List<ParamDocView> generateMessagePropertyDocs(ModelTypeTable typeTable, SurfaceNamer namer, Iterable<FieldModel> fields) {
    ImmutableList.Builder<ParamDocView> propertyDocs = ImmutableList.builder();
    for (FieldModel field : fields) {
        SimpleParamDocView.Builder doc = SimpleParamDocView.newBuilder();
        doc.paramName(namer.getFieldKey(field));
        doc.typeName(namer.getMessagePropertyTypeName(typeTable, field));
        doc.lines(namer.getDocLines(field));
        propertyDocs.add(doc.build());
    }
    return propertyDocs.build();
}
Also used : SimpleParamDocView(com.google.api.codegen.viewmodel.SimpleParamDocView) ImmutableList(com.google.common.collect.ImmutableList) SimpleParamDocView(com.google.api.codegen.viewmodel.SimpleParamDocView) ParamDocView(com.google.api.codegen.viewmodel.ParamDocView) FieldModel(com.google.api.codegen.config.FieldModel)

Example 5 with FieldModel

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

the class IamResourceTransformer method generateIamResources.

public List<IamResourceView> generateIamResources(InterfaceContext context) {
    List<IamResourceView> resources = new ArrayList<>();
    for (FieldModel field : context.getProductConfig().getInterfaceConfig(context.getInterfaceModel()).getIamResources()) {
        String resourceTypeName = context.getImportTypeTable().getAndSaveNicknameFor(field.getParentTypeName(context.getImportTypeTable()).getFullName());
        resources.add(IamResourceView.builder().resourceGetterFunctionName(context.getNamer().getIamResourceGetterFunctionName(field)).paramName(context.getNamer().getIamResourceParamName(field)).exampleName(context.getNamer().getIamResourceGetterFunctionExampleName(context.getInterfaceConfig(), field)).fieldName(context.getNamer().publicFieldName(Name.from(field.getSimpleName()))).resourceTypeName(resourceTypeName).resourceConstructorName(context.getNamer().getTypeConstructor(resourceTypeName)).build());
    }
    return resources;
}
Also used : ArrayList(java.util.ArrayList) FieldModel(com.google.api.codegen.config.FieldModel) IamResourceView(com.google.api.codegen.viewmodel.IamResourceView)

Aggregations

FieldModel (com.google.api.codegen.config.FieldModel)44 MethodConfig (com.google.api.codegen.config.MethodConfig)13 ArrayList (java.util.ArrayList)13 ImmutableList (com.google.common.collect.ImmutableList)11 FieldConfig (com.google.api.codegen.config.FieldConfig)9 MethodModel (com.google.api.codegen.config.MethodModel)8 SimpleParamDocView (com.google.api.codegen.viewmodel.SimpleParamDocView)7 ParamDocView (com.google.api.codegen.viewmodel.ParamDocView)6 BatchingConfig (com.google.api.codegen.config.BatchingConfig)4 PageStreamingConfig (com.google.api.codegen.config.PageStreamingConfig)4 DynamicLangDefaultableParamView (com.google.api.codegen.viewmodel.DynamicLangDefaultableParamView)4 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)3 Name (com.google.api.codegen.util.Name)3 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)2 GenericFieldSelector (com.google.api.codegen.config.GenericFieldSelector)2 Schema (com.google.api.codegen.discovery.Schema)2 SymbolTable (com.google.api.codegen.util.SymbolTable)2 TypeName (com.google.api.codegen.util.TypeName)2 RequestObjectParamView (com.google.api.codegen.viewmodel.RequestObjectParamView)2 StaticLangApiMessageView (com.google.api.codegen.viewmodel.StaticLangApiMessageView)2