Search in sources :

Example 1 with MethodContext

use of com.google.api.codegen.transformer.MethodContext in project toolkit by googleapis.

the class JavaDiscoGapicRequestToViewTransformer method getRequestObjectParams.

private RequestObjectParamView getRequestObjectParams(DiscoGapicInterfaceContext context, MethodModel method) {
    MethodConfig methodConfig = context.getMethodConfig(method);
    // Generate the ResourceName methods.
    if (methodConfig.isFlattening()) {
        for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
            MethodContext flattenedMethodContext = context.asFlattenedMethodContext(method, flatteningGroup);
            Iterable<FieldConfig> fieldConfigs = flattenedMethodContext.getFlatteningConfig().getFlattenedFieldConfigs().values();
            for (FieldConfig fieldConfig : fieldConfigs) {
                if (context.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
                    return resourceObjectTransformer.generateRequestObjectParam(flattenedMethodContext, fieldConfig);
                }
            }
        }
    }
    return null;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) FieldConfig(com.google.api.codegen.config.FieldConfig) MethodContext(com.google.api.codegen.transformer.MethodContext) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig)

Example 2 with MethodContext

use of com.google.api.codegen.transformer.MethodContext in project toolkit by googleapis.

the class CSharpGapicClientTransformer method generateApiMethods.

private List<StaticLangApiMethodView> generateApiMethods(GapicInterfaceContext context) {
    List<ParamWithSimpleDoc> pagedMethodAdditionalParams = new ImmutableList.Builder<ParamWithSimpleDoc>().addAll(csharpCommonTransformer.pagedMethodAdditionalParams()).addAll(csharpCommonTransformer.callSettingsParam()).build();
    List<StaticLangApiMethodView> apiMethods = new ArrayList<>();
    for (MethodModel method : csharpCommonTransformer.getSupportedMethods(context)) {
        MethodConfig methodConfig = context.getMethodConfig(method);
        MethodContext requestMethodContext = context.asRequestMethodContext(method);
        if (methodConfig.isGrpcStreaming()) {
            // Only for protobuf-based APIs.
            apiMethods.add(apiMethodTransformer.generateGrpcStreamingRequestObjectMethod(requestMethodContext));
        } else if (methodConfig.isLongRunningOperation()) {
            // Only for protobuf-based APIs.
            GapicMethodContext gapicMethodContext = (GapicMethodContext) requestMethodContext;
            if (methodConfig.isFlattening()) {
                for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
                    GapicMethodContext methodContext = context.asFlattenedMethodContext(requestMethodContext.getMethodModel(), flatteningGroup);
                    apiMethods.add(apiMethodTransformer.generateAsyncOperationFlattenedMethod(methodContext, csharpCommonTransformer.callSettingsParam(), ClientMethodType.AsyncOperationFlattenedCallSettingsMethod, true));
                    apiMethods.add(apiMethodTransformer.generateAsyncOperationFlattenedMethod(methodContext, csharpCommonTransformer.cancellationTokenParam(), ClientMethodType.AsyncOperationFlattenedCancellationMethod, true));
                    apiMethods.add(apiMethodTransformer.generateOperationFlattenedMethod(methodContext, csharpCommonTransformer.callSettingsParam()));
                }
            }
            apiMethods.add(apiMethodTransformer.generateAsyncOperationRequestObjectMethod(requestMethodContext, csharpCommonTransformer.callSettingsParam(), true));
            apiMethods.add(apiMethodTransformer.generateOperationRequestObjectMethod(gapicMethodContext, csharpCommonTransformer.callSettingsParam()));
        } else if (methodConfig.isPageStreaming()) {
            if (methodConfig.isFlattening()) {
                for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
                    GapicMethodContext methodContext = context.asFlattenedMethodContext(method, flatteningGroup);
                    apiMethods.add(apiMethodTransformer.generatePagedFlattenedAsyncMethod(methodContext, pagedMethodAdditionalParams));
                    apiMethods.add(apiMethodTransformer.generatePagedFlattenedMethod(methodContext, pagedMethodAdditionalParams));
                }
            }
            apiMethods.add(apiMethodTransformer.generatePagedRequestObjectAsyncMethod(requestMethodContext, csharpCommonTransformer.callSettingsParam()));
            apiMethods.add(apiMethodTransformer.generatePagedRequestObjectMethod(requestMethodContext, csharpCommonTransformer.callSettingsParam()));
        } else {
            if (methodConfig.isFlattening()) {
                for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
                    GapicMethodContext methodContext = context.asFlattenedMethodContext(method, flatteningGroup);
                    apiMethods.add(apiMethodTransformer.generateFlattenedAsyncMethod(methodContext, csharpCommonTransformer.callSettingsParam(), ClientMethodType.FlattenedAsyncCallSettingsMethod));
                    apiMethods.add(apiMethodTransformer.generateFlattenedAsyncMethod(methodContext, csharpCommonTransformer.cancellationTokenParam(), ClientMethodType.FlattenedAsyncCancellationTokenMethod));
                    apiMethods.add(apiMethodTransformer.generateFlattenedMethod(methodContext, csharpCommonTransformer.callSettingsParam()));
                }
            }
            apiMethods.add(apiMethodTransformer.generateRequestObjectAsyncMethod(requestMethodContext, csharpCommonTransformer.callSettingsParam(), ClientMethodType.AsyncRequestObjectCallSettingsMethod));
            apiMethods.add(apiMethodTransformer.generateRequestObjectAsyncMethod(requestMethodContext, csharpCommonTransformer.cancellationTokenParam(), ClientMethodType.AsyncRequestObjectCancellationMethod));
            apiMethods.add(apiMethodTransformer.generateRequestObjectMethod(requestMethodContext, csharpCommonTransformer.callSettingsParam()));
        }
    }
    return apiMethods;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodModel(com.google.api.codegen.config.MethodModel) GapicMethodContext(com.google.api.codegen.transformer.GapicMethodContext) ParamWithSimpleDoc(com.google.api.codegen.transformer.ParamWithSimpleDoc) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) GapicMethodContext(com.google.api.codegen.transformer.GapicMethodContext) MethodContext(com.google.api.codegen.transformer.MethodContext) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig)

