Search in sources :

Example 1 with ResourceProtoFieldView

use of com.google.api.codegen.viewmodel.ResourceProtoFieldView 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 ResourceProtoFieldView

use of com.google.api.codegen.viewmodel.ResourceProtoFieldView 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

FieldConfig (com.google.api.codegen.config.FieldConfig)2 FieldModel (com.google.api.codegen.config.FieldModel)2 ResourceNameMessageConfigs (com.google.api.codegen.config.ResourceNameMessageConfigs)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 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1