Search in sources :

Example 1 with SchemaTypeTable

use of com.google.api.codegen.transformer.SchemaTypeTable 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 2 with SchemaTypeTable

use of com.google.api.codegen.transformer.SchemaTypeTable in project toolkit by googleapis.

the class JavaDiscoGapicRequestToViewTransformer method createTypeTable.

private SchemaTypeTable createTypeTable(String implicitPackageName) {
    JavaTypeTable typeTable = new JavaTypeTable(implicitPackageName, IGNORE_JAVA_LANG_CLASH);
    SchemaTypeNameConverter typeNameConverter = new JavaSchemaTypeNameConverter(implicitPackageName, nameFormatter);
    return new SchemaTypeTable(typeTable, typeNameConverter, new JavaSurfaceNamer(implicitPackageName, implicitPackageName));
}
Also used : SchemaTypeNameConverter(com.google.api.codegen.transformer.SchemaTypeNameConverter) JavaSchemaTypeNameConverter(com.google.api.codegen.transformer.java.JavaSchemaTypeNameConverter) JavaSchemaTypeNameConverter(com.google.api.codegen.transformer.java.JavaSchemaTypeNameConverter) JavaTypeTable(com.google.api.codegen.util.java.JavaTypeTable) SchemaTypeTable(com.google.api.codegen.transformer.SchemaTypeTable) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer)

Aggregations

SchemaTypeTable (com.google.api.codegen.transformer.SchemaTypeTable)2 FieldModel (com.google.api.codegen.config.FieldModel)1 SchemaTransformationContext (com.google.api.codegen.discogapic.SchemaTransformationContext)1 Schema (com.google.api.codegen.discovery.Schema)1 SchemaTypeNameConverter (com.google.api.codegen.transformer.SchemaTypeNameConverter)1 JavaSchemaTypeNameConverter (com.google.api.codegen.transformer.java.JavaSchemaTypeNameConverter)1 JavaSurfaceNamer (com.google.api.codegen.transformer.java.JavaSurfaceNamer)1 SymbolTable (com.google.api.codegen.util.SymbolTable)1 JavaTypeTable (com.google.api.codegen.util.java.JavaTypeTable)1 StaticLangApiMessageView (com.google.api.codegen.viewmodel.StaticLangApiMessageView)1 LinkedList (java.util.LinkedList)1