Search in sources :

Example 1 with InterfaceContext

use of com.google.api.codegen.transformer.InterfaceContext 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 2 with InterfaceContext

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

Example 3 with InterfaceContext

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

the class JavaSurfaceTransformer method generateRpcStubClass.

private StaticLangRpcStubView generateRpcStubClass(InterfaceContext context, GapicProductConfig productConfig) {
    SurfaceNamer namer = context.getNamer();
    InterfaceConfig interfaceConfig = context.getInterfaceConfig();
    addRpcStubImports(context);
    // Stub class has different default package name from method, request, and resource classes.
    InterfaceContext apiMethodsContext = context.withNewTypeTable(context.getNamer().getRootPackageName());
    List<StaticLangApiMethodView> methods = generateApiMethods(apiMethodsContext);
    StaticLangRpcStubView.Builder stubClass = StaticLangRpcStubView.newBuilder();
    stubClass.doc(serviceTransformer.generateServiceDoc(context, null, productConfig));
    String name = namer.getApiRpcStubClassName(interfaceConfig.getInterfaceModel(), productConfig.getTransportProtocol());
    stubClass.releaseLevelAnnotation(namer.getReleaseAnnotation(ReleaseLevel.BETA));
    stubClass.name(name);
    stubClass.parentName(namer.getApiStubInterfaceName(interfaceConfig));
    stubClass.settingsClassName(getAndSaveNicknameForRootType(apiMethodsContext, namer.getApiSettingsClassName(interfaceConfig)));
    stubClass.stubSettingsClassName(getAndSaveNicknameForStubType(apiMethodsContext, namer.getApiStubSettingsClassName(interfaceConfig)));
    stubClass.callableFactoryClassName(getAndSaveNicknameForStubType(apiMethodsContext, namer.getCallableFactoryClassName(interfaceConfig, productConfig.getTransportProtocol())));
    stubClass.methodDescriptors(apiCallableTransformer.generateMethodDescriptors(apiMethodsContext));
    stubClass.apiCallables(apiCallableTransformer.generateStaticLangApiCallables(apiMethodsContext));
    stubClass.callableMethods(filterIncludeCallableMethods(methods));
    stubClass.hasDefaultInstance(interfaceConfig.hasDefaultInstance());
    stubClass.hasLongRunningOperations(interfaceConfig.hasLongRunningOperations());
    for (TypeAlias alias : apiMethodsContext.getImportTypeTable().getTypeTable().getAllImports().values()) {
        context.getImportTypeTable().getAndSaveNicknameFor(alias);
    }
    return stubClass.build();
}
Also used : InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) TypeAlias(com.google.api.codegen.util.TypeAlias) InterfaceContext(com.google.api.codegen.transformer.InterfaceContext) StaticLangRpcStubView(com.google.api.codegen.viewmodel.StaticLangRpcStubView) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Example 4 with InterfaceContext

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

the class JavaSurfaceTestTransformer method transform.

@Override
public List<ViewModel> transform(ApiModel model, GapicProductConfig productConfig) {
    SurfaceNamer namer = surfaceTransformer.createSurfaceNamer(productConfig);
    boolean enableStringFormatFunctions = productConfig.getResourceNameMessageConfigs().isEmpty();
    List<ViewModel> views = new ArrayList<>();
    for (InterfaceModel apiInterface : model.getInterfaces()) {
        ImportTypeTable typeTable = surfaceTransformer.createTypeTable(productConfig.getPackageName());
        InterfaceContext context = surfaceTransformer.createInterfaceContext(apiInterface, productConfig, namer, typeTable, enableStringFormatFunctions);
        views.add(createUnitTestFileView(context));
        if (context.getInterfaceConfig().getSmokeTestConfig() != null) {
            context = surfaceTransformer.createInterfaceContext(apiInterface, productConfig, namer, typeTable.cloneEmpty(), enableStringFormatFunctions);
            views.add(createSmokeTestClassView(context));
        }
    }
    for (InterfaceModel apiInterface : mockServiceTransformer.getGrpcInterfacesToMock(model, productConfig)) {
        ImportTypeTable typeTable = surfaceTransformer.createTypeTable(productConfig.getPackageName());
        InterfaceContext context = surfaceTransformer.createInterfaceContext(apiInterface, productConfig, namer, typeTable, enableStringFormatFunctions);
        views.add(createMockServiceImplFileView(context));
        context = surfaceTransformer.createInterfaceContext(apiInterface, productConfig, namer, typeTable.cloneEmpty(), enableStringFormatFunctions);
        views.add(createMockServiceView(context));
    }
    return views;
}
Also used : InterfaceModel(com.google.api.codegen.config.InterfaceModel) ImportTypeTable(com.google.api.codegen.transformer.ImportTypeTable) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) InterfaceContext(com.google.api.codegen.transformer.InterfaceContext) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Example 5 with InterfaceContext

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

