Search in sources :

Example 16 with MethodContext

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

the class CSharpGapicClientTransformer method generateModifyMethods.

private List<ModifyMethodView> generateModifyMethods(GapicInterfaceContext context) {
    SurfaceNamer namer = context.getNamer();
    List<ModifyMethodView> modifyMethods = new ArrayList<>();
    Set<String> modifyTypeNames = new HashSet<>();
    for (MethodModel method : csharpCommonTransformer.getSupportedMethods(context)) {
        MethodContext methodContext = context.asRequestMethodContext(method);
        String inputTypeFullName = methodContext.getMethodModel().getInputFullName();
        if (modifyTypeNames.contains(inputTypeFullName)) {
            continue;
        }
        modifyTypeNames.add(inputTypeFullName);
        MethodConfig methodConfig = methodContext.getMethodConfig();
        ModifyMethodView.Builder builder = ModifyMethodView.builder();
        builder.name(namer.getModifyMethodName(methodContext));
        builder.requestTypeName(method.getAndSaveRequestTypeName(context.getImportTypeTable(), context.getNamer()));
        builder.grpcStreamingType(methodConfig.getGrpcStreamingType());
        modifyMethods.add(builder.build());
    }
    return modifyMethods;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodModel(com.google.api.codegen.config.MethodModel) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.config.MethodContext) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) ModifyMethodView(com.google.api.codegen.viewmodel.ModifyMethodView) HashSet(java.util.HashSet)

Example 17 with MethodContext

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

the class OutputTransformer method arrayLoopView.

private OutputView.ArrayLoopView arrayLoopView(ResponseStatementProto.LoopStatement loop, MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext) {
    ScopeTable scope = outputContext.scopeTable();
    String loopVariable = loop.getVariable();
    assertIdentifierNotUsed(loopVariable, methodContext, sampleContext);
    OutputView.VariableView accessor = accessorNewVariable(new Scanner(loop.getCollection()), methodContext, sampleContext, scope, loopVariable, true);
    return OutputView.ArrayLoopView.newBuilder().variableType(scope.getTypeName(loopVariable)).variableName(methodContext.getNamer().localVarName(Name.from(loopVariable))).collection(accessor).body(loop.getBodyList().stream().map(body -> toView(body, methodContext, sampleContext, outputContext)).collect(ImmutableList.toImmutableList())).build();
}
Also used : IntStream(java.util.stream.IntStream) FieldModel(com.google.api.codegen.config.FieldModel) ResponseStatementProto(com.google.api.codegen.samplegen.v1p2.ResponseStatementProto) HashMap(java.util.HashMap) HashSet(java.util.HashSet) LongRunningConfig(com.google.api.codegen.config.LongRunningConfig) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) Map(java.util.Map) OutputContext(com.google.api.codegen.config.OutputContext) SampleConfig(com.google.api.codegen.config.SampleConfig) Nullable(javax.annotation.Nullable) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleValueSet(com.google.api.codegen.SampleValueSet) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) Set(java.util.Set) TypeModel(com.google.api.codegen.config.TypeModel) Scanner(com.google.api.codegen.util.Scanner) Name(com.google.api.codegen.util.Name) List(java.util.List) OutputSpec(com.google.api.codegen.OutputSpec) SampleSpec(com.google.api.codegen.config.SampleSpec) Preconditions(com.google.common.base.Preconditions) OutputView(com.google.api.codegen.viewmodel.OutputView) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) Scanner(com.google.api.codegen.util.Scanner) OutputView(com.google.api.codegen.viewmodel.OutputView)

Example 18 with MethodContext

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

the class MockServiceTransformer method createMockGrpcMethodViews.

public List<MockGrpcMethodView> createMockGrpcMethodViews(InterfaceContext context) {
    if (!context.getProductConfig().getTransportProtocol().equals(TransportProtocol.GRPC)) {
        return ImmutableList.of();
    }
    List<? extends MethodModel> methods = context.getInterfaceMethods();
    ArrayList<MockGrpcMethodView> mocks = new ArrayList<>(methods.size());
    for (MethodModel method : methods) {
        if (context.getMethodConfig(method) == null || !context.isSupported(method)) {
            continue;
        }
        MethodContext methodContext = context.asRequestMethodContext(method);
        String requestTypeName = method.getAndSaveRequestTypeName(methodContext.getTypeTable(), methodContext.getNamer());
        String responseTypeName = method.getAndSaveResponseTypeName(methodContext.getTypeTable(), methodContext.getNamer());
        MethodConfig methodConfig = methodContext.getMethodConfig();
        mocks.add(MockGrpcMethodView.newBuilder().name(methodContext.getNamer().getApiMethodName(method, VisibilityConfig.PUBLIC)).requestTypeName(requestTypeName).responseTypeName(responseTypeName).grpcStreamingType(methodConfig.getGrpcStreamingType()).streamHandleTypeName(methodContext.getNamer().getStreamingServerName(method)).build());
    }
    return mocks;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodModel(com.google.api.codegen.config.MethodModel) MockGrpcMethodView(com.google.api.codegen.viewmodel.testing.MockGrpcMethodView) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.config.MethodContext)

