Search in sources :

Example 11 with SymbolTable

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

the class JavaDiscoGapicSchemaToViewTransformer method generateSchemaClasses.

private StaticLangApiMessageView generateSchemaClasses(Map<SchemaTransformationContext, StaticLangApiMessageView> messageViewAccumulator, DiscoGapicInterfaceContext documentContext, Schema schema) {
    FieldModel schemaModel = DiscoveryField.create(schema, documentContext.getApiModel());
    SchemaTypeTable schemaTypeTable = documentContext.getSchemaTypeTable().cloneEmpty();
    SchemaTransformationContext context = SchemaTransformationContext.create(schema.getIdentifier(), schemaTypeTable, documentContext);
    StaticLangApiMessageView.Builder schemaView = StaticLangApiMessageView.newBuilder();
    boolean hasRequiredProperties = false;
    // Child schemas cannot have the same symbols as parent schemas, but sibling schemas can have
    // the same symbols.
    SymbolTable symbolTableCopy = SymbolTable.fromSeed(reservedKeywords);
    String schemaId = Name.anyCamel(schema.getIdentifier()).toLowerCamel();
    String schemaName = nameFormatter.privateFieldName(Name.anyCamel(symbolTableCopy.getNewSymbol(schemaId)));
    schemaView.name(schemaName);
    schemaView.defaultValue(schema.defaultValue());
    schemaView.description(schema.description());
    schemaView.fieldGetFunction(context.getNamer().getFieldGetFunctionName(schemaModel));
    schemaView.fieldSetFunction(context.getNamer().getFieldSetFunctionName(schemaModel));
    schemaView.fieldAddFunction(context.getNamer().getFieldAddFunctionName(schemaModel));
    String schemaTypeName = schemaTypeTable.getAndSaveNicknameFor(schema);
    schemaView.typeName(schemaTypeName);
    if (schema.repeated() || schema.type() == Type.ARRAY) {
        schemaView.innerTypeName(schemaTypeTable.getInnerTypeNameFor(schema));
    } else {
        schemaView.innerTypeName(schemaTypeName);
    }
    // Generate a Schema view from each property.
    List<StaticLangApiMessageView> viewProperties = new LinkedList<>();
    List<Schema> schemaProperties = new LinkedList<>();
    schemaProperties.addAll(schema.properties().values());
    if (schema.items() != null) {
        schemaProperties.addAll(schema.items().properties().values());
    }
    for (Schema property : schemaProperties) {
        viewProperties.add(generateSchemaClasses(messageViewAccumulator, documentContext, property));
        if (!property.properties().isEmpty() || (property.items() != null)) {
            // Add non-primitive-type property to imports.
            schemaTypeTable.getAndSaveNicknameFor(property);
        }
        if (property.required()) {
            hasRequiredProperties = true;
        }
    }
    Collections.sort(viewProperties);
    schemaView.properties(viewProperties);
    schemaView.canRepeat(schema.repeated() || schema.type().equals(Type.ARRAY));
    schemaView.isRequired(schema.required());
    schemaView.isRequestMessage(false);
    schemaView.hasRequiredProperties(hasRequiredProperties);
    if (!schema.properties().isEmpty() || (schema.items() != null && !schema.items().properties().isEmpty())) {
        // This is a top-level Schema, so add it to list of file ViewModels for rendering.
        messageViewAccumulator.put(context, schemaView.build());
    }
    return schemaView.build();
}
Also used : SchemaTransformationContext(com.google.api.codegen.discogapic.SchemaTransformationContext) SchemaTypeTable(com.google.api.codegen.transformer.SchemaTypeTable) StaticLangApiMessageView(com.google.api.codegen.viewmodel.StaticLangApiMessageView) Schema(com.google.api.codegen.discovery.Schema) SymbolTable(com.google.api.codegen.util.SymbolTable) FieldModel(com.google.api.codegen.config.FieldModel) LinkedList(java.util.LinkedList)

Example 12 with SymbolTable

use of com.google.api.codegen.util.SymbolTable 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()) {
            ClientMethodType clientMethodType;
            if (methodConfig.isPageStreaming()) {
                clientMethodType = ClientMethodType.PagedFlattenedMethod;
            } else if (methodConfig.isLongRunningOperation()) {
                clientMethodType = ClientMethodType.AsyncOperationFlattenedMethod;
            } else {
                clientMethodType = ClientMethodType.FlattenedMethod;
            }
            for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
                MethodContext methodContext = context.asFlattenedMethodContext(method, flatteningGroup);
                InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext(methodContext, new SymbolTable(), flatteningGroup.getFlattenedFieldConfigs().values(), InitCodeOutputType.FieldList, valueGenerator);
                testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType));
            }
        } else {
            // TODO: Add support of non-flattening method
            // Github issue: https://github.com/googleapis/toolkit/issues/393
            System.err.println("Non-flattening method test is not supported yet for " + method.getSimpleName());
        }
    }
    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.transformer.MethodContext) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Example 13 with SymbolTable

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

