Search in sources :

Example 11 with MethodContext

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

the class OutputTransformer method mapLoopView.

private OutputView.MapLoopView mapLoopView(ResponseStatementProto.LoopStatement loop, MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext) {
    outputContext.mapSpecs().add(loop);
    ScopeTable scope = outputContext.scopeTable();
    String key = loop.getKey();
    String value = loop.getValue();
    OutputView.VariableView mapVar = accessor(new Scanner(loop.getMap()), methodContext, sampleContext, scope);
    TypeModel keyType = mapVar.type().getMapKeyType();
    TypeModel valueType = mapVar.type().getMapValueType();
    String keyTypeName = methodContext.getTypeTable().getNicknameFor(keyType);
    String valueTypeName = methodContext.getTypeTable().getNicknameFor(valueType);
    if (!key.isEmpty()) {
        assertIdentifierNotUsed(key, methodContext, sampleContext);
        scope.put(key, keyType, keyTypeName);
    }
    if (!value.isEmpty()) {
        assertIdentifierNotUsed(value, methodContext, sampleContext);
        scope.put(value, valueType, valueTypeName);
    }
    return OutputView.MapLoopView.newBuilder().keyVariableName(methodContext.getNamer().localVarName(Name.anyLower(key))).keyType(keyTypeName).valueVariableName(methodContext.getNamer().localVarName(Name.anyLower(value))).valueType(valueTypeName).map(mapVar).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) TypeModel(com.google.api.codegen.config.TypeModel)

Example 12 with MethodContext

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

the class ApiCallableTransformer method generateHttpFields.

