Search in sources :

Example 1 with SymbolTable

use of com.google.api.codegen.util.SymbolTable in project toolkit by googleapis.

the class JavaDiscoGapicRequestToViewTransformer method generateRequestClass.

private StaticLangApiMessageView generateRequestClass(SchemaTransformationContext context, MethodModel method, RequestObjectParamView resourceNameView) {
    StaticLangApiMessageView.Builder requestView = StaticLangApiMessageView.newBuilder();
    SymbolTable symbolTable = SymbolTable.fromSeed(reservedKeywords);
    String requestClassId = context.getNamer().privateFieldName(DiscoGapicParser.getRequestName(((DiscoveryMethodModel) method).getDiscoMethod()));
    String requestName = nameFormatter.privateFieldName(Name.anyCamel(symbolTable.getNewSymbol(requestClassId)));
    boolean hasRequiredProperties = false;
    requestView.name(requestName);
    requestView.description(method.getDescription());
    String requestTypeName = nameFormatter.publicClassName(Name.anyCamel(requestClassId));
    requestView.typeName(requestTypeName);
    requestView.innerTypeName(requestTypeName);
    List<StaticLangApiMessageView> properties = new LinkedList<>();
    // Add the standard query parameters.
    for (String param : STANDARD_QUERY_PARAMS.keySet()) {
        if (method.getInputField(param) != null) {
            continue;
        }
        StaticLangApiMessageView.Builder paramView = StaticLangApiMessageView.newBuilder();
        paramView.description(STANDARD_QUERY_PARAMS.get(param));
        paramView.name(symbolTable.getNewSymbol(param));
        paramView.typeName("String");
        paramView.innerTypeName("String");
        paramView.isRequired(false);
        paramView.canRepeat(false);
        paramView.fieldGetFunction(context.getNamer().getFieldGetFunctionName(DiscoGapicParser.stringToName(param), SurfaceNamer.MapType.NOT_MAP, SurfaceNamer.Cardinality.NOT_REPEATED));
        paramView.fieldSetFunction(context.getDiscoGapicNamer().getResourceSetterName(param, SurfaceNamer.Cardinality.NOT_REPEATED, context.getNamer()));
        paramView.properties(Collections.emptyList());
        paramView.isRequestMessage(false);
        paramView.hasRequiredProperties(false);
        properties.add(paramView.build());
    }
    for (FieldModel entry : method.getInputFields()) {
        if (entry.mayBeInResourceName()) {
            hasRequiredProperties = true;
            continue;
        }
        String parameterName = entry.getNameAsParameter();
        properties.add(schemaToParamView(context, entry, parameterName, symbolTable, EscapeName.ESCAPE_NAME));
        if (entry.isRequired()) {
            hasRequiredProperties = true;
        }
    }
    StaticLangApiMessageView.Builder paramView = StaticLangApiMessageView.newBuilder();
    Method discoMethod = ((DiscoveryMethodModel) method).getDiscoMethod();
    String resourceName = DiscoGapicParser.getResourceIdentifier(discoMethod.path()).toLowerCamel();
    StringBuilder description = new StringBuilder(discoMethod.parameters().get(resourceName).description());
    description.append(String.format("\nIt must have the format `%s`. ", discoMethod.path()));
    description.append(String.format("\\`{%s}\\` must start with a letter,\n", resourceName));
    description.append("and contain only letters (\\`[A-Za-z]\\`), numbers (\\`[0-9]\\`), dashes (\\`-\\`),\n" + "     * underscores (\\`_\\`), periods (\\`.\\`), tildes (\\`~\\`), plus (\\`+\\`) or percent\n" + "     * signs (\\`%\\`). It must be between 3 and 255 characters in length, and it\n" + "     * must not start with \\`\"goog\"\\`.");
    paramView.description(description.toString());
    paramView.name(symbolTable.getNewSymbol(resourceNameView.name()));
    paramView.typeName("String");
    paramView.innerTypeName("String");
    paramView.isRequired(true);
    paramView.canRepeat(false);
    paramView.fieldGetFunction(resourceNameView.getCallName());
    paramView.fieldSetFunction(resourceNameView.setCallName());
    paramView.properties(new LinkedList<>());
    paramView.isRequestMessage(false);
    paramView.hasRequiredProperties(false);
    properties.add(paramView.build());
    Collections.sort(properties);
    requestView.canRepeat(false);
    requestView.isRequired(true);
    requestView.properties(properties);
    requestView.hasRequiredProperties(hasRequiredProperties);
    requestView.isRequestMessage(true);
    requestView.pathAsResourceName(resourceNameView);
    Schema requestBodyDef = ((DiscoveryMethodModel) method).getDiscoMethod().request();
    if (requestBodyDef != null && !Strings.isNullOrEmpty(requestBodyDef.reference())) {
        FieldModel requestBody = DiscoveryField.create(requestBodyDef, context.getDocContext().getApiModel());
        requestView.requestBodyType(schemaToParamView(context, requestBody, DiscoGapicParser.getSchemaNameAsParameter(requestBodyDef).toLowerCamel(), symbolTable, EscapeName.NO_ESCAPE_NAME));
    }
    return requestView.build();
}
Also used : Schema(com.google.api.codegen.discovery.Schema) SymbolTable(com.google.api.codegen.util.SymbolTable) Method(com.google.api.codegen.discovery.Method) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) LinkedList(java.util.LinkedList) StaticLangApiMessageView(com.google.api.codegen.viewmodel.StaticLangApiMessageView) FieldModel(com.google.api.codegen.config.FieldModel)