Example 3 with MethodContext

use of com.google.api.codegen.transformer.MethodContext in project toolkit by googleapis.

the class CSharpGapicSnippetsTransformer method generateMethods.

private List<StaticLangApiMethodSnippetView> generateMethods(InterfaceContext context) {
    List<StaticLangApiMethodSnippetView> methods = new ArrayList<>();
    for (MethodModel method : csharpCommonTransformer.getSupportedMethods(context)) {
        MethodConfig methodConfig = context.getMethodConfig(method);
        MethodContext methodContext = context.asRequestMethodContext(method);
        if (methodConfig.isGrpcStreaming()) {
            methods.add(generateGrpcStreamingRequestMethod(methodContext));
        } else if (methodConfig.isLongRunningOperation()) {
            if (methodConfig.isFlattening()) {
                ImmutableList<FlatteningConfig> flatteningGroups = methodConfig.getFlatteningConfigs();
                boolean requiresNameSuffix = flatteningGroups.size() > 1;
                for (int i = 0; i < flatteningGroups.size(); i++) {
                    FlatteningConfig flatteningGroup = flatteningGroups.get(i);
                    String nameSuffix = requiresNameSuffix ? Integer.toString(i + 1) : "";
                    MethodContext methodContextFlat = context.asFlattenedMethodContext(method, flatteningGroup);
                    methods.add(generateOperationFlattenedAsyncMethod(methodContextFlat, nameSuffix));
                    methods.add(generateOperationFlattenedMethod(methodContextFlat, nameSuffix));
                }
            }
            methods.add(generateOperationRequestAsyncMethod(methodContext));
            methods.add(generateOperationRequestMethod(methodContext));
        } else if (methodConfig.isPageStreaming()) {
            if (methodConfig.isFlattening()) {
                ImmutableList<FlatteningConfig> flatteningGroups = methodConfig.getFlatteningConfigs();
                // Find flattenings that have ambiguous parameters, and mark them to use named arguments.
                // Ambiguity occurs in a page-stream flattening that has one or two extra string
                // parameters (that are not resource-names) compared to any other flattening of this same
                // method.
                // Create a string for each flattening, encoding which parameters are strings and
                // not-strings. Each character in the string refers to a parameter. Each string refers
                // to a flattening.
                String[] stringParams = flatteningGroups.stream().map(flat -> flat.getFlattenedFieldConfigs().values().stream().map(field -> field.getField().getType().isStringType() && field.getResourceNameConfig() == null ? 's' : '.').collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString()).toArray(String[]::new);
                // Array of which flattenings need to use named arguments.
                // Each array entry refers to the correspondingly indexed flattening.
                Boolean[] requiresNamedParameters = Arrays.stream(stringParams).map(a -> Arrays.stream(stringParams).anyMatch(b -> a.startsWith(b + "s") || a.startsWith(b + "ss"))).toArray(Boolean[]::new);
                boolean requiresNameSuffix = flatteningGroups.size() > 1;
                // Build method list.
                for (int i = 0; i < flatteningGroups.size(); i++) {
                    FlatteningConfig flatteningGroup = flatteningGroups.get(i);
                    String nameSuffix = requiresNameSuffix ? Integer.toString(i + 1) : "";
                    MethodContext methodContextFlat = context.asFlattenedMethodContext(method, flatteningGroup);
                    methods.add(generatePagedFlattenedAsyncMethod(methodContextFlat, nameSuffix, requiresNamedParameters[i]));
                    methods.add(generatePagedFlattenedMethod(methodContextFlat, nameSuffix, requiresNamedParameters[i]));
                }
            }
            methods.add(generatePagedRequestAsyncMethod(methodContext));
            methods.add(generatePagedRequestMethod(methodContext));
        } else {
            if (methodConfig.isFlattening()) {
                ImmutableList<FlatteningConfig> flatteningGroups = methodConfig.getFlatteningConfigs();
                boolean requiresNameSuffix = flatteningGroups.size() > 1;
                for (int i = 0; i < flatteningGroups.size(); i++) {
                    FlatteningConfig flatteningGroup = flatteningGroups.get(i);
                    String nameSuffix = requiresNameSuffix ? Integer.toString(i + 1) : "";
                    MethodContext methodContextFlat = context.asFlattenedMethodContext(method, flatteningGroup);
                    methods.add(generateFlattenedAsyncMethod(methodContextFlat, nameSuffix));
                    methods.add(generateFlattenedMethod(methodContextFlat, nameSuffix));
                }
            }
            methods.add(generateRequestAsyncMethod(methodContext));
            methods.add(generateRequestMethod(methodContext));
        }
    }
    return methods;
}
Also used : StandardImportSectionTransformer(com.google.api.codegen.transformer.StandardImportSectionTransformer) Arrays(java.util.Arrays) GapicInterfaceContext(com.google.api.codegen.transformer.GapicInterfaceContext) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig) ArrayList(java.util.ArrayList) StaticLangApiMethodTransformer(com.google.api.codegen.transformer.StaticLangApiMethodTransformer) MethodContext(com.google.api.codegen.transformer.MethodContext) ImmutableList(com.google.common.collect.ImmutableList) MethodModel(com.google.api.codegen.config.MethodModel) ViewModel(com.google.api.codegen.viewmodel.ViewModel) ParamWithSimpleDoc(com.google.api.codegen.transformer.ParamWithSimpleDoc) MethodConfig(com.google.api.codegen.config.MethodConfig) StaticLangApiMethodSnippetView(com.google.api.codegen.viewmodel.StaticLangApiMethodSnippetView) SnippetsFileView(com.google.api.codegen.viewmodel.SnippetsFileView) GapicCodePathMapper(com.google.api.codegen.gapic.GapicCodePathMapper) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) FileHeaderTransformer(com.google.api.codegen.transformer.FileHeaderTransformer) InterfaceContext(com.google.api.codegen.transformer.InterfaceContext) File(java.io.File) GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) List(java.util.List) ApiModel(com.google.api.codegen.config.ApiModel) InterfaceModel(com.google.api.codegen.config.InterfaceModel) PageStreamingConfig(com.google.api.codegen.config.PageStreamingConfig) Collections(java.util.Collections) ModelToViewTransformer(com.google.api.codegen.transformer.ModelToViewTransformer) FieldConfig(com.google.api.codegen.config.FieldConfig) ClientMethodType(com.google.api.codegen.viewmodel.ClientMethodType) MethodModel(com.google.api.codegen.config.MethodModel) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.transformer.MethodContext) MethodConfig(com.google.api.codegen.config.MethodConfig) StaticLangApiMethodSnippetView(com.google.api.codegen.viewmodel.StaticLangApiMethodSnippetView) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig)

