Search in sources :

Example 1 with StaticLangApiMethodSnippetView

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

Aggregations

FieldConfig (com.google.api.codegen.config.FieldConfig)1 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)1 FlatteningConfigs (com.google.api.codegen.config.FlatteningConfigs)1 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)1 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)1 InterfaceContext (com.google.api.codegen.config.InterfaceContext)1 InterfaceModel (com.google.api.codegen.config.InterfaceModel)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 MethodContext (com.google.api.codegen.config.MethodContext)1 MethodModel (com.google.api.codegen.config.MethodModel)1 PageStreamingConfig (com.google.api.codegen.config.PageStreamingConfig)1 ProtoApiModel (com.google.api.codegen.config.ProtoApiModel)1 GapicCodePathMapper (com.google.api.codegen.gapic.GapicCodePathMapper)1 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)1 FileHeaderTransformer (com.google.api.codegen.transformer.FileHeaderTransformer)1 InitCodeTransformer (com.google.api.codegen.transformer.InitCodeTransformer)1 ModelToViewTransformer (com.google.api.codegen.transformer.ModelToViewTransformer)1 ParamWithSimpleDoc (com.google.api.codegen.transformer.ParamWithSimpleDoc)1 SampleTransformer (com.google.api.codegen.transformer.SampleTransformer)1 StandardImportSectionTransformer (com.google.api.codegen.transformer.StandardImportSectionTransformer)1