the class NodeJSGapicSurfaceTestTransformer method createTestCaseViews.

private List<TestCaseView> createTestCaseViews(GapicInterfaceContext context) {
    ArrayList<TestCaseView> testCaseViews = new ArrayList<>();
    SymbolTable testNameTable = new SymbolTable();
    for (MethodModel method : context.getSupportedMethods()) {
        GapicMethodContext methodContext = context.asRequestMethodContext(method);
        if (methodContext.getMethodConfig().getGrpcStreamingType() == GrpcStreamingType.ClientStreaming) {
            // Issue: https://github.com/googleapis/toolkit/issues/946
            continue;
        }
        Iterable<FieldConfig> fieldConfigs = methodContext.getMethodConfig().getRequiredFieldConfigs();
        InitCodeContext initCodeContext = InitCodeContext.newBuilder().initObjectType(methodContext.getMethodModel().getInputType()).suggestedName(Name.from("request")).initFieldConfigStrings(methodContext.getMethodConfig().getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(methodContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).outputType(InitCodeOutputType.SingleObject).fieldConfigMap(FieldConfig.toFieldConfigMap(fieldConfigs)).valueGenerator(valueGenerator).build();
        testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, getMethodType(methodContext.getMethodConfig())));
    }
    return testCaseViews;
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) GapicMethodContext(com.google.api.codegen.transformer.GapicMethodContext) TestCaseView(com.google.api.codegen.viewmodel.testing.TestCaseView) FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList) SymbolTable(com.google.api.codegen.util.SymbolTable) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Example 14 with SymbolTable

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

the class PhpGapicSurfaceTestTransformer method createTestCaseViews.

private List<TestCaseView> createTestCaseViews(GapicInterfaceContext context) {
    ArrayList<TestCaseView> testCaseViews = new ArrayList<>();
    SymbolTable testNameTable = new SymbolTable();
    for (MethodModel method : context.getSupportedMethods()) {
        GapicMethodContext methodContext = context.asRequestMethodContext(method);
        if (methodContext.getMethodConfig().getGrpcStreamingType() == GrpcStreamingType.ClientStreaming) {
            // Issue: https://github.com/googleapis/toolkit/issues/946
            continue;
        }
        InitCodeOutputType initCodeOutputType = InitCodeOutputType.FieldList;
        if (methodContext.getMethodConfig().getGrpcStreamingType() == GrpcStreamingType.BidiStreaming) {
            initCodeOutputType = InitCodeOutputType.SingleObject;
        }
        ClientMethodType clientMethodType = ClientMethodType.OptionalArrayMethod;
        if (methodContext.getMethodConfig().isLongRunningOperation()) {
            clientMethodType = ClientMethodType.OperationOptionalArrayMethod;
        } else if (methodContext.getMethodConfig().isPageStreaming()) {
            clientMethodType = ClientMethodType.PagedOptionalArrayMethod;
        }
        Iterable<FieldConfig> fieldConfigs = methodContext.getMethodConfig().getRequiredFieldConfigs();
        InitCodeContext initCodeContext = InitCodeContext.newBuilder().initObjectType(methodContext.getMethodModel().getInputType()).suggestedName(Name.from("request")).initFieldConfigStrings(methodContext.getMethodConfig().getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(methodContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).outputType(initCodeOutputType).fieldConfigMap(FieldConfig.toFieldConfigMap(fieldConfigs)).valueGenerator(valueGenerator).build();
        testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType));
    }
    return testCaseViews;
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) GapicMethodContext(com.google.api.codegen.transformer.GapicMethodContext) ClientMethodType(com.google.api.codegen.viewmodel.ClientMethodType) TestCaseView(com.google.api.codegen.viewmodel.testing.TestCaseView) FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList) SymbolTable(com.google.api.codegen.util.SymbolTable) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)

Aggregations

SymbolTable (com.google.api.codegen.util.SymbolTable)14 MethodModel (com.google.api.codegen.config.MethodModel)8 InitCodeContext (com.google.api.codegen.metacode.InitCodeContext)7 GapicMethodContext (com.google.api.codegen.transformer.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 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)3 MethodConfig (com.google.api.codegen.config.MethodConfig)3 Schema (com.google.api.codegen.discovery.Schema)3 LinkedList (java.util.LinkedList)3 FieldModel (com.google.api.codegen.config.FieldModel)2 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)2 StaticLangApiMessageView (com.google.api.codegen.viewmodel.StaticLangApiMessageView)2 ImmutableList (com.google.common.collect.ImmutableList)2 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)1 SmokeTestConfig (com.google.api.codegen.config.SmokeTestConfig)1 SchemaTransformationContext (com.google.api.codegen.discogapic.SchemaTransformationContext)1 Method (com.google.api.codegen.discovery.Method)1