Search in sources :

Example 11 with FieldModel

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

the class TestCaseTransformer method createTestCaseView.

public TestCaseView createTestCaseView(MethodContext methodContext, SymbolTable testNameTable, InitCodeContext initCodeContext, ClientMethodType clientMethodType, Synchronicity synchronicity, InitCodeContext requestObjectInitCodeContext) {
    MethodModel method = methodContext.getMethodModel();
    MethodConfig methodConfig = methodContext.getMethodConfig();
    SurfaceNamer namer = methodContext.getNamer();
    ImportTypeTable typeTable = methodContext.getTypeTable();
    String clientMethodName;
    String responseTypeName;
    String callerResponseTypeName;
    String fullyQualifiedResponseTypeName = methodContext.getMethodModel().getOutputTypeName(typeTable).getFullName();
    if (methodConfig.isPageStreaming()) {
        clientMethodName = namer.getApiMethodName(method, methodConfig.getVisibility());
        responseTypeName = namer.getAndSavePagedResponseTypeName(methodContext, methodConfig.getPageStreaming().getResourcesFieldConfig());
        callerResponseTypeName = namer.getAndSaveCallerPagedResponseTypeName(methodContext, methodConfig.getPageStreaming().getResourcesFieldConfig());
    } else if (methodConfig.isLongRunningOperation()) {
        clientMethodName = namer.getLroApiMethodName(method, methodConfig.getVisibility());
        responseTypeName = methodContext.getTypeTable().getAndSaveNicknameFor(methodConfig.getLongRunningConfig().getReturnType());
        callerResponseTypeName = responseTypeName;
        fullyQualifiedResponseTypeName = methodContext.getTypeTable().getFullNameFor(methodConfig.getLongRunningConfig().getReturnType());
    } else if (clientMethodType == ClientMethodType.CallableMethod) {
        clientMethodName = namer.getCallableMethodName(method);
        responseTypeName = method.getAndSaveResponseTypeName(typeTable, namer);
        callerResponseTypeName = responseTypeName;
    } else {
        clientMethodName = synchronicity == Synchronicity.Sync ? namer.getApiMethodName(method, methodConfig.getVisibility()) : namer.getAsyncApiMethodName(method, methodConfig.getVisibility());
        responseTypeName = method.getAndSaveResponseTypeName(typeTable, namer);
        callerResponseTypeName = responseTypeName;
    }
    InitCodeView initCode = initCodeTransformer.generateInitCode(methodContext, initCodeContext);
    InitCodeView requestObjectInitCode = requestObjectInitCodeContext != null ? initCodeTransformer.generateInitCode(methodContext, requestObjectInitCodeContext) : null;
    boolean hasRequestParameters = initCode.lines().size() > 0;
    boolean hasReturnValue = !method.isOutputTypeEmpty();
    if (methodConfig.isLongRunningOperation()) {
        hasReturnValue = !methodConfig.getLongRunningConfig().getReturnType().isEmptyType();
    }
    InitCodeContext responseInitCodeContext = createResponseInitCodeContext(methodContext, initCodeContext.symbolTable());
    MockRpcResponseView mockRpcResponseView = createMockResponseView(methodContext, responseInitCodeContext);
    GrpcStreamingView grpcStreamingView = null;
    if (methodConfig.isGrpcStreaming()) {
        String resourceTypeName = null;
        String resourcesFieldGetterName = null;
        if (methodConfig.getGrpcStreaming().hasResourceField()) {
            FieldModel resourcesField = methodConfig.getGrpcStreaming().getResourcesField();
            resourceTypeName = methodContext.getTypeTable().getAndSaveNicknameForElementType(resourcesField);
            resourcesFieldGetterName = namer.getFieldGetFunctionName(resourcesField, Name.from(resourcesField.getSimpleName()));
        }
        grpcStreamingView = GrpcStreamingView.newBuilder().resourceTypeName(resourceTypeName).resourcesFieldGetterName(resourcesFieldGetterName).requestInitCodeList(createGrpcStreamingInitCodeViews(methodContext, initCodeContext, initCode)).responseInitCodeList(createGrpcStreamingInitCodeViews(methodContext, responseInitCodeContext, mockRpcResponseView.initCode())).build();
    }
    return TestCaseView.newBuilder().asserts(initCodeTransformer.generateRequestAssertViews(methodContext, initCodeContext)).clientMethodType(clientMethodType).grpcStreamingType(methodConfig.getGrpcStreamingType()).hasRequestParameters(hasRequestParameters).hasReturnValue(hasReturnValue).initCode(initCode).requestObjectInitCode(requestObjectInitCode).mockResponse(mockRpcResponseView).mockServiceVarName(namer.getMockServiceVarName(methodContext.getTargetInterface())).name(synchronicity == Synchronicity.Sync ? namer.getTestCaseName(testNameTable, method) : namer.getAsyncTestCaseName(testNameTable, method)).nameWithException(namer.getExceptionTestCaseName(testNameTable, method)).pageStreamingResponseViews(createPageStreamingResponseViews(methodContext)).grpcStreamingView(grpcStreamingView).requestTypeName(method.getAndSaveRequestTypeName(typeTable, namer)).responseTypeName(responseTypeName).callerResponseTypeName(callerResponseTypeName).fullyQualifiedRequestTypeName(method.getInputTypeName(typeTable).getFullName()).fullyQualifiedResponseTypeName(fullyQualifiedResponseTypeName).serviceConstructorName(namer.getApiWrapperClassConstructorName(methodContext.getInterfaceConfig())).fullyQualifiedServiceClassName(namer.getFullyQualifiedApiWrapperClassName(methodContext.getInterfaceConfig())).fullyQualifiedAliasedServiceClassName(namer.getTopLevelAliasedApiClassName((methodContext.getInterfaceConfig()), packageHasMultipleServices)).clientMethodName(clientMethodName).mockGrpcStubTypeName(namer.getMockGrpcServiceImplName(methodContext.getTargetInterface())).createStubFunctionName(namer.getCreateStubFunctionName(methodContext.getTargetInterface())).grpcStubCallString(namer.getGrpcStubCallString(methodContext.getTargetInterface(), method)).clientHasDefaultInstance(methodContext.getInterfaceConfig().hasDefaultInstance()).methodDescriptor(getMethodDescriptorName(methodContext)).grpcMethodName(synchronicity == Synchronicity.Sync ? namer.getGrpcMethodName(method) : namer.getAsyncGrpcMethodName(method)).build();
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodModel(com.google.api.codegen.config.MethodModel) MockRpcResponseView(com.google.api.codegen.viewmodel.testing.MockRpcResponseView) GrpcStreamingView(com.google.api.codegen.viewmodel.testing.GrpcStreamingView) FieldModel(com.google.api.codegen.config.FieldModel) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext) InitCodeView(com.google.api.codegen.viewmodel.InitCodeView)