Example 2 with SymbolTable

use of com.google.api.codegen.util.SymbolTable in project toolkit by googleapis.

the class CSharpGapicUnitTestTransformer method createFlattenedTestCase.

private TestCaseView createFlattenedTestCase(MethodContext methodContext, SymbolTable testNameTable, Iterable<FieldConfig> fieldConfigs, ClientMethodType clientMethodType, Synchronicity synchronicity) {
    InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext(methodContext, new SymbolTable(), fieldConfigs, InitCodeOutputType.FieldList, valueGenerator);
    InitCodeContext initCodeRequestObjectContext = InitCodeContext.newBuilder().initObjectType(methodContext.getMethodModel().getInputType()).symbolTable(new SymbolTable()).suggestedName(Name.from("expected_request")).initFieldConfigStrings(methodContext.getMethodConfig().getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(methodContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).fieldConfigMap(methodContext.getProductConfig().getDefaultResourceNameFieldConfigMap()).outputType(InitCodeOutputType.SingleObject).valueGenerator(valueGenerator).build();
    return testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType, synchronicity, initCodeRequestObjectContext);
}
Also used : SymbolTable(com.google.api.codegen.util.SymbolTable) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Example 3 with SymbolTable

use of com.google.api.codegen.util.SymbolTable in project toolkit by googleapis.

the class CSharpGapicUnitTestTransformer method createTestCaseViews.

private List<TestCaseView> createTestCaseViews(GapicInterfaceContext context) {
    ArrayList<TestCaseView> testCaseViews = new ArrayList<>();
    SymbolTable testNameTable = new SymbolTable();
    for (MethodModel method : context.getSupportedMethods()) {
        GapicMethodContext defaultMethodContext = context.asRequestMethodContext(method);
        MethodConfig methodConfig = context.getMethodConfig(method);
        if (methodConfig.isGrpcStreaming()) {
        // TODO: Add support for streaming methods
        } else if (methodConfig.isFlattening()) {
            ClientMethodType clientMethodTypeSync;
            ClientMethodType clientMethodTypeAsync;
            if (methodConfig.isPageStreaming()) {
                // TODO: Add support for page-streaming methods
                continue;
            } else if (defaultMethodContext.isLongRunningMethodContext()) {
                // TODO: Add support for LRO methods
                continue;
            } else {
                clientMethodTypeSync = ClientMethodType.FlattenedMethod;
                clientMethodTypeAsync = ClientMethodType.FlattenedAsyncCallSettingsMethod;
            }
            if (methodConfig.getRerouteToGrpcInterface() != null) {
                // TODO: Add support for rerouted methods
                continue;
            }
            GapicMethodContext requestContext = context.asRequestMethodContext(method);
            for (FlatteningConfig flatteningGroup : FlatteningConfigs.getRepresentativeFlatteningConfigs(methodConfig.getFlatteningConfigs())) {
                GapicMethodContext methodContext = context.asFlattenedMethodContext(defaultMethodContext, flatteningGroup);
                testCaseViews.add(createFlattenedTestCase(methodContext, requestContext, testNameTable, flatteningGroup.getFlattenedFieldConfigs().values(), clientMethodTypeSync, Synchronicity.Sync));
                testCaseViews.add(createFlattenedTestCase(methodContext, requestContext, testNameTable, flatteningGroup.getFlattenedFieldConfigs().values(), clientMethodTypeAsync, Synchronicity.Async));
            }
            testCaseViews.add(createRequestObjectTestCase(requestContext, methodConfig, testNameTable, Synchronicity.Sync));
            testCaseViews.add(createRequestObjectTestCase(requestContext, methodConfig, testNameTable, Synchronicity.Async));
        } else {
            if (methodConfig.isPageStreaming() || defaultMethodContext.isLongRunningMethodContext() || methodConfig.getRerouteToGrpcInterface() != null) {
                // TODO: Add support for page-streaming, LRO, and rerouted methods
                continue;
            }
            GapicMethodContext requestContext = context.asRequestMethodContext(method);
            testCaseViews.add(createRequestObjectTestCase(requestContext, methodConfig, testNameTable, Synchronicity.Sync));
            testCaseViews.add(createRequestObjectTestCase(requestContext, methodConfig, testNameTable, Synchronicity.Async));
        }
    }
    return testCaseViews;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodModel(com.google.api.codegen.config.MethodModel) GapicMethodContext(com.google.api.codegen.config.GapicMethodContext) 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) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig)

