Search in sources :

Example 1 with InterfaceContext

use of com.google.api.codegen.config.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(productConfig)) {
        if (!productConfig.hasInterfaceConfig(apiInterface)) {
            continue;
        }
        ImportTypeTable typeTable = surfaceTransformer.createTypeTable(productConfig.getPackageName());
        InterfaceContext context = surfaceTransformer.createInterfaceContext(apiInterface, productConfig, namer, typeTable);
        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.config.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 2 with InterfaceContext

use of com.google.api.codegen.config.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 = javaApiMethodTransformer.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());
    stubClass.transportProtocol(productConfig.getTransportProtocol());
    if (productConfig.getTransportProtocol() == TransportProtocol.HTTP) {
        stubClass.callSettingsClassName("HttpJsonCallSettings");
        stubClass.stubCallableFactoryClassName("HttpJsonStubCallableFactory");
    } else {
        stubClass.callSettingsClassName("GrpcCallSettings");
        stubClass.stubCallableFactoryClassName("GrpcStubCallableFactory");
    }
    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.config.InterfaceContext) StaticLangRpcStubView(com.google.api.codegen.viewmodel.StaticLangRpcStubView) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Example 3 with InterfaceContext

use of com.google.api.codegen.config.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 (methodContext.isLongRunningMethodContext()) {
            if (methodConfig.isFlattening()) {
                List<FlatteningConfig> flatteningGroups = FlatteningConfigs.getRepresentativeFlatteningConfigs(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(methodContext, 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()) {
                List<FlatteningConfig> flatteningGroups = FlatteningConfigs.getRepresentativeFlatteningConfigs(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(methodContext, 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()) {
                List<FlatteningConfig> flatteningGroups = FlatteningConfigs.getRepresentativeFlatteningConfigs(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(methodContext, 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) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig) ArrayList(java.util.ArrayList) InterfaceContext(com.google.api.codegen.config.InterfaceContext) StaticLangApiMethodTransformer(com.google.api.codegen.transformer.StaticLangApiMethodTransformer) MethodContext(com.google.api.codegen.config.MethodContext) 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) SampleTransformer(com.google.api.codegen.transformer.SampleTransformer) FlatteningConfigs(com.google.api.codegen.config.FlatteningConfigs) CSharpAliasMode(com.google.api.codegen.util.csharp.CSharpAliasMode) StaticLangApiMethodSnippetView(com.google.api.codegen.viewmodel.StaticLangApiMethodSnippetView) SnippetsFileView(com.google.api.codegen.viewmodel.SnippetsFileView) GapicCodePathMapper(com.google.api.codegen.gapic.GapicCodePathMapper) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) CallingForm(com.google.api.codegen.viewmodel.CallingForm) Collection(java.util.Collection) FileHeaderTransformer(com.google.api.codegen.transformer.FileHeaderTransformer) File(java.io.File) GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) InitCodeTransformer(com.google.api.codegen.transformer.InitCodeTransformer) List(java.util.List) 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) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.config.MethodContext) MethodConfig(com.google.api.codegen.config.MethodConfig) StaticLangApiMethodSnippetView(com.google.api.codegen.viewmodel.StaticLangApiMethodSnippetView) ArrayList(java.util.ArrayList) List(java.util.List) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig)

Example 4 with InterfaceContext

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

the class StaticLangGapicSamplesTransformer method getSampleContexts.

