Search in sources :

Example 21 with FieldModel

use of com.google.api.codegen.config.FieldModel 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 22 with FieldModel

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

the class InitCodeNode method buildSubTrees.

private static List<InitCodeNode> buildSubTrees(InitCodeContext context) {
    List<InitCodeNode> subTrees = new ArrayList<>();
    if (context.initFieldConfigStrings() != null) {
        for (String initFieldConfigString : context.initFieldConfigStrings()) {
            subTrees.add(FieldStructureParser.parse(initFieldConfigString, context.initValueConfigMap()));
        }
    }
    if (context.initFields() != null) {
        // Add items in fieldSet to newSubTrees in case they were not included in
        // sampleCodeInitFields, and to ensure the order is determined by initFields
        List<InitCodeNode> newSubTrees = new ArrayList<>();
        for (FieldModel field : context.initFields()) {
            String nameString = field.getNameAsParameter();
            InitValueConfig initValueConfig = context.initValueConfigMap().get(nameString);
            if (initValueConfig == null) {
                newSubTrees.add(InitCodeNode.createWithName(nameString, field.getNameAsParameter()));
            } else {
                newSubTrees.add(InitCodeNode.createWithValue(nameString, initValueConfig));
            }
        }
        newSubTrees.addAll(subTrees);
        subTrees = newSubTrees;
    } else if (context.outputType() == InitCodeOutputType.FieldList) {
        throw new IllegalArgumentException("Init field array is not set for flattened method.");
    }
    if (context.additionalInitCodeNodes() != null) {
        subTrees.addAll(Lists.newArrayList(context.additionalInitCodeNodes()));
    }
    return subTrees;
}
Also used : ArrayList(java.util.ArrayList) FieldModel(com.google.api.codegen.config.FieldModel)

Example 23 with FieldModel

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

the class ApiCallableTransformer method generateApiCallableSettings.

public ApiCallSettingsView generateApiCallableSettings(MethodContext context, ApiCallableImplType callableImplType) {
    SurfaceNamer namer = context.getNamer();
    ImportTypeTable typeTable = context.getTypeTable();
    MethodModel method = context.getMethodModel();
    MethodConfig methodConfig = context.getMethodConfig();
    Map<String, RetryCodesDefinitionView> retryCodesByKey = new HashMap<>();
    for (RetryCodesDefinitionView retryCodes : retryDefinitionsTransformer.generateRetryCodesDefinitions(context.getSurfaceInterfaceContext())) {
        retryCodesByKey.put(retryCodes.key(), retryCodes);
    }
    Map<String, RetryParamsDefinitionView> retryParamsByKey = new HashMap<>();
    for (RetryParamsDefinitionView retryParams : retryDefinitionsTransformer.generateRetryParamsDefinitions(context.getSurfaceInterfaceContext())) {
        retryParamsByKey.put(retryParams.key(), retryParams);
    }
    ApiCallSettingsView.Builder settings = ApiCallSettingsView.newBuilder();
    settings.methodName(namer.getApiMethodName(method, VisibilityConfig.PUBLIC));
    settings.asyncMethodName(namer.getAsyncApiMethodName(method, VisibilityConfig.PUBLIC));
    settings.requestTypeName(method.getAndSaveRequestTypeName(context.getTypeTable(), context.getNamer()));
    settings.responseTypeName(method.getAndSaveResponseTypeName(context.getTypeTable(), context.getNamer()));
    settings.grpcTypeName(typeTable.getAndSaveNicknameFor(context.getGrpcContainerTypeName()));
    settings.grpcMethodConstant(namer.getGrpcMethodConstant(method));
    settings.retryCodesName(methodConfig.getRetryCodesConfigName());
    settings.retryCodesView(retryCodesByKey.get(methodConfig.getRetryCodesConfigName()));
    settings.retryParamsName(methodConfig.getRetrySettingsConfigName());
    settings.retryParamsView(retryParamsByKey.get(methodConfig.getRetrySettingsConfigName()));
    settings.visibility(namer.getVisiblityKeyword(methodConfig.getVisibility()));
    String notImplementedPrefix = "ApiCallableTransformer.generateApiCallableSettings - ";
    settings.resourceTypeName(namer.getNotImplementedString(notImplementedPrefix + "resourceTypeName"));
    settings.pagedListResponseTypeName(namer.getNotImplementedString(notImplementedPrefix + "pagedListResponseTypeName"));
    settings.pageStreamingDescriptorName(namer.getNotImplementedString(notImplementedPrefix + "pageStreamingDescriptorName"));
    settings.pagedListResponseFactoryName(namer.getNotImplementedString(notImplementedPrefix + "pagedListResponseFactoryName"));
    settings.batchingDescriptorName(namer.getNotImplementedString(notImplementedPrefix + "batchingDescriptorName"));
    settings.memberName(namer.getSettingsMemberName(method));
    settings.settingsGetFunction(namer.getSettingsFunctionName(method));
    settings.type(callableImplType);
    switch(callableImplType) {
        case BidiStreamingApiCallable:
        case ClientStreamingApiCallable:
        case ServerStreamingApiCallable:
            settings.type(ApiCallableImplType.of(methodConfig.getGrpcStreamingType()));
            if (methodConfig.getGrpcStreaming().hasResourceField()) {
                FieldModel resourceType = methodConfig.getGrpcStreaming().getResourcesField();
                settings.resourceTypeName(typeTable.getAndSaveNicknameForElementType(resourceType));
            }
            settings.grpcStreamingType(methodConfig.getGrpcStreaming().getType());
            break;
        case PagedApiCallable:
            settings.type(ApiCallableImplType.PagedApiCallable);
            FieldModel resourceField = methodConfig.getPageStreaming().getResourcesField();
            settings.resourceTypeName(typeTable.getAndSaveNicknameForElementType(resourceField));
            settings.pagedListResponseTypeName(namer.getAndSavePagedResponseTypeName(context, methodConfig.getPageStreaming().getResourcesFieldConfig()));
            settings.pageStreamingDescriptorName(namer.getPageStreamingDescriptorConstName(method));
            settings.pagedListResponseFactoryName(namer.getPagedListResponseFactoryConstName(method));
            break;
        case BatchingApiCallable:
            settings.type(ApiCallableImplType.BatchingApiCallable);
            settings.batchingDescriptorName(namer.getBatchingDescriptorConstName(method));
            settings.batchingConfig(batchingTransformer.generateBatchingConfig(context));
            break;
        case OperationApiCallable:
            settings.type(ApiCallableImplType.OperationApiCallable);
            settings.operationMethod(lroTransformer.generateDetailView(context));
            settings.memberName(namer.getOperationSettingsMemberName(method));
            settings.settingsGetFunction(namer.getOperationSettingsFunctionName(method));
            break;
        case SimpleApiCallable:
            break;
        default:
            throw new IllegalStateException("Unhandled ApiCallableImplType enum value: " + callableImplType);
    }
    return settings.build();
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) HashMap(java.util.HashMap) RetryCodesDefinitionView(com.google.api.codegen.viewmodel.RetryCodesDefinitionView) ApiCallSettingsView(com.google.api.codegen.viewmodel.ApiCallSettingsView) MethodConfig(com.google.api.codegen.config.MethodConfig) RetryParamsDefinitionView(com.google.api.codegen.viewmodel.RetryParamsDefinitionView) FieldModel(com.google.api.codegen.config.FieldModel)