the class JavaSurfaceTransformer method generateStubInterface.

private StaticLangStubInterfaceView generateStubInterface(InterfaceContext context, GapicProductConfig productConfig) {
    InterfaceConfig interfaceConfig = context.getInterfaceConfig();
    addStubInterfaceImports(context);
    // Stub class has different default package name from methods classes.
    InterfaceContext apiMethodsContext = context.withNewTypeTable(context.getNamer().getRootPackageName());
    List<StaticLangApiMethodView> methods = generateApiMethods(apiMethodsContext);
    for (TypeAlias alias : apiMethodsContext.getImportTypeTable().getTypeTable().getAllImports().values()) {
        context.getImportTypeTable().getAndSaveNicknameFor(alias);
    }
    StaticLangStubInterfaceView.Builder stubInterface = StaticLangStubInterfaceView.newBuilder();
    stubInterface.doc(serviceTransformer.generateServiceDoc(context, null, productConfig));
    String name = context.getNamer().getApiStubInterfaceName(context.getInterfaceConfig());
    stubInterface.releaseLevelAnnotation(context.getNamer().getReleaseAnnotation(packageMetadataConfig.releaseLevel(TargetLanguage.JAVA)));
    stubInterface.name(name);
    stubInterface.callableMethods(filterIncludeCallableMethods(methods));
    stubInterface.hasLongRunningOperations(interfaceConfig.hasLongRunningOperations());
    return stubInterface.build();
}
Also used : InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) StaticLangStubInterfaceView(com.google.api.codegen.viewmodel.StaticLangStubInterfaceView) TypeAlias(com.google.api.codegen.util.TypeAlias) InterfaceContext(com.google.api.codegen.transformer.InterfaceContext)

Aggregations

InterfaceContext (com.google.api.codegen.transformer.InterfaceContext)5 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)4 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)4 InterfaceModel (com.google.api.codegen.config.InterfaceModel)3 ViewModel (com.google.api.codegen.viewmodel.ViewModel)3 ArrayList (java.util.ArrayList)3 InterfaceConfig (com.google.api.codegen.config.InterfaceConfig)2 ImportTypeTable (com.google.api.codegen.transformer.ImportTypeTable)2 TypeAlias (com.google.api.codegen.util.TypeAlias)2 StaticLangRpcStubView (com.google.api.codegen.viewmodel.StaticLangRpcStubView)2 StaticLangStubInterfaceView (com.google.api.codegen.viewmodel.StaticLangStubInterfaceView)2 ApiModel (com.google.api.codegen.config.ApiModel)1 FieldConfig (com.google.api.codegen.config.FieldConfig)1 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)1 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 MethodModel (com.google.api.codegen.config.MethodModel)1 PageStreamingConfig (com.google.api.codegen.config.PageStreamingConfig)1 GapicCodePathMapper (com.google.api.codegen.gapic.GapicCodePathMapper)1 FileHeaderTransformer (com.google.api.codegen.transformer.FileHeaderTransformer)1