Search in sources :

Example 1 with ResourceNameMessageConfigs

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

the class PathTemplateTransformer method generateResourceProtos.

public List<ResourceProtoView> generateResourceProtos(GapicInterfaceContext context) {
    SurfaceNamer namer = context.getNamer();
    ResourceNameMessageConfigs resourceConfigs = context.getProductConfig().getResourceNameMessageConfigs();
    ListMultimap<String, FieldModel> fieldsByMessage = resourceConfigs.getFieldsWithResourceNamesByMessage();
    Map<String, FieldConfig> fieldConfigMap = context.getProductConfig().getDefaultResourceNameFieldConfigMap();
    List<ResourceProtoView> protos = new ArrayList<>();
    for (Entry<String, Collection<FieldModel>> entry : fieldsByMessage.asMap().entrySet()) {
        String msgName = entry.getKey();
        Collection<FieldModel> fields = new ArrayList<>(entry.getValue());
        ResourceProtoView.Builder protoBuilder = ResourceProtoView.newBuilder();
        protoBuilder.protoClassName(namer.getTypeNameConverter().getTypeName(msgName).getNickname());
        List<ResourceProtoFieldView> fieldViews = new ArrayList<>();
        for (FieldModel field : fields) {
            FieldConfig fieldConfig = fieldConfigMap.get(field.getFullName());
            String fieldTypeSimpleName = namer.getResourceTypeName(fieldConfig.getResourceNameConfig());
            String fieldTypeName = context.getImportTypeTable().getAndSaveNicknameForTypedResourceName(fieldConfig, fieldTypeSimpleName);
            if (field.isRepeated()) {
                fieldTypeName = fieldTypeName.replaceFirst("IEnumerable", "ResourceNameList");
            }
            String fieldDocTypeName = fieldTypeName.replace('<', '{').replace('>', '}');
            String fieldElementTypeName = context.getImportTypeTable().getAndSaveNicknameForResourceNameElementType(fieldConfig, fieldTypeSimpleName);
            ResourceProtoFieldView fieldView = ResourceProtoFieldView.newBuilder().typeName(fieldTypeName).parseMethodTypeName(namer.getPackageName() + "." + fieldTypeName).docTypeName(fieldDocTypeName).elementTypeName(fieldElementTypeName).isAny(fieldConfig.getResourceNameType() == ResourceNameType.ANY).isRepeated(field.isRepeated()).isOneof(fieldConfig.getResourceNameType() == ResourceNameType.ONEOF).propertyName(namer.getResourceNameFieldGetFunctionName(fieldConfig)).underlyingPropertyName(namer.publicMethodName(Name.from(field.getSimpleName()))).build();
            fieldViews.add(fieldView);
        }
        protoBuilder.fields(fieldViews);
        protos.add(protoBuilder.build());
    }
    Collections.sort(protos, new Comparator<ResourceProtoView>() {

        @Override
        public int compare(ResourceProtoView a, ResourceProtoView b) {
            return a.protoClassName().compareTo(b.protoClassName());
        }
    });
    return protos;
}
Also used : FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList) ResourceNameMessageConfigs(com.google.api.codegen.config.ResourceNameMessageConfigs) ResourceProtoView(com.google.api.codegen.viewmodel.ResourceProtoView) ResourceProtoFieldView(com.google.api.codegen.viewmodel.ResourceProtoFieldView) Collection(java.util.Collection) FieldModel(com.google.api.codegen.config.FieldModel)

Example 2 with ResourceNameMessageConfigs

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

the class JavaFeatureConfig method create.

public static JavaFeatureConfig create(GapicProductConfig productConfig) {
    boolean enableStringFormatFunctions;
    if (productConfig.enableStringFormattingFunctionsOverride() != null) {
        enableStringFormatFunctions = productConfig.enableStringFormattingFunctionsOverride().booleanValue();
    } else {
        ResourceNameMessageConfigs resourceNameMessageConfigs = productConfig.getResourceNameMessageConfigs();
        enableStringFormatFunctions = resourceNameMessageConfigs == null || resourceNameMessageConfigs.isEmpty();
    }
    TransportProtocol tp = productConfig.getTransportProtocol();
    return JavaFeatureConfig.newBuilder().resourceNameTypesEnabled(true).enableMixins(tp != TransportProtocol.HTTP).enableGrpcStreaming(tp != TransportProtocol.HTTP).enableStringFormatFunctions(enableStringFormatFunctions).useStaticCreateMethodForOneofs(productConfig.getProtoParser().isProtoAnnotationsEnabled()).build();
}
Also used : ResourceNameMessageConfigs(com.google.api.codegen.config.ResourceNameMessageConfigs) TransportProtocol(com.google.api.codegen.config.TransportProtocol)

Example 3 with ResourceNameMessageConfigs

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

the class CSharpGapicClientTransformer method generateResourceProtos.