Example 24 with FieldModel

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

the class BatchingTransformer method generateDiscriminatorFieldCopies.

private List<FieldCopyView> generateDiscriminatorFieldCopies(MethodContext context) {
    List<FieldCopyView> fieldCopies = new ArrayList<>();
    BatchingConfig batching = context.getMethodConfig().getBatching();
    for (GenericFieldSelector fieldSelector : batching.getDiscriminatorFields()) {
        FieldModel selectedType = fieldSelector.getLastField();
        FieldCopyView fieldCopy = FieldCopyView.newBuilder().fieldGetFunction(context.getNamer().getFieldGetFunctionName(selectedType)).fieldSetFunction(context.getNamer().getFieldSetFunctionName(selectedType)).build();
        fieldCopies.add(fieldCopy);
    }
    return fieldCopies;
}
Also used : FieldCopyView(com.google.api.codegen.viewmodel.FieldCopyView) ArrayList(java.util.ArrayList) BatchingConfig(com.google.api.codegen.config.BatchingConfig) GenericFieldSelector(com.google.api.codegen.config.GenericFieldSelector) FieldModel(com.google.api.codegen.config.FieldModel)

Example 25 with FieldModel

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

the class BatchingTransformer method generatePartitionKeys.

private List<BatchingPartitionKeyView> generatePartitionKeys(MethodContext context) {
    List<BatchingPartitionKeyView> keys = new ArrayList<>();
    BatchingConfig batching = context.getMethodConfig().getBatching();
    for (GenericFieldSelector fieldSelector : batching.getDiscriminatorFields()) {
        FieldModel selectedType = fieldSelector.getLastField();
        BatchingPartitionKeyView key = BatchingPartitionKeyView.newBuilder().fieldGetFunction(context.getNamer().getFieldGetFunctionName(selectedType)).build();
        keys.add(key);
    }
    return keys;
}
Also used : ArrayList(java.util.ArrayList) BatchingConfig(com.google.api.codegen.config.BatchingConfig) GenericFieldSelector(com.google.api.codegen.config.GenericFieldSelector) BatchingPartitionKeyView(com.google.api.codegen.viewmodel.BatchingPartitionKeyView) 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