private HttpMethodView generateHttpFields(MethodContext context) {
    if (context.getProductConfig().getTransportProtocol().equals(TransportProtocol.HTTP)) {
        if (context.getMethodModel() instanceof DiscoveryMethodModel) {
            // This section is only for DiscoGapic and will be deleted once the generator stops
            // ingesting Discovery files.
            Method method = ((DiscoveryMethodModel) context.getMethodModel()).getDiscoMethod();
            HttpMethodView.Builder httpMethodView = HttpMethodView.newBuilder();
            httpMethodView.fullMethodName(method.id());
            httpMethodView.httpMethod(method.httpMethod());
            List<String> pathParams = new ArrayList<>(method.pathParams().keySet());
            List<String> queryParams = new ArrayList<>(method.queryParams().keySet());
            Collections.sort(pathParams);
            Collections.sort(queryParams);
            httpMethodView.pathParams(pathParams);
            httpMethodView.queryParams(queryParams);
            httpMethodView.pathTemplate(method.path());
            // TODO(andrealin): handle multiple resource names.
            DiscoGapicInterfaceConfig interfaceConfig = (DiscoGapicInterfaceConfig) context.getSurfaceInterfaceContext().getInterfaceConfig();
            SingleResourceNameConfig nameConfig = interfaceConfig.methodToResourceNameMap().get(context.getMethodConfig());
            httpMethodView.resourceNameTypeName(context.getNamer().publicClassName(DiscoGapicParser.getResourceNameName(nameConfig)));
            // Find the field with the resource name config.
            for (FieldConfig fieldConfig : context.getMethodConfig().getRequiredFieldConfigs()) {
                if (fieldConfig.getResourceNameConfig() != null && fieldConfig.getResourceNameConfig().equals(nameConfig)) {
                    httpMethodView.resourceNameFieldName(context.getNamer().privateFieldName(Name.anyCamel(fieldConfig.getField().getNameAsParameter())));
                }
            }
            return httpMethodView.build();
        } else if (context.getMethodModel() instanceof ProtoMethodModel) {
            com.google.api.tools.framework.model.Method method = ((ProtoMethodModel) context.getMethodModel()).getProtoMethod();
            HttpAttribute httpAttr = method.getAttribute(HttpAttribute.KEY);
            HttpMethodView.Builder httpMethodView = HttpMethodView.newBuilder();
            httpMethodView.httpMethod(httpAttr.getMethodKind().toString());
            httpMethodView.fullMethodName(httpAttr.getRestMethod().getFullName());
            SurfaceNamer namer = context.getNamer();
            httpMethodView.pathTemplate(httpAttr.getPath().stream().map(pathSegment -> normalizePathSegment(pathSegment.toString())).collect(Collectors.joining("/", "/", "")));
            httpMethodView.pathParamSelectors(populateMethodSelectors(namer, httpAttr.getPathSelectors()));
            httpMethodView.queryParamSelectors(populateMethodSelectors(namer, httpAttr.getParamSelectors()));
            httpMethodView.bodySelectors(populateMethodSelectors(namer, httpAttr.getBodySelectors()));
            return httpMethodView.build();
        }
    }
    return null;
}
Also used : ApiCallableImplType(com.google.api.codegen.viewmodel.ApiCallableImplType) FieldModel(com.google.api.codegen.config.FieldModel) RetryParamsDefinitionView(com.google.api.codegen.viewmodel.RetryParamsDefinitionView) Field(com.google.api.tools.framework.model.Field) GapicMethodContext(com.google.api.codegen.config.GapicMethodContext) ServiceMethodType(com.google.api.codegen.viewmodel.ServiceMethodType) RetryCodesDefinitionView(com.google.api.codegen.viewmodel.RetryCodesDefinitionView) DiscoGapicInterfaceConfig(com.google.api.codegen.config.DiscoGapicInterfaceConfig) HashMap(java.util.HashMap) ProtoField(com.google.api.codegen.config.ProtoField) ApiCallableView(com.google.api.codegen.viewmodel.ApiCallableView) LongRunningOperationDetailView(com.google.api.codegen.viewmodel.LongRunningOperationDetailView) ArrayList(java.util.ArrayList) InterfaceContext(com.google.api.codegen.config.InterfaceContext) ProtoMethodModel(com.google.api.codegen.config.ProtoMethodModel) DiscoGapicParser(com.google.api.codegen.discogapic.transformer.DiscoGapicParser) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) MethodModel(com.google.api.codegen.config.MethodModel) TransportProtocol(com.google.api.codegen.config.TransportProtocol) Map(java.util.Map) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) ApiCallSettingsView(com.google.api.codegen.viewmodel.ApiCallSettingsView) MethodConfig(com.google.api.codegen.config.MethodConfig) HttpAttribute(com.google.api.tools.framework.aspects.http.model.HttpAttribute) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) HttpMethodView(com.google.api.codegen.viewmodel.HttpMethodView) HttpMethodSelectorView(com.google.api.codegen.viewmodel.HttpMethodSelectorView) Collectors(java.util.stream.Collectors) FieldSelector(com.google.api.tools.framework.model.FieldSelector) Name(com.google.api.codegen.util.Name) List(java.util.List) MethodDescriptorView(com.google.api.codegen.viewmodel.MethodDescriptorView) PageStreamingConfig(com.google.api.codegen.config.PageStreamingConfig) Method(com.google.api.codegen.discovery.Method) Collections(java.util.Collections) FieldConfig(com.google.api.codegen.config.FieldConfig) VisibilityConfig(com.google.api.codegen.config.VisibilityConfig) HttpAttribute(com.google.api.tools.framework.aspects.http.model.HttpAttribute) ProtoMethodModel(com.google.api.codegen.config.ProtoMethodModel) FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList) HttpMethodView(com.google.api.codegen.viewmodel.HttpMethodView) DiscoGapicInterfaceConfig(com.google.api.codegen.config.DiscoGapicInterfaceConfig) Method(com.google.api.codegen.discovery.Method) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 13 with MethodContext

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

the class JavaDiscoGapicRequestToViewTransformer method getRequestObjectParams.

private RequestObjectParamView getRequestObjectParams(DiscoGapicInterfaceContext context, MethodModel method) {
    MethodConfig methodConfig = context.getMethodConfig(method);
    // Generate the ResourceName methods.
    if (methodConfig.isFlattening()) {
        for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
            DiscoGapicMethodContext defaultMethodContext = context.asRequestMethodContext(method);
            MethodContext flattenedMethodContext = context.asFlattenedMethodContext(defaultMethodContext, flatteningGroup);
            if (FlatteningConfig.hasAnyRepeatedResourceNameParameter(flatteningGroup)) {
                flattenedMethodContext = flattenedMethodContext.withResourceNamesInSamplesOnly();
            }
            Iterable<FieldConfig> fieldConfigs = flattenedMethodContext.getFlatteningConfig().getFlattenedFieldConfigs().values();
            for (FieldConfig fieldConfig : fieldConfigs) {
                if (context.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
                    return resourceObjectTransformer.generateRequestObjectParam(flattenedMethodContext, fieldConfig);
                }
            }
        }
    }
    return null;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) FieldConfig(com.google.api.codegen.config.FieldConfig) DiscoGapicMethodContext(com.google.api.codegen.config.DiscoGapicMethodContext) MethodContext(com.google.api.codegen.config.MethodContext) DiscoGapicMethodContext(com.google.api.codegen.config.DiscoGapicMethodContext) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig)