Example 12 with FieldModel

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

the class TestCaseTransformer method createPageStreamingResponseViews.

private List<PageStreamingResponseView> createPageStreamingResponseViews(MethodContext methodContext) {
    MethodConfig methodConfig = methodContext.getMethodConfig();
    SurfaceNamer namer = methodContext.getNamer();
    List<PageStreamingResponseView> pageStreamingResponseViews = new ArrayList<>();
    if (!methodConfig.isPageStreaming()) {
        return pageStreamingResponseViews;
    }
    FieldConfig resourcesFieldConfig = methodConfig.getPageStreaming().getResourcesFieldConfig();
    FieldModel resourcesField = resourcesFieldConfig.getField();
    String resourceTypeName = methodContext.getTypeTable().getAndSaveNicknameForElementType(resourcesField);
    // Construct the list of function calls needed to retrieve paged resource from response object.
    ImmutableList.Builder<String> resourcesFieldGetFunctionList = new ImmutableList.Builder<>();
    for (FieldModel field : resourcesFieldConfig.getFieldPath()) {
        resourcesFieldGetFunctionList.add(namer.getFieldGetFunctionName(field));
    }
    pageStreamingResponseViews.add(PageStreamingResponseView.newBuilder().resourceTypeName(resourceTypeName).resourcesFieldGetterNames(resourcesFieldGetFunctionList.build()).resourcesIterateMethod(namer.getPagedResponseIterateMethod()).resourcesVarName(namer.localVarName(Name.from("resources"))).build());
    if (methodContext.getFeatureConfig().useResourceNameFormatOption(resourcesFieldConfig)) {
        resourceTypeName = methodContext.getNamer().getAndSaveElementResourceTypeName(methodContext.getTypeTable(), resourcesFieldConfig);
        resourcesFieldGetFunctionList = new ImmutableList.Builder<>();
        for (FieldModel field : resourcesFieldConfig.getFieldPath()) {
            resourcesFieldGetFunctionList.add(namer.getFieldGetFunctionName(methodContext.getFeatureConfig(), resourcesFieldConfig));
        }
        String expectedTransformFunction = null;
        if (methodContext.getFeatureConfig().useResourceNameConverters(resourcesFieldConfig)) {
            expectedTransformFunction = namer.getResourceTypeParseMethodName(methodContext.getTypeTable(), resourcesFieldConfig);
        }
        pageStreamingResponseViews.add(PageStreamingResponseView.newBuilder().resourceTypeName(resourceTypeName).resourcesFieldGetterNames(resourcesFieldGetFunctionList.build()).resourcesIterateMethod(namer.getPagedResponseIterateMethod(methodContext.getFeatureConfig(), resourcesFieldConfig)).expectedValueTransformFunction(expectedTransformFunction).resourcesVarName(namer.localVarName(Name.from("resource_names"))).build());
    }
    return pageStreamingResponseViews;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) InitFieldConfig(com.google.api.codegen.metacode.InitFieldConfig) FieldConfig(com.google.api.codegen.config.FieldConfig) ImmutableList(com.google.common.collect.ImmutableList) PageStreamingResponseView(com.google.api.codegen.viewmodel.testing.PageStreamingResponseView) ArrayList(java.util.ArrayList) FieldModel(com.google.api.codegen.config.FieldModel)