Example 4 with MethodContext

use of com.google.api.codegen.transformer.MethodContext in project toolkit by googleapis.

the class JavaSurfaceTestTransformer method createSmokeTestClassViewBuilder.

/**
 * Package-private
 *
 * <p>A helper method that creates a partially initialized builder that can be customized and
 * build the smoke test class view later.
 */
SmokeTestClassView.Builder createSmokeTestClassViewBuilder(InterfaceContext context) {
    addSmokeTestImports(context);
    MethodModel method = context.getInterfaceConfig().getSmokeTestConfig().getMethod();
    SurfaceNamer namer = context.getNamer();
    FlatteningConfig flatteningGroup = testCaseTransformer.getSmokeTestFlatteningGroup(context.getMethodConfig(method), context.getInterfaceConfig().getSmokeTestConfig());
    MethodContext methodContext = context.asFlattenedMethodContext(method, flatteningGroup);
    SmokeTestClassView.Builder testClass = SmokeTestClassView.newBuilder();
    StaticLangApiMethodView apiMethodView = createSmokeTestCaseApiMethodView(methodContext);
    testClass.apiSettingsClassName(namer.getApiSettingsClassName(context.getInterfaceConfig()));
    testClass.apiClassName(namer.getApiWrapperClassName(context.getInterfaceConfig()));
    testClass.templateFileName(SMOKE_TEST_TEMPLATE_FILE);
    testClass.apiMethod(apiMethodView);
    testClass.requireProjectId(testCaseTransformer.requireProjectIdInSmokeTest(apiMethodView.initCode(), context.getNamer()));
    // Imports must be done as the last step to catch all imports.
    FileHeaderView fileHeader = fileHeaderTransformer.generateFileHeader(context);
    testClass.fileHeader(fileHeader);
    return testClass;
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) SmokeTestClassView(com.google.api.codegen.viewmodel.testing.SmokeTestClassView) MethodContext(com.google.api.codegen.transformer.MethodContext) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) FileHeaderView(com.google.api.codegen.viewmodel.FileHeaderView)