private List<ResourceProtoView> generateResourceProtos(GapicInterfaceContext context) {
    SurfaceNamer namer = context.getNamer();
    ResourceNameMessageConfigs resourceConfigs = context.getProductConfig().getResourceNameMessageConfigs();
    String csharpDefaultNamespace = context.getProductConfig().getPackageName();
    ListMultimap<String, FieldModel> fieldsByMessage = resourceConfigs.getFieldsWithResourceNamesByMessage();
    Map<String, FieldConfig> fieldConfigMap = context.getProductConfig().getDefaultResourceNameFieldConfigMap();
    List<ResourceProtoView> protos = new ArrayList<>();
    // Get the default proto package name for this interface.
    String defaultPackageName = context.getInterfaceModel().getParentFullName();
    for (Entry<String, Collection<FieldModel>> entry : fieldsByMessage.asMap().entrySet()) {
        String msgName = entry.getKey();
        if (!msgName.startsWith(defaultPackageName)) {
            // If the proto is not in this package, don't generate a partial class for it.
            continue;
        }
        // msgName is the "<proto package name>.<C# proto class name>".
        // Remove the proto package name, and prepend the C# namespace
        msgName = csharpDefaultNamespace + msgName.substring(msgName.lastIndexOf('.'));
        Collection<FieldModel> fields = new ArrayList<>(entry.getValue());
        ResourceProtoView.Builder protoBuilder = ResourceProtoView.newBuilder();
        protoBuilder.protoClassName(namer.getTypeNameConverter().getTypeName(msgName).getNickname());
        List<ResourceProtoFieldView> fieldViews = new ArrayList<>();
        for (FieldModel field : fields) {
            FieldConfig fieldConfig = fieldConfigMap.get(field.getFullName());
            ResourceNameConfig resourceNameConfig = fieldConfig.getResourceNameConfig();
            String fieldTypeSimpleName = namer.getResourceTypeName(resourceNameConfig);
            boolean isAny = false;
            if (fieldTypeSimpleName.equals("IResourceName")) {
                fieldTypeSimpleName = CSharpTypeTable.ALIAS_GAX + "::IResourceName";
                isAny = true;
            }
            String fieldTypeName = context.getImportTypeTable().getAndSaveNicknameForTypedResourceName(fieldConfig, fieldTypeSimpleName);
            if (field.isRepeated()) {
                fieldTypeName = fieldTypeName.replaceFirst(CSharpTypeTable.ALIAS_SYSTEM_COLLECTIONS_GENERIC + "::IEnumerable", CSharpTypeTable.ALIAS_GAX + "::ResourceNameList");
            } else if (resourceNameConfig.getCommonResourceName() == null && !isAny) {
                // Needs to be fully qualifed because the 'fieldTypeName' class name will be
                // the same as a property name on this proto message.
                fieldTypeName = namer.getPackageName() + "." + fieldTypeName;
            }
            String fieldDocTypeName = fieldTypeName.replace('<', '{').replace('>', '}');
            String fieldElementTypeName = context.getImportTypeTable().getAndSaveNicknameForResourceNameElementType(fieldConfig, fieldTypeSimpleName);
            ResourceProtoFieldView fieldView = ResourceProtoFieldView.newBuilder().typeName(fieldTypeName).parseMethodTypeName(fieldTypeName).docTypeName(fieldDocTypeName).elementTypeName(fieldElementTypeName).isAny(fieldConfig.getResourceNameType() == ResourceNameType.ANY).isRepeated(field.isRepeated()).isOneof(fieldConfig.getResourceNameType() == ResourceNameType.ONEOF).propertyName(namer.getResourceNameFieldGetFunctionName(fieldConfig)).underlyingPropertyName(namer.publicMethodName(Name.from(field.getSimpleName()))).build();
            fieldViews.add(fieldView);
        }
        protoBuilder.fields(fieldViews);
        protos.add(protoBuilder.build());
    }
    // Sort alphabetically, to make generated code deterministic.
    Collections.sort(protos, new Comparator<ResourceProtoView>() {

        @Override
        public int compare(ResourceProtoView a, ResourceProtoView b) {
            return a.protoClassName().compareTo(b.protoClassName());
        }
    });
    return protos;
}
Also used : FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList) ResourceNameConfig(com.google.api.codegen.config.ResourceNameConfig) ResourceNameMessageConfigs(com.google.api.codegen.config.ResourceNameMessageConfigs) ResourceProtoView(com.google.api.codegen.viewmodel.ResourceProtoView) ResourceProtoFieldView(com.google.api.codegen.viewmodel.ResourceProtoFieldView) Collection(java.util.Collection) FieldModel(com.google.api.codegen.config.FieldModel) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Aggregations

ResourceNameMessageConfigs (com.google.api.codegen.config.ResourceNameMessageConfigs)3 FieldConfig (com.google.api.codegen.config.FieldConfig)2 FieldModel (com.google.api.codegen.config.FieldModel)2 ResourceProtoFieldView (com.google.api.codegen.viewmodel.ResourceProtoFieldView)2 ResourceProtoView (com.google.api.codegen.viewmodel.ResourceProtoView)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 ResourceNameConfig (com.google.api.codegen.config.ResourceNameConfig)1 TransportProtocol (com.google.api.codegen.config.TransportProtocol)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1