Example 13 with FieldModel

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

the class CSharpSurfaceNamer method getResourceNameFieldGetFunctionName.

@Override
public String getResourceNameFieldGetFunctionName(FieldConfig fieldConfig) {
    FieldModel type = fieldConfig.getField();
    String fieldName = fieldConfig.getField().getSimpleName();
    Name identifier = Name.from(fieldName);
    Name resourceName;
    if (type.isMap()) {
        return getNotImplementedString("SurfaceNamer.getResourceNameFieldGetFunctionName:map-type");
    }
    // Omit the identifier when the field is called name (or names for the repeated case)
    boolean requireIdentifier = !((type.isRepeated() && fieldName.toLowerCase().equals("names")) || (!type.isRepeated() && fieldName.toLowerCase().equals("name")));
    boolean requireAs = requireIdentifier || (fieldConfig.getResourceNameType() == ResourceNameType.ANY);
    boolean requirePlural = type.isRepeated();
    if (fieldConfig.getResourceNameType() == ResourceNameType.ANY) {
        resourceName = Name.from("resource_name");
    } else {
        resourceName = getResourceTypeNameObject(fieldConfig.getMessageResourceNameConfig());
    }
    Name name = Name.from();
    if (requireIdentifier) {
        name = name.join(identifier);
    }
    if (requireAs) {
        name = name.join("as");
    }
    String functionName = publicMethodName(name.join(resourceName));
    if (requirePlural) {
        functionName += "s";
    }
    return functionName;
}
Also used : FieldModel(com.google.api.codegen.config.FieldModel) TypeName(com.google.api.codegen.util.TypeName) Name(com.google.api.codegen.util.Name)

Example 14 with FieldModel

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

the class JavaSurfaceTransformer method generatePagedResponseWrapper.

private StaticLangPagedResponseView generatePagedResponseWrapper(MethodContext context, ImportTypeTable typeTable) {
    MethodModel method = context.getMethodModel();
    FieldModel resourceField = context.getMethodConfig().getPageStreaming().getResourcesField();
    StaticLangPagedResponseView.Builder pagedResponseWrapper = StaticLangPagedResponseView.newBuilder();
    String pagedResponseTypeName = context.getNamer().getPagedResponseTypeInnerName(method, typeTable, resourceField);
    pagedResponseWrapper.pagedResponseTypeName(pagedResponseTypeName);
    pagedResponseWrapper.pageTypeName(context.getNamer().getPageTypeInnerName(method, typeTable, resourceField));
    pagedResponseWrapper.fixedSizeCollectionTypeName(context.getNamer().getFixedSizeCollectionTypeInnerName(method, typeTable, resourceField));
    pagedResponseWrapper.requestTypeName(method.getAndSaveRequestTypeName(context.getTypeTable(), context.getNamer()));
    pagedResponseWrapper.responseTypeName(method.getAndSaveResponseTypeName(context.getTypeTable(), context.getNamer()));
    pagedResponseWrapper.resourceTypeName(typeTable.getAndSaveNicknameForElementType(resourceField));
    pagedResponseWrapper.iterateMethods(getIterateMethods(context));
    return pagedResponseWrapper.build();
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) StaticLangPagedResponseView(com.google.api.codegen.viewmodel.StaticLangPagedResponseView) FieldModel(com.google.api.codegen.config.FieldModel)