Example 5 with MethodContext

use of com.google.api.codegen.transformer.MethodContext in project toolkit by googleapis.

the class JavaSurfaceTransformer method generateApiMethods.

private List<StaticLangApiMethodView> generateApiMethods(InterfaceContext context) {
    List<StaticLangApiMethodView> apiMethods = new ArrayList<>();
    for (MethodModel method : context.getSupportedMethods()) {
        MethodConfig methodConfig = context.getMethodConfig(method);
        MethodContext requestMethodContext = context.asRequestMethodContext(method);
        if (methodConfig.isPageStreaming()) {
            if (methodConfig.isFlattening()) {
                for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
                    MethodContext flattenedMethodContext = context.asFlattenedMethodContext(method, flatteningGroup);
                    apiMethods.add(apiMethodTransformer.generatePagedFlattenedMethod(flattenedMethodContext));
                    if (hasAnyResourceNameParameter(flatteningGroup)) {
                        apiMethods.add(apiMethodTransformer.generatePagedFlattenedMethod(flattenedMethodContext.withResourceNamesInSamplesOnly()));
                    }
                }
            }
            apiMethods.add(apiMethodTransformer.generatePagedRequestObjectMethod(requestMethodContext));
            apiMethods.add(apiMethodTransformer.generatePagedCallableMethod(requestMethodContext));
            apiMethods.add(apiMethodTransformer.generateUnpagedListCallableMethod(requestMethodContext));
        } else if (methodConfig.isGrpcStreaming()) {
            ImportTypeTable typeTable = context.getImportTypeTable();
            switch(methodConfig.getGrpcStreamingType()) {
                case BidiStreaming:
                    typeTable.saveNicknameFor("com.google.api.gax.rpc.BidiStreamingCallable");
                    break;
                case ClientStreaming:
                    typeTable.saveNicknameFor("com.google.api.gax.rpc.ClientStreamingCallable");
                    break;
                case ServerStreaming:
                    typeTable.saveNicknameFor("com.google.api.gax.rpc.ServerStreamingCallable");
                    break;
                default:
                    throw new IllegalArgumentException("Invalid streaming type: " + methodConfig.getGrpcStreamingType());
            }
            apiMethods.add(apiMethodTransformer.generateCallableMethod(requestMethodContext));
        } else if (methodConfig.isLongRunningOperation()) {
            context.getImportTypeTable().saveNicknameFor("com.google.api.gax.rpc.OperationCallable");
            if (methodConfig.isFlattening()) {
                for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
                    MethodContext flattenedMethodContext = context.asFlattenedMethodContext(method, flatteningGroup);
                    apiMethods.add(apiMethodTransformer.generateAsyncOperationFlattenedMethod(flattenedMethodContext));
                    if (hasAnyResourceNameParameter(flatteningGroup)) {
                        apiMethods.add(apiMethodTransformer.generateAsyncOperationFlattenedMethod(flattenedMethodContext.withResourceNamesInSamplesOnly()));
                    }
                }
            }
            apiMethods.add(apiMethodTransformer.generateAsyncOperationRequestObjectMethod(requestMethodContext));
            apiMethods.add(apiMethodTransformer.generateOperationCallableMethod(requestMethodContext));
            apiMethods.add(apiMethodTransformer.generateCallableMethod(requestMethodContext));
        } else {
            if (methodConfig.isFlattening()) {
                for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
                    MethodContext flattenedMethodContext = context.asFlattenedMethodContext(method, flatteningGroup);
                    apiMethods.add(apiMethodTransformer.generateFlattenedMethod(flattenedMethodContext));
                    if (hasAnyResourceNameParameter(flatteningGroup)) {
                        apiMethods.add(apiMethodTransformer.generateFlattenedMethod(flattenedMethodContext.withResourceNamesInSamplesOnly()));
                    }
                }
            }
            apiMethods.add(apiMethodTransformer.generateRequestObjectMethod(requestMethodContext));
            apiMethods.add(apiMethodTransformer.generateCallableMethod(requestMethodContext));
        }
    }
    return apiMethods;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodModel(com.google.api.codegen.config.MethodModel) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) ImportTypeTable(com.google.api.codegen.transformer.ImportTypeTable) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.transformer.MethodContext) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig)

