Search in sources :

Example 96 with SurfaceNamer

use of com.google.api.codegen.transformer.SurfaceNamer 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)

Example 97 with SurfaceNamer

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

the class CSharpGapicClientTransformer method generateModifyMethods.

private List<ModifyMethodView> generateModifyMethods(GapicInterfaceContext context) {
    SurfaceNamer namer = context.getNamer();
    List<ModifyMethodView> modifyMethods = new ArrayList<>();
    Set<String> modifyTypeNames = new HashSet<>();
    for (MethodModel method : csharpCommonTransformer.getSupportedMethods(context)) {
        MethodContext methodContext = context.asRequestMethodContext(method);
        String inputTypeFullName = methodContext.getMethodModel().getInputFullName();
        if (modifyTypeNames.contains(inputTypeFullName)) {
            continue;
        }
        modifyTypeNames.add(inputTypeFullName);
        MethodConfig methodConfig = methodContext.getMethodConfig();
        ModifyMethodView.Builder builder = ModifyMethodView.builder();
        builder.name(namer.getModifyMethodName(methodContext));
        builder.requestTypeName(method.getAndSaveRequestTypeName(context.getImportTypeTable(), context.getNamer()));
        builder.grpcStreamingType(methodConfig.getGrpcStreamingType());
        modifyMethods.add(builder.build());
    }
    return modifyMethods;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodModel(com.google.api.codegen.config.MethodModel) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.config.MethodContext) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) ModifyMethodView(com.google.api.codegen.viewmodel.ModifyMethodView) HashSet(java.util.HashSet)

Example 98 with SurfaceNamer

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

the class CSharpGapicClientTransformer method generateSettingsDoc.

public SettingsDocView generateSettingsDoc(GapicInterfaceContext context) {
    SurfaceNamer namer = context.getNamer();
    SettingsDocView.Builder settingsDoc = SettingsDocView.newBuilder();
    settingsDoc.serviceHostname(productServiceConfig.getServiceHostname(context.getServiceAddress()));
    settingsDoc.servicePort(productServiceConfig.getServicePort(context.getServiceAddress()));
    // Unused in C#
    settingsDoc.exampleApiMethodName("");
    // Unused in C#
    settingsDoc.exampleApiMethodSettingsGetter("");
    // Unused in C#
    settingsDoc.exampleApiMethodSettingsHasRetrySettings(true);
    settingsDoc.apiClassName(namer.getApiWrapperClassName(context.getInterfaceConfig()));
    settingsDoc.settingsVarName(namer.getApiSettingsVariableName(context.getInterfaceConfig()));
    settingsDoc.settingsClassName(namer.getApiSettingsClassName(context.getInterfaceConfig()));
    settingsDoc.settingsBuilderVarName(namer.getApiSettingsBuilderVarName(context.getInterfaceConfig()));
    settingsDoc.hasDefaultInstance(context.getInterfaceConfig().hasDefaultInstance());
    return settingsDoc.build();
}
Also used : SettingsDocView(com.google.api.codegen.viewmodel.SettingsDocView) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Example 99 with SurfaceNamer

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

the class CSharpGapicClientTransformer method generateApiClass.