public List<SampleContext> getSampleContexts(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig) {
    SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
    ImmutableTable<String, String, ImmutableList<SampleConfig>> sampleConfigTable = productConfig.getSampleConfigTable();
    ImmutableList.Builder<SampleContext> sampleContexts = ImmutableList.builder();
    // Loop through sample configs and and map each sample ID to its matching calling forms.
    // We need this information when we need to create, in a language-specific way, unique
    // sample ids when one sample id has multiple matching calling forms
    Map<String, List<CallingForm>> configsAndMatchingForms = new HashMap<>();
    for (InterfaceContext interfaceContext : interfaceContexts) {
        for (MethodModel method : interfaceContext.getSupportedMethods()) {
            MethodContext methodContext = interfaceContext.asRequestMethodContext(method);
            String interfaceName = interfaceContext.getInterfaceConfig().getInterfaceModel().getFullName();
            String methodName = method.getSimpleName();
            List<SampleConfig> sampleConfigs = sampleConfigTable.get(interfaceName, methodName);
            sampleConfigs = firstNonNull(sampleConfigs, ImmutableList.<SampleConfig>of());
            for (SampleConfig config : sampleConfigs) {
                List<CallingForm> allMatchingCallingForms = namer.getMatchingCallingForms(methodContext, config.callingPattern());
                List<CallingForm> existingForms = configsAndMatchingForms.get(config.id());
                if (existingForms == null) {
                    existingForms = new ArrayList<>();
                    configsAndMatchingForms.put(config.id(), existingForms);
                }
                existingForms.addAll(allMatchingCallingForms);
            }
        }
    }
    SampleFileRegistry registry = new SampleFileRegistry(namer, configsAndMatchingForms);
    for (InterfaceContext interfaceContext : interfaceContexts) {
        for (MethodModel method : interfaceContext.getSupportedMethods()) {
            MethodContext methodContext = interfaceContext.asRequestMethodContext(method);
            String interfaceName = interfaceContext.getInterfaceConfig().getInterfaceModel().getFullName();
            String methodName = method.getSimpleName();
            ImmutableList<SampleConfig> sampleConfigs = sampleConfigTable.get(interfaceName, methodName);
            if (sampleConfigs == null) {
                continue;
            }
            for (SampleConfig sampleConfig : sampleConfigs) {
                List<CallingForm> allMatchingCallingForms = configsAndMatchingForms.get(sampleConfig.id());
                for (CallingForm form : allMatchingCallingForms) {
                    InitCodeOutputType initCodeOutputType = InitCodeOutputType.SingleObject;
                    // In Java and C#, we need to handle flattening.
                    if (CallingForm.isFlattened(form)) {
                        if (!methodContext.getMethodConfig().isFlattening()) {
                            continue;
                        }
                        initCodeOutputType = InitCodeOutputType.FieldList;
                        methodContext = methodContext.getSurfaceInterfaceContext().asFlattenedMethodContext(methodContext, methodContext.getMethodConfig().getFlatteningConfigs().get(0));
                    }
                    SampleContext sampleContext = SampleContext.newBuilder().uniqueSampleId(registry.getUniqueSampleId(sampleConfig, form)).sampleType(SampleSpec.SampleType.STANDALONE).callingForm(form).clientMethodType(fromCallingForm(form)).sampleConfig(sampleConfig).initCodeOutputType(initCodeOutputType).methodContext(methodContext).build();
                    sampleContexts.add(sampleContext);
                }
            }
        }
    }
    return sampleContexts.build();
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) SampleConfig(com.google.api.codegen.config.SampleConfig) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 5 with InterfaceContext

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

the class StaticLangGapicSamplesTransformer method generateSamplesFromGapicConfigs.

private List<ViewModel> generateSamplesFromGapicConfigs(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig, SurfaceNamer namer) {
    List<MethodSampleView> allSamples = interfaceContexts.stream().flatMap(iface -> apiMethodTransformer.generateApiMethods(iface).stream()).flatMap(method -> method.samples().stream()).collect(ImmutableList.toImmutableList());
    SampleFileRegistry registry = new SampleFileRegistry(namer, allSamples);
    List<ViewModel> sampleFileViews = new ArrayList<>();
    for (InterfaceContext context : interfaceContexts) {
        List<StaticLangApiMethodView> methods = apiMethodTransformer.generateApiMethods(context);
        for (StaticLangApiMethodView method : methods) {
            for (MethodSampleView sample : method.samples()) {
                sampleFileViews.add(newSampleFileView(productConfig, context, registry.getSampleClassName(sample, method.name()), registry.getSampleFileName(sample, method.name()), method, sample));
            }
        }
    }
    return sampleFileViews;
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) Function(java.util.function.Function) ArrayList(java.util.ArrayList) InterfaceContext(com.google.api.codegen.config.InterfaceContext) SampleEntryPointView(com.google.api.codegen.viewmodel.SampleEntryPointView) MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) MethodModel(com.google.api.codegen.config.MethodModel) ViewModel(com.google.api.codegen.viewmodel.ViewModel) Map(java.util.Map) StaticLangSampleClassView(com.google.api.codegen.viewmodel.StaticLangSampleClassView) StaticLangFileView(com.google.api.codegen.viewmodel.StaticLangFileView) SampleConfig(com.google.api.codegen.config.SampleConfig) Nullable(javax.annotation.Nullable) GapicCodePathMapper(com.google.api.codegen.gapic.GapicCodePathMapper) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) Streams(com.google.common.collect.Streams) GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) List(java.util.List) Paths(java.nio.file.Paths) SampleSpec(com.google.api.codegen.config.SampleSpec) ImmutableTable(com.google.common.collect.ImmutableTable) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) ClientMethodType(com.google.api.codegen.viewmodel.ClientMethodType) MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext)

Aggregations

InterfaceContext (com.google.api.codegen.config.InterfaceContext)18 ArrayList (java.util.ArrayList)13 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)11 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)10 ViewModel (com.google.api.codegen.viewmodel.ViewModel)10 MethodContext (com.google.api.codegen.config.MethodContext)9 SampleContext (com.google.api.codegen.config.SampleContext)8 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 MethodModel (com.google.api.codegen.config.MethodModel)7 CallingForm (com.google.api.codegen.viewmodel.CallingForm)7 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)6 ProtoApiModel (com.google.api.codegen.config.ProtoApiModel)6 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)6 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)5 MethodConfig (com.google.api.codegen.config.MethodConfig)5 SampleConfig (com.google.api.codegen.config.SampleConfig)5 GapicCodePathMapper (com.google.api.codegen.gapic.GapicCodePathMapper)5 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)5 HashMap (java.util.HashMap)5