Search in sources :

Example 16 with StaticLangApiMethodView

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

the class GoGapicSurfaceTransformer method generate.

private StaticLangClientFileView generate(GapicInterfaceContext context) {
    StaticLangClientFileView.Builder view = StaticLangClientFileView.newBuilder();
    SurfaceNamer namer = context.getNamer();
    ApiModel model = context.getApiModel();
    ProtoInterfaceModel apiInterface = context.getInterfaceModel();
    GapicProductConfig productConfig = context.getProductConfig();
    GapicInterfaceConfig interfaceConfig = context.getInterfaceConfig();
    view.templateFileName(API_TEMPLATE_FILENAME);
    view.serviceDoc(serviceTransformer.generateServiceDoc(context, null, productConfig));
    view.domainLayerLocation(productConfig.getDomainLayerLocation());
    view.clientTypeName(namer.getApiWrapperClassName(context.getInterfaceConfig()));
    view.clientConstructorName(namer.getApiWrapperClassConstructorName(interfaceConfig));
    view.defaultClientOptionFunctionName(namer.getDefaultApiSettingsFunctionName(interfaceConfig));
    view.defaultCallOptionFunctionName(namer.getDefaultCallSettingsFunctionName(interfaceConfig));
    view.callOptionsTypeName(namer.getCallSettingsTypeName(interfaceConfig));
    view.serviceOriginalName(model.getTitle());
    view.servicePhraseName(namer.getServicePhraseName(interfaceConfig));
    String outputPath = pathMapper.getOutputPath(apiInterface.getFullName(), productConfig);
    String fileName = namer.getServiceFileName(context.getInterfaceConfig());
    view.outputPath(outputPath + File.separator + fileName);
    List<RetryConfigDefinitionView> retryDef = generateRetryConfigDefinitions(context, context.getSupportedMethods());
    view.retryPairDefinitions(retryDef);
    view.callSettings(apiCallableTransformer.generateCallSettings(context));
    List<StaticLangApiMethodView> apiMethods = generateApiMethods(context, context.getSupportedMethods());
    view.apiMethods(apiMethods);
    // If any methods have header request params, "fmt" is needed for `fmt.Sprintf` calls.
    if (apiMethods.stream().anyMatch(m -> !m.headerRequestParams().isEmpty())) {
        context.getImportTypeTable().saveNicknameFor("fmt;;;");
    }
    view.iamResources(iamResourceTransformer.generateIamResources(context));
    if (!((GapicInterfaceConfig) productConfig.getInterfaceConfig(apiInterface.getFullName())).getIamResources().isEmpty()) {
        context.getImportTypeTable().saveNicknameFor("cloud.google.com/go/iam;;;");
    }
    // In Go, multiple methods share the same iterator type, one iterator type per resource type.
    // We have to dedupe the iterators.
    Map<String, PageStreamingDescriptorClassView> iterators = new TreeMap<>();
    for (PageStreamingDescriptorClassView desc : pageStreamingTransformer.generateDescriptorClasses(context)) {
        iterators.put(desc.typeName(), desc);
    }
    view.pageStreamingDescriptorClasses(new ArrayList<>(iterators.values()));
    // Same with long running operations.
    Map<String, LongRunningOperationDetailView> lros = new TreeMap<>();
    for (StaticLangApiMethodView apiMethod : apiMethods) {
        LongRunningOperationDetailView lro = apiMethod.operationMethod();
        if (lro != null) {
            lros.put(lro.clientReturnTypeName(), lro);
        }
    }
    view.lroDetailViews(new ArrayList<>(lros.values()));
    view.serviceAddress(context.getApiModel().getServiceAddress());
    view.servicePort(model.getServicePort());
    view.stubs(grpcStubTransformer.generateGrpcStubs(context));
    addXApiImports(context, context.getSupportedMethods());
    view.fileHeader(fileHeaderTransformer.generateFileHeader(context));
    return view.build();
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) ProtoInterfaceModel(com.google.api.codegen.config.ProtoInterfaceModel) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) RetryConfigDefinitionView(com.google.api.codegen.viewmodel.RetryConfigDefinitionView) GapicInterfaceConfig(com.google.api.codegen.config.GapicInterfaceConfig) ApiModel(com.google.api.codegen.config.ApiModel) TreeMap(java.util.TreeMap) LongRunningOperationDetailView(com.google.api.codegen.viewmodel.LongRunningOperationDetailView) StaticLangClientFileView(com.google.api.codegen.viewmodel.StaticLangClientFileView) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) PageStreamingDescriptorClassView(com.google.api.codegen.viewmodel.PageStreamingDescriptorClassView)

Example 17 with StaticLangApiMethodView

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

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

the class JavaSurfaceTestTransformer method createSmokeTestCaseApiMethodView.

private StaticLangApiMethodView createSmokeTestCaseApiMethodView(MethodContext methodContext) {
    MethodConfig methodConfig = methodContext.getMethodConfig();
    StaticLangApiMethodView initialApiMethodView;
    if (methodConfig.isPageStreaming()) {
        if (methodContext.isFlattenedMethodContext()) {
            initialApiMethodView = apiMethodTransformer.generatePagedFlattenedMethod(methodContext);
        } else {
            throw new UnsupportedOperationException("Unsupported smoke test type: page-streaming + request-object");
        }
    } else if (methodConfig.isGrpcStreaming()) {
        throw new UnsupportedOperationException("Unsupported smoke test type: grpc-streaming");
    } else if (methodConfig.isLongRunningOperation()) {
        if (methodContext.isFlattenedMethodContext()) {
            initialApiMethodView = apiMethodTransformer.generateAsyncOperationFlattenedMethod(methodContext);
        } else {
            throw new UnsupportedOperationException("Unsupported smoke test type: long-running + request-object");
        }
    } else {
        if (methodContext.isFlattenedMethodContext()) {
            initialApiMethodView = apiMethodTransformer.generateFlattenedMethod(methodContext);
        } else {
            throw new UnsupportedOperationException("Unsupported smoke test type: simple-call + request-object");
        }
    }
    StaticLangApiMethodView.Builder apiMethodView = initialApiMethodView.toBuilder();
    InitCodeView initCodeView = initCodeTransformer.generateInitCode(methodContext, testCaseTransformer.createSmokeTestInitContext(methodContext));
    apiMethodView.initCode(initCodeView);
    return apiMethodView.build();
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) InitCodeView(com.google.api.codegen.viewmodel.InitCodeView)

