Search in sources :

Example 1 with StaticLangMemberView

use of com.google.api.codegen.viewmodel.StaticLangMemberView 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();
}
Also used : Schema(com.google.api.codegen.discovery.Schema) SymbolTable(com.google.api.codegen.util.SymbolTable) StaticLangMemberView(com.google.api.codegen.viewmodel.StaticLangMemberView) StaticLangApiResourceNameView(com.google.api.codegen.viewmodel.StaticLangApiResourceNameView) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 2 with StaticLangMemberView

use of com.google.api.codegen.viewmodel.StaticLangMemberView 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();
}
Also used : ArrayList(java.util.ArrayList) StaticLangMemberView(com.google.api.codegen.viewmodel.StaticLangMemberView) RequestObjectParamView(com.google.api.codegen.viewmodel.RequestObjectParamView) FieldModel(com.google.api.codegen.config.FieldModel)

Aggregations

StaticLangMemberView (com.google.api.codegen.viewmodel.StaticLangMemberView)2 FieldModel (com.google.api.codegen.config.FieldModel)1 Schema (com.google.api.codegen.discovery.Schema)1 SymbolTable (com.google.api.codegen.util.SymbolTable)1 RequestObjectParamView (com.google.api.codegen.viewmodel.RequestObjectParamView)1 StaticLangApiResourceNameView (com.google.api.codegen.viewmodel.StaticLangApiResourceNameView)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1