Example 15 with FieldModel

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

the class PhpApiMethodParamTransformer method getMethodParamDocs.

private List<ParamDocView> getMethodParamDocs(GapicMethodContext context, Iterable<FieldModel> fields) {
    if (context.getMethodModel().getRequestStreaming()) {
        return ImmutableList.of();
    }
    MethodConfig methodConfig = context.getMethodConfig();
    ImmutableList.Builder<ParamDocView> paramDocs = ImmutableList.builder();
    for (FieldModel field : fields) {
        SimpleParamDocView.Builder paramDoc = SimpleParamDocView.newBuilder();
        paramDoc.paramName(context.getNamer().getVariableName(field));
        paramDoc.typeName(context.getTypeTable().getAndSaveNicknameFor(field));
        ImmutableList.Builder<String> docLines = ImmutableList.builder();
        if (methodConfig.isPageStreaming() && methodConfig.getPageStreaming().hasPageSizeField() && field.equals(methodConfig.getPageStreaming().getPageSizeField())) {
            docLines.add("The maximum number of resources contained in the underlying API", "response. The API may return fewer values in a page, even if", "there are additional values to be retrieved.");
        } else if (methodConfig.isPageStreaming() && field.equals(methodConfig.getPageStreaming().getRequestTokenField())) {
            docLines.add("A page token is used to specify a page of values to be returned.", "If no page token is specified (the default), the first page", "of values will be returned. Any page token used here must have", "been generated by a previous call to the API.");
        } else {
            docLines.addAll(context.getNamer().getDocLines(field));
        }
        if (field.isEnum()) {
            // For enums, we alter the param type to int, and document where to find the relevant
            // const values
            String typeNameSingular = context.getTypeTable().getFullNameFor(field.getType().makeOptional());
            if (field.isRepeated()) {
                paramDoc.typeName("int[]");
            } else {
                paramDoc.typeName("int");
            }
            docLines.add("For allowed values, use constants defined on {@see " + typeNameSingular + "}");
        }
        paramDoc.lines(docLines.build());
        paramDocs.add(paramDoc.build());
    }
    return paramDocs.build();
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) SimpleParamDocView(com.google.api.codegen.viewmodel.SimpleParamDocView) ImmutableList(com.google.common.collect.ImmutableList) MapParamDocView(com.google.api.codegen.viewmodel.MapParamDocView) SimpleParamDocView(com.google.api.codegen.viewmodel.SimpleParamDocView) ParamDocView(com.google.api.codegen.viewmodel.ParamDocView) FieldModel(com.google.api.codegen.config.FieldModel)

Aggregations

FieldModel (com.google.api.codegen.config.FieldModel)44 MethodConfig (com.google.api.codegen.config.MethodConfig)13 ArrayList (java.util.ArrayList)13 ImmutableList (com.google.common.collect.ImmutableList)11 FieldConfig (com.google.api.codegen.config.FieldConfig)9 MethodModel (com.google.api.codegen.config.MethodModel)8 SimpleParamDocView (com.google.api.codegen.viewmodel.SimpleParamDocView)7 ParamDocView (com.google.api.codegen.viewmodel.ParamDocView)6 BatchingConfig (com.google.api.codegen.config.BatchingConfig)4 PageStreamingConfig (com.google.api.codegen.config.PageStreamingConfig)4 DynamicLangDefaultableParamView (com.google.api.codegen.viewmodel.DynamicLangDefaultableParamView)4 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)3 Name (com.google.api.codegen.util.Name)3 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)2 GenericFieldSelector (com.google.api.codegen.config.GenericFieldSelector)2 Schema (com.google.api.codegen.discovery.Schema)2 SymbolTable (com.google.api.codegen.util.SymbolTable)2 TypeName (com.google.api.codegen.util.TypeName)2 RequestObjectParamView (com.google.api.codegen.viewmodel.RequestObjectParamView)2 StaticLangApiMessageView (com.google.api.codegen.viewmodel.StaticLangApiMessageView)2