Example 14 with MethodContext

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

the class RubyGapicSurfaceTestTransformer method createUnitTestCaseInitCodeContext.

private InitCodeContext createUnitTestCaseInitCodeContext(GapicInterfaceContext context, MethodModel method) {
    MethodContext requestMethodContext = context.asRequestMethodContext(method);
    MethodContext dynamicMethodContext = context.asRequestMethodContext(method);
    MethodConfig methodConfig = requestMethodContext.getMethodConfig();
    Collection<FieldConfig> fieldConfigs = methodConfig.getRequiredFieldConfigs();
    InitCodeOutputType outputType = method.getRequestStreaming() ? InitCodeOutputType.SingleObject : InitCodeOutputType.FieldList;
    return InitCodeContext.newBuilder().initObjectType(method.getInputType()).suggestedName(Name.from("request")).initFieldConfigStrings(methodConfig.getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(dynamicMethodContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).outputType(outputType).fieldConfigMap(FieldConfig.toFieldConfigMap(fieldConfigs)).build();
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) FieldConfig(com.google.api.codegen.config.FieldConfig) GapicMethodContext(com.google.api.codegen.config.GapicMethodContext) MethodContext(com.google.api.codegen.config.MethodContext) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)

Example 15 with MethodContext

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

the class RubyGapicSurfaceTestTransformer method createSmokeTestClassView.

private SmokeTestClassView createSmokeTestClassView(GapicInterfaceContext context) {
    boolean packageHasMultipleServices = context.getApiModel().hasMultipleServices();
    String outputPath = smokeTestPathMapper.getOutputPath(context.getInterfaceModel().getFullName(), context.getProductConfig());
    SurfaceNamer namer = context.getNamer();
    RubyPackageMetadataNamer metadataNamer = new RubyPackageMetadataNamer(context.getNamer().getPackageName());
    String name = namer.getSmokeTestClassName(context.getInterfaceConfig());
    MethodModel method = context.getInterfaceConfig().getSmokeTestConfig().getMethod();
    MethodContext methodContext = context.asRequestMethodContext(method);
    TestCaseTransformer testCaseTransformer = new TestCaseTransformer(valueProducer, packageHasMultipleServices);
    FlatteningConfig flatteningGroup = testCaseTransformer.getSmokeTestFlatteningGroup(methodContext.getMethodConfig());
    GapicMethodContext flattenedMethodContext = context.asFlattenedMethodContext(methodContext, flatteningGroup);
    SmokeTestClassView.Builder testClass = SmokeTestClassView.newBuilder();
    OptionalArrayMethodView apiMethodView = createSmokeTestCaseApiMethodView(flattenedMethodContext, packageHasMultipleServices);
    testClass.apiSettingsClassName(namer.getApiSettingsClassName(context.getInterfaceConfig()));
    testClass.apiClassName(namer.getApiWrapperClassName(context.getInterfaceConfig()));
    testClass.name(name);
    testClass.outputPath(namer.getSourceFilePath(outputPath, name));
    testClass.templateFileName(SMOKE_TEST_TEMPLATE_FILE);
    testClass.apiMethod(apiMethodView);
    testClass.requireProjectId(testCaseTransformer.requireProjectIdInSmokeTest(apiMethodView.initCode(), context.getNamer()));
    testClass.projectIdVariableName(metadataNamer.getProjectVariable(true));
    testClass.apiVersion(packageConfig.apiVersion());
    FileHeaderView fileHeader = fileHeaderTransformer.generateFileHeader(context.getProductConfig(), importSectionTransformer.generateSmokeTestImportSection(context), namer);
    testClass.fileHeader(fileHeader);
    return testClass.build();
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) GapicMethodContext(com.google.api.codegen.config.GapicMethodContext) MethodContext(com.google.api.codegen.config.MethodContext) FileHeaderView(com.google.api.codegen.viewmodel.FileHeaderView) GapicMethodContext(com.google.api.codegen.config.GapicMethodContext) OptionalArrayMethodView(com.google.api.codegen.viewmodel.OptionalArrayMethodView) TestCaseTransformer(com.google.api.codegen.transformer.TestCaseTransformer) SmokeTestClassView(com.google.api.codegen.viewmodel.testing.SmokeTestClassView) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

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