Example 4 with SymbolTable

use of com.google.api.codegen.util.SymbolTable in project toolkit by googleapis.

the class CSharpGapicUnitTestTransformer method createFlattenedTestCase.

private TestCaseView createFlattenedTestCase(MethodContext methodContext, GapicMethodContext requestContext, SymbolTable testNameTable, Collection<FieldConfig> fieldConfigs, ClientMethodType clientMethodType, Synchronicity synchronicity) {
    InitCodeContext initCodeContext = InitCodeContext.newBuilder().initObjectType(methodContext.getMethodModel().getInputType()).symbolTable(new SymbolTable()).suggestedName(Name.from("request")).initFieldConfigStrings(methodContext.getMethodConfig().getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(methodContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).fieldConfigMap(FieldConfig.toFieldConfigMap(fieldConfigs)).outputType(InitCodeOutputType.FieldList).valueGenerator(valueGenerator).build();
    InitCodeContext initCodeRequestObjectContext = InitCodeContext.newBuilder().initObjectType(requestContext.getMethodModel().getInputType()).symbolTable(new SymbolTable()).suggestedName(Name.from("expected_request")).initFieldConfigStrings(requestContext.getMethodConfig().getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(requestContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).fieldConfigMap(FieldConfig.toFieldConfigMap(fieldConfigs)).outputType(InitCodeOutputType.SingleObject).valueGenerator(valueGenerator).build();
    return testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType, synchronicity, initCodeRequestObjectContext, requestContext, null);
}
Also used : SymbolTable(com.google.api.codegen.util.SymbolTable) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Example 5 with SymbolTable

use of com.google.api.codegen.util.SymbolTable in project toolkit by googleapis.

the class TestCaseTransformer method createSmokeTestInitContext.

public InitCodeContext createSmokeTestInitContext(MethodContext context) {
    SmokeTestConfig testConfig = context.getInterfaceConfig().getSmokeTestConfig();
    InitCodeContext.InitCodeOutputType outputType;
    ImmutableMap<String, FieldConfig> fieldConfigMap;
    if (context.isFlattenedMethodContext()) {
        outputType = InitCodeContext.InitCodeOutputType.FieldList;
        fieldConfigMap = FieldConfig.toFieldConfigMap(context.getFlatteningConfig().getFlattenedFieldConfigs().values());
    } else {
        outputType = InitCodeContext.InitCodeOutputType.SingleObject;
        fieldConfigMap = FieldConfig.toFieldConfigMap(context.getMethodConfig().getRequiredFieldConfigs());
    }
    // Store project ID variable name into the symbol table since it is used
    // by the execute method as a parameter.
    SymbolTable table = new SymbolTable();
    table.getNewSymbol(Name.from(InitFieldConfig.PROJECT_ID_VARIABLE_NAME));
    InitCodeContext.Builder contextBuilder = InitCodeContext.newBuilder().initObjectType(testConfig.getMethod().getInputType()).suggestedName(Name.from("request")).outputType(outputType).initValueConfigMap(InitCodeTransformer.createCollectionMap(context)).initFieldConfigStrings(testConfig.getInitFieldConfigStrings()).symbolTable(table).fieldConfigMap(fieldConfigMap);
    if (context.isFlattenedMethodContext()) {
        contextBuilder.initFields(context.getFlatteningConfig().getFlattenedFields());
    }
    return contextBuilder.build();
}
Also used : InitFieldConfig(com.google.api.codegen.metacode.InitFieldConfig) FieldConfig(com.google.api.codegen.config.FieldConfig) SmokeTestConfig(com.google.api.codegen.config.SmokeTestConfig) SymbolTable(com.google.api.codegen.util.SymbolTable) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Aggregations

SymbolTable (com.google.api.codegen.util.SymbolTable)17 MethodModel (com.google.api.codegen.config.MethodModel)8 InitCodeContext (com.google.api.codegen.metacode.InitCodeContext)8 GapicMethodContext (com.google.api.codegen.config.GapicMethodContext)7 TestCaseView (com.google.api.codegen.viewmodel.testing.TestCaseView)7 ClientMethodType (com.google.api.codegen.viewmodel.ClientMethodType)5 ArrayList (java.util.ArrayList)5 FieldConfig (com.google.api.codegen.config.FieldConfig)4 Schema (com.google.api.codegen.discovery.Schema)4 LinkedList (java.util.LinkedList)4 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)3 StaticLangApiMessageView (com.google.api.codegen.viewmodel.StaticLangApiMessageView)3 DiscoveryField (com.google.api.codegen.config.DiscoveryField)2 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)2 MethodConfig (com.google.api.codegen.config.MethodConfig)2 Method (com.google.api.codegen.discovery.Method)2 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)2 ImmutableList (com.google.common.collect.ImmutableList)2 FieldModel (com.google.api.codegen.config.FieldModel)1 MethodContext (com.google.api.codegen.config.MethodContext)1