private StaticLangApiView generateApiClass(GapicInterfaceContext context) {
    SurfaceNamer namer = context.getNamer();
    StaticLangApiView.Builder apiClass = StaticLangApiView.newBuilder();
    List<StaticLangApiMethodView> methods = apiMethodTransformer.generateApiMethods(context);
    apiClass.doc(serviceTransformer.generateServiceDoc(context, null, context.getProductConfig()));
    apiClass.name(namer.getApiWrapperClassName(context.getInterfaceConfig()));
    apiClass.implName(namer.getApiWrapperClassImplName(context.getInterfaceConfig()));
    apiClass.grpcServiceName(namer.getGrpcContainerTypeName(context.getInterfaceModel()));
    String grpcTypeName = namer.getGrpcServiceClassName(context.getInterfaceModel());
    int dotIndex = grpcTypeName.indexOf('.');
    apiClass.grpcTypeNameOuter(grpcTypeName.substring(0, dotIndex));
    apiClass.grpcTypeNameInner(grpcTypeName.substring(dotIndex + 1, grpcTypeName.length()));
    apiClass.settingsClassName(context.getNamer().getApiSettingsClassName(context.getInterfaceConfig()));
    List<ApiCallableView> callables = new ArrayList<>();
    for (ApiCallableView call : apiCallableTransformer.generateStaticLangApiCallables(context)) {
        if (call.type() == ApiCallableImplType.SimpleApiCallable || call.type() == ApiCallableImplType.BatchingApiCallable || call.type() == ApiCallableImplType.BidiStreamingApiCallable || call.type() == ApiCallableImplType.ServerStreamingApiCallable) {
            callables.add(call);
        }
    }
    apiClass.apiCallableMembers(callables);
    apiClass.pathTemplates(pathTemplateTransformer.generatePathTemplates(context));
    apiClass.formatResourceFunctions(pathTemplateTransformer.generateFormatResourceFunctions(context));
    apiClass.parseResourceFunctions(pathTemplateTransformer.generateParseResourceFunctions(context));
    apiClass.apiMethods(methods);
    List<StaticLangApiMethodView> methodsImpl = new ArrayList<>();
    for (StaticLangApiMethodView method : methods) {
        if (methodTypeHasImpl(method.type())) {
            methodsImpl.add(method);
        }
    }
    apiClass.apiMethodsImpl(methodsImpl);
    apiClass.hasDefaultInstance(context.getInterfaceConfig().hasDefaultInstance());
    apiClass.hasLongRunningOperations(context.getInterfaceConfig().hasLongRunningOperations());
    apiClass.reroutedGrpcClients(csharpCommonTransformer.generateReroutedGrpcView(context));
    apiClass.modifyMethods(generateModifyMethods(context));
    apiClass.apiHasUnaryMethod(methods.stream().anyMatch(m -> m.grpcStreamingType() == GrpcStreamingType.NonStreaming));
    apiClass.apiHasServerStreamingMethod(methods.stream().anyMatch(m -> m.grpcStreamingType() == GrpcStreamingType.ServerStreaming));
    apiClass.apiHasClientStreamingMethod(methods.stream().anyMatch(m -> m.grpcStreamingType() == GrpcStreamingType.ClientStreaming));
    apiClass.apiHasBidiStreamingMethod(methods.stream().anyMatch(m -> m.grpcStreamingType() == GrpcStreamingType.BidiStreaming));
    return apiClass.build();
}
Also used : Arrays(java.util.Arrays) ListMultimap(com.google.common.collect.ListMultimap) RetryCodesDefinitionView(com.google.api.codegen.viewmodel.RetryCodesDefinitionView) ApiCallableView(com.google.api.codegen.viewmodel.ApiCallableView) StaticLangApiView(com.google.api.codegen.viewmodel.StaticLangApiView) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) StaticLangApiMethodTransformer(com.google.api.codegen.transformer.StaticLangApiMethodTransformer) MethodContext(com.google.api.codegen.config.MethodContext) BatchingTransformer(com.google.api.codegen.transformer.BatchingTransformer) ViewModel(com.google.api.codegen.viewmodel.ViewModel) Map(java.util.Map) ResourceProtoFieldView(com.google.api.codegen.viewmodel.ResourceProtoFieldView) ApiCallSettingsView(com.google.api.codegen.viewmodel.ApiCallSettingsView) ResourceNameType(com.google.api.codegen.config.ResourceNameType) ResourceProtoView(com.google.api.codegen.viewmodel.ResourceProtoView) CSharpTypeTable(com.google.api.codegen.util.csharp.CSharpTypeTable) GapicCodePathMapper(com.google.api.codegen.gapic.GapicCodePathMapper) ResourceNameView(com.google.api.codegen.viewmodel.ResourceNameView) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) Collection(java.util.Collection) Set(java.util.Set) FileHeaderTransformer(com.google.api.codegen.transformer.FileHeaderTransformer) Collectors(java.util.stream.Collectors) Name(com.google.api.codegen.util.Name) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) List(java.util.List) ApiCallableTransformer(com.google.api.codegen.transformer.ApiCallableTransformer) InterfaceModel(com.google.api.codegen.config.InterfaceModel) SettingsDocView(com.google.api.codegen.viewmodel.SettingsDocView) Entry(java.util.Map.Entry) ModelToViewTransformer(com.google.api.codegen.transformer.ModelToViewTransformer) ResourceNameSingleView(com.google.api.codegen.viewmodel.ResourceNameSingleView) ApiCallableImplType(com.google.api.codegen.viewmodel.ApiCallableImplType) StandardImportSectionTransformer(com.google.api.codegen.transformer.StandardImportSectionTransformer) FieldModel(com.google.api.codegen.config.FieldModel) GrpcStreamingType(com.google.api.codegen.config.GrpcStreamingConfig.GrpcStreamingType) PageStreamingTransformer(com.google.api.codegen.transformer.PageStreamingTransformer) ServiceTransformer(com.google.api.codegen.transformer.ServiceTransformer) PathTemplateTransformer(com.google.api.codegen.transformer.PathTemplateTransformer) StaticLangApiAndSettingsFileView(com.google.api.codegen.viewmodel.StaticLangApiAndSettingsFileView) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MethodModel(com.google.api.codegen.config.MethodModel) MethodConfig(com.google.api.codegen.config.MethodConfig) CSharpAliasMode(com.google.api.codegen.util.csharp.CSharpAliasMode) StaticLangSettingsView(com.google.api.codegen.viewmodel.StaticLangSettingsView) ResourceNameMessageConfigs(com.google.api.codegen.config.ResourceNameMessageConfigs) ResourceNameConfig(com.google.api.codegen.config.ResourceNameConfig) File(java.io.File) StaticLangResourceNamesView(com.google.api.codegen.viewmodel.StaticLangResourceNamesView) GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) ApiModel(com.google.api.codegen.config.ApiModel) ProductServiceConfig(com.google.api.codegen.config.ProductServiceConfig) RetryDefinitionsTransformer(com.google.api.codegen.transformer.RetryDefinitionsTransformer) InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) Comparator(java.util.Comparator) Collections(java.util.Collections) ModifyMethodView(com.google.api.codegen.viewmodel.ModifyMethodView) FieldConfig(com.google.api.codegen.config.FieldConfig) ClientMethodType(com.google.api.codegen.viewmodel.ClientMethodType) ApiCallableView(com.google.api.codegen.viewmodel.ApiCallableView) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) ArrayList(java.util.ArrayList) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) StaticLangApiView(com.google.api.codegen.viewmodel.StaticLangApiView)