Example 19 with MethodContext

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

the class DynamicLangGapicSamplesTransformer 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 = methodContext.getMethodModel().getRequestStreaming() ? InitCodeOutputType.SingleObject : InitCodeOutputType.FieldList;
                    SampleContext sampleContext = SampleContext.newBuilder().uniqueSampleId(registry.getUniqueSampleId(sampleConfig, form)).sampleType(SampleSpec.SampleType.STANDALONE).callingForm(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 20 with MethodContext

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

the class JavaSurfaceTestTransformer method createTestCaseViews.

private List<TestCaseView> createTestCaseViews(InterfaceContext context) {
    ArrayList<TestCaseView> testCaseViews = new ArrayList<>();
    SymbolTable testNameTable = new SymbolTable();
    for (MethodModel method : context.getSupportedMethods()) {
        MethodConfig methodConfig = context.getMethodConfig(method);
        if (methodConfig.isGrpcStreaming()) {
            if (methodConfig.getGrpcStreamingType() == GrpcStreamingType.ClientStreaming) {
                // Issue: https://github.com/googleapis/toolkit/issues/946
                continue;
            }
            addGrpcStreamingTestImports(context, methodConfig.getGrpcStreamingType());
            MethodContext methodContext = context.asRequestMethodContext(method);
            InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext(methodContext, new SymbolTable(), methodConfig.getRequiredFieldConfigs(), InitCodeOutputType.SingleObject, valueGenerator);
            testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, ClientMethodType.CallableMethod));
        } else if (methodConfig.isFlattening()) {
            MethodContext defaultMethodContext = context.asRequestMethodContext(method);
            for (FlatteningConfig flatteningGroup : FlatteningConfigs.getRepresentativeFlatteningConfigs(FlatteningConfig.withRepeatedResourceInSampleOnly(methodConfig.getFlatteningConfigs()))) {
                MethodContext methodContext = context.asFlattenedMethodContext(defaultMethodContext, flatteningGroup);
                ClientMethodType clientMethodType = getClientMethodType(methodContext);
                InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext(methodContext, new SymbolTable(), flatteningGroup.getFlattenedFieldConfigs().values(), InitCodeOutputType.FieldList, valueGenerator);
                testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType, Synchronicity.Sync, null, null, TargetLanguage.JAVA));
            }
        } else {
            MethodContext methodContext = context.asRequestMethodContext(method);
            ClientMethodType clientMethodType = getClientMethodType(methodContext);
            InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext(methodContext, new SymbolTable(), methodConfig.getRequiredFieldConfigs(), InitCodeOutputType.SingleObject, valueGenerator);
            TestCaseView testCaseView = testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType, Synchronicity.Sync, null, null, TargetLanguage.JAVA);
            testCaseViews.add(testCaseView);
        }
    }
    return testCaseViews;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodModel(com.google.api.codegen.config.MethodModel) ClientMethodType(com.google.api.codegen.viewmodel.ClientMethodType) TestCaseView(com.google.api.codegen.viewmodel.testing.TestCaseView) ArrayList(java.util.ArrayList) SymbolTable(com.google.api.codegen.util.SymbolTable) MethodContext(com.google.api.codegen.config.MethodContext) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Aggregations

MethodContext (com.google.api.codegen.config.MethodContext)21 ArrayList (java.util.ArrayList)14 MethodConfig (com.google.api.codegen.config.MethodConfig)13 MethodModel (com.google.api.codegen.config.MethodModel)12 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)9 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)8 InterfaceContext (com.google.api.codegen.config.InterfaceContext)7 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)7 ImmutableList (com.google.common.collect.ImmutableList)7 List (java.util.List)7 HashMap (java.util.HashMap)6 FieldConfig (com.google.api.codegen.config.FieldConfig)5 GapicMethodContext (com.google.api.codegen.config.GapicMethodContext)5 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)5 CallingForm (com.google.api.codegen.viewmodel.CallingForm)5 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)4 SampleConfig (com.google.api.codegen.config.SampleConfig)4 SampleContext (com.google.api.codegen.config.SampleContext)4 Name (com.google.api.codegen.util.Name)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4