Aggregations

MethodContext (com.google.api.codegen.transformer.MethodContext)10 MethodConfig (com.google.api.codegen.config.MethodConfig)8 MethodModel (com.google.api.codegen.config.MethodModel)8 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)6 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)6 ArrayList (java.util.ArrayList)6 GapicMethodContext (com.google.api.codegen.transformer.GapicMethodContext)4 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)4 FieldConfig (com.google.api.codegen.config.FieldConfig)3 ParamWithSimpleDoc (com.google.api.codegen.transformer.ParamWithSimpleDoc)2 ClientMethodType (com.google.api.codegen.viewmodel.ClientMethodType)2 FileHeaderView (com.google.api.codegen.viewmodel.FileHeaderView)2 SmokeTestClassView (com.google.api.codegen.viewmodel.testing.SmokeTestClassView)2 ImmutableList (com.google.common.collect.ImmutableList)2 ApiModel (com.google.api.codegen.config.ApiModel)1 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)1 InterfaceModel (com.google.api.codegen.config.InterfaceModel)1 PageStreamingConfig (com.google.api.codegen.config.PageStreamingConfig)1 GapicCodePathMapper (com.google.api.codegen.gapic.GapicCodePathMapper)1 InitCodeContext (com.google.api.codegen.metacode.InitCodeContext)1