Example 19 with StaticLangApiMethodView

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

Example 20 with StaticLangApiMethodView

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

the class JavaSurfaceTransformer method transform.

public List<ViewModel> transform(ApiModel model, GapicProductConfig productConfig) {
    List<ViewModel> surfaceDocs = new ArrayList<>();
    SurfaceNamer namer = surfaceTransformer.createSurfaceNamer(productConfig);
    List<ServiceDocView> serviceDocs = new ArrayList<>();
    for (InterfaceModel apiInterface : model.getInterfaces()) {
        boolean enableStringFormatFunctions = productConfig.getResourceNameMessageConfigs().isEmpty();
        ImportTypeTable typeTable = surfaceTransformer.createTypeTable(productConfig.getPackageName());
        InterfaceContext context = surfaceTransformer.createInterfaceContext(apiInterface, productConfig, namer, typeTable, enableStringFormatFunctions);
        StaticLangFileView<StaticLangApiView> apiFile = generateApiFile(context, productConfig);
        surfaceDocs.add(apiFile);
        serviceDocs.add(apiFile.classView().doc());
        StaticLangApiMethodView exampleApiMethod = getExampleApiMethod(apiFile.classView().apiMethods());
        context = context.withNewTypeTable(namer.getStubPackageName());
        StaticLangFileView<StaticLangStubSettingsView> stubSettingsFile = generateStubSettingsFile(context, productConfig, exampleApiMethod);
        context = context.withNewTypeTable(namer.getRootPackageName());
        StaticLangFileView<StaticLangSettingsView> settingsFile = generateSettingsFile(context, productConfig, exampleApiMethod, stubSettingsFile.classView());
        surfaceDocs.add(settingsFile);
        surfaceDocs.add(stubSettingsFile);
        context = context.withNewTypeTable(namer.getStubPackageName());
        StaticLangFileView<StaticLangStubInterfaceView> stubInterfaceFile = generateStubInterfaceFile(context, productConfig);
        surfaceDocs.add(stubInterfaceFile);
        context = context.withNewTypeTable(namer.getStubPackageName());
        StaticLangFileView<StaticLangRpcStubView> grpcStubFile = generateRpcStubClassFile(context, productConfig);
        surfaceDocs.add(grpcStubFile);
        surfaceDocs.add(generateCallableFactoryClassFile(context, productConfig));
    }
    PackageInfoView packageInfo = generatePackageInfo(model, productConfig, namer, serviceDocs);
    surfaceDocs.add(packageInfo);
    return surfaceDocs;
}
Also used : StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) StaticLangStubInterfaceView(com.google.api.codegen.viewmodel.StaticLangStubInterfaceView) ArrayList(java.util.ArrayList) PackageInfoView(com.google.api.codegen.viewmodel.PackageInfoView) ViewModel(com.google.api.codegen.viewmodel.ViewModel) InterfaceContext(com.google.api.codegen.transformer.InterfaceContext) ServiceDocView(com.google.api.codegen.viewmodel.ServiceDocView) InterfaceModel(com.google.api.codegen.config.InterfaceModel) ImportTypeTable(com.google.api.codegen.transformer.ImportTypeTable) StaticLangSettingsView(com.google.api.codegen.viewmodel.StaticLangSettingsView) StaticLangStubSettingsView(com.google.api.codegen.viewmodel.StaticLangStubSettingsView) StaticLangRpcStubView(com.google.api.codegen.viewmodel.StaticLangRpcStubView) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) StaticLangApiView(com.google.api.codegen.viewmodel.StaticLangApiView)

Aggregations

StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)45 MethodModel (com.google.api.codegen.config.MethodModel)23 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)22 Builder (com.google.api.codegen.viewmodel.StaticLangApiMethodView.Builder)17 FieldConfig (com.google.api.codegen.config.FieldConfig)6 MethodConfig (com.google.api.codegen.config.MethodConfig)6 MethodContext (com.google.api.codegen.transformer.MethodContext)6 ArrayList (java.util.ArrayList)6 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)5 PageStreamingConfig (com.google.api.codegen.config.PageStreamingConfig)5 TypeModel (com.google.api.codegen.config.TypeModel)5 InterfaceConfig (com.google.api.codegen.config.InterfaceConfig)4 GapicMethodContext (com.google.api.codegen.transformer.GapicMethodContext)4 InterfaceContext (com.google.api.codegen.transformer.InterfaceContext)3 ParamWithSimpleDoc (com.google.api.codegen.transformer.ParamWithSimpleDoc)3 StaticLangApiView (com.google.api.codegen.viewmodel.StaticLangApiView)3 ApiModel (com.google.api.codegen.config.ApiModel)2 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)2 InterfaceModel (com.google.api.codegen.config.InterfaceModel)2 ImportTypeTable (com.google.api.codegen.transformer.ImportTypeTable)2