Example 100 with SurfaceNamer

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

the class CSharpGapicSnippetsTransformer method generatePagedRequestAsyncMethod.

private StaticLangApiMethodSnippetView generatePagedRequestAsyncMethod(MethodContext methodContext) {
    StaticLangApiMethodView method = generateInitCode(apiMethodTransformer.generatePagedRequestObjectAsyncMethod(methodContext, csharpCommonTransformer.pagedMethodAdditionalParams(), null), methodContext, methodContext.getMethodConfig().getRequiredFieldConfigs(), InitCodeOutputType.SingleObject, CallingForm.RequestAsyncPaged);
    SurfaceNamer namer = methodContext.getNamer();
    PageStreamingConfig pageStreaming = methodContext.getMethodConfig().getPageStreaming();
    FieldConfig resourceFieldConfig = pageStreaming.getResourcesFieldConfig();
    String callerResponseTypeName = namer.getAndSaveCallerAsyncPagedResponseTypeName(methodContext, resourceFieldConfig);
    return StaticLangApiMethodSnippetView.newBuilder().method(method).snippetMethodName(method.name() + "_RequestObject").callerResponseTypeName(callerResponseTypeName).apiClassName(namer.getApiWrapperClassName(methodContext.getInterfaceConfig())).apiVariableName(method.apiVariableName()).build();
}
Also used : StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) FieldConfig(com.google.api.codegen.config.FieldConfig) PageStreamingConfig(com.google.api.codegen.config.PageStreamingConfig) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Aggregations

SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)109 InterfaceModel (com.google.api.codegen.config.InterfaceModel)26 ViewModel (com.google.api.codegen.viewmodel.ViewModel)23 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)22 ArrayList (java.util.ArrayList)22 ImmutableList (com.google.common.collect.ImmutableList)17 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)15 MethodModel (com.google.api.codegen.config.MethodModel)14 ImportSectionView (com.google.api.codegen.viewmodel.ImportSectionView)13 SmokeTestClassView (com.google.api.codegen.viewmodel.testing.SmokeTestClassView)11 InterfaceConfig (com.google.api.codegen.config.InterfaceConfig)10 ModelTypeTable (com.google.api.codegen.transformer.ModelTypeTable)10 FileHeaderView (com.google.api.codegen.viewmodel.FileHeaderView)10 ApiModel (com.google.api.codegen.config.ApiModel)9 MethodConfig (com.google.api.codegen.config.MethodConfig)9 ProtoApiModel (com.google.api.codegen.config.ProtoApiModel)9 DynamicLangXApiView (com.google.api.codegen.viewmodel.DynamicLangXApiView)9 FieldConfig (com.google.api.codegen.config.FieldConfig)8 GapicMethodContext (com.google.api.codegen.config.GapicMethodContext)8 ImportTypeTable (com.google.api.codegen.transformer.ImportTypeTable)8