Search in sources :

Example 1 with RetryConfigDefinitionView

use of com.google.api.codegen.viewmodel.RetryConfigDefinitionView in project toolkit by googleapis.

the class GoGapicSurfaceTransformer method generate.

private StaticLangClientFileView generate(GapicInterfaceContext context) {
    StaticLangClientFileView.Builder view = StaticLangClientFileView.newBuilder();
    SurfaceNamer namer = context.getNamer();
    ApiModel model = context.getApiModel();
    ProtoInterfaceModel apiInterface = context.getInterfaceModel();
    GapicProductConfig productConfig = context.getProductConfig();
    GapicInterfaceConfig interfaceConfig = context.getInterfaceConfig();
    view.templateFileName(API_TEMPLATE_FILENAME);
    view.serviceDoc(serviceTransformer.generateServiceDoc(context, null, productConfig));
    view.domainLayerLocation(productConfig.getDomainLayerLocation());
    view.clientTypeName(namer.getApiWrapperClassName(context.getInterfaceConfig()));
    view.clientConstructorName(namer.getApiWrapperClassConstructorName(interfaceConfig));
    view.defaultClientOptionFunctionName(namer.getDefaultApiSettingsFunctionName(interfaceConfig));
    view.defaultCallOptionFunctionName(namer.getDefaultCallSettingsFunctionName(interfaceConfig));
    view.callOptionsTypeName(namer.getCallSettingsTypeName(interfaceConfig));
    view.serviceOriginalName(model.getTitle());
    view.servicePhraseName(namer.getServicePhraseName(interfaceConfig));
    String outputPath = pathMapper.getOutputPath(apiInterface.getFullName(), productConfig);
    String fileName = namer.getServiceFileName(context.getInterfaceConfig());
    view.outputPath(outputPath + File.separator + fileName);
    List<RetryConfigDefinitionView> retryDef = generateRetryConfigDefinitions(context, context.getSupportedMethods());
    view.retryPairDefinitions(retryDef);
    view.callSettings(apiCallableTransformer.generateCallSettings(context));
    List<StaticLangApiMethodView> apiMethods = generateApiMethods(context, context.getSupportedMethods());
    view.apiMethods(apiMethods);
    // "net/url" is needed for `url.QueryEscape`.
    if (apiMethods.stream().anyMatch(m -> !m.headerRequestParams().isEmpty())) {
        context.getImportTypeTable().saveNicknameFor("fmt;;;");
        context.getImportTypeTable().saveNicknameFor("net/url;;;");
    }
    // In Go, multiple methods share the same iterator type, one iterator type per resource type.
    // We have to dedupe the iterators.
    Map<String, PageStreamingDescriptorClassView> iterators = new TreeMap<>();
    for (PageStreamingDescriptorClassView desc : pageStreamingTransformer.generateDescriptorClasses(context)) {
        iterators.put(desc.typeName(), desc);
    }
    view.pageStreamingDescriptorClasses(new ArrayList<>(iterators.values()));
    // Same with long running operations.
    Map<String, LongRunningOperationDetailView> lros = new TreeMap<>();
    for (StaticLangApiMethodView apiMethod : apiMethods) {
        LongRunningOperationDetailView lro = apiMethod.operationMethod();
        if (lro != null) {
            lros.put(lro.clientReturnTypeName(), lro);
        }
    }
    view.lroDetailViews(new ArrayList<>(lros.values()));
    view.serviceHostname(productServiceConfig.getServiceHostname(context.getServiceAddress()));
    view.servicePort(productServiceConfig.getServicePort(context.getServiceAddress()));
    view.stubs(grpcStubTransformer.generateGrpcStubs(context));
    addXApiImports(context, context.getSupportedMethods());
    view.fileHeader(fileHeaderTransformer.generateFileHeader(context));
    return view.build();
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) ProtoInterfaceModel(com.google.api.codegen.config.ProtoInterfaceModel) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) RetryConfigDefinitionView(com.google.api.codegen.viewmodel.RetryConfigDefinitionView) GapicInterfaceConfig(com.google.api.codegen.config.GapicInterfaceConfig) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) ApiModel(com.google.api.codegen.config.ApiModel) TreeMap(java.util.TreeMap) LongRunningOperationDetailView(com.google.api.codegen.viewmodel.LongRunningOperationDetailView) StaticLangClientFileView(com.google.api.codegen.viewmodel.StaticLangClientFileView) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) PageStreamingDescriptorClassView(com.google.api.codegen.viewmodel.PageStreamingDescriptorClassView)

Example 2 with RetryConfigDefinitionView

use of com.google.api.codegen.viewmodel.RetryConfigDefinitionView in project toolkit by googleapis.

the class GoGapicSurfaceTransformer method generateRetryConfigDefinitions.

@VisibleForTesting
List<RetryConfigDefinitionView> generateRetryConfigDefinitions(InterfaceContext context, List<MethodModel> methods) {
    Set<RetryConfigDefinitionView.Name> retryNames = new HashSet<>();
    for (MethodModel method : methods) {
        MethodConfig conf = context.getMethodConfig(method);
        retryNames.add(RetryConfigDefinitionView.Name.create(conf.getRetrySettingsConfigName(), conf.getRetryCodesConfigName()));
    }
    TreeMap<RetryConfigDefinitionView.Name, RetryConfigDefinitionView> retryDef = new TreeMap<>();
    Map<String, ImmutableList<String>> retryCodesDef = context.getInterfaceConfig().getRetryCodesConfig().getRetryCodesDefinition();
    ImmutableMap<String, RetryParamsDefinitionProto> retryParamsDef = context.getInterfaceConfig().getRetrySettingsDefinition();
    for (RetryConfigDefinitionView.Name name : retryNames) {
        ImmutableList<String> codes = retryCodesDef.get(name.retryCodesConfigName());
        if (codes.isEmpty()) {
            continue;
        }
        List<String> retryCodeNames = new ArrayList<>();
        for (String code : codes) {
            retryCodeNames.add(context.getNamer().getStatusCodeName(code));
        }
        retryDef.put(name, RetryConfigDefinitionView.newBuilder().name(name).retryCodes(retryCodeNames).params(retryParamsDef.get(name.retrySettingsConfigName())).build());
    }
    if (!retryDef.isEmpty()) {
        context.getImportTypeTable().saveNicknameFor("time;;;");
        context.getImportTypeTable().saveNicknameFor("google.golang.org/grpc/codes;;;");
    }
    return new ArrayList<>(retryDef.values());
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) ImmutableList(com.google.common.collect.ImmutableList) RetryConfigDefinitionView(com.google.api.codegen.viewmodel.RetryConfigDefinitionView) ArrayList(java.util.ArrayList) RetryParamsDefinitionProto(com.google.api.codegen.RetryParamsDefinitionProto) TreeMap(java.util.TreeMap) MethodConfig(com.google.api.codegen.config.MethodConfig) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

RetryConfigDefinitionView (com.google.api.codegen.viewmodel.RetryConfigDefinitionView)2 TreeMap (java.util.TreeMap)2 RetryParamsDefinitionProto (com.google.api.codegen.RetryParamsDefinitionProto)1 ApiModel (com.google.api.codegen.config.ApiModel)1 GapicInterfaceConfig (com.google.api.codegen.config.GapicInterfaceConfig)1 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 MethodModel (com.google.api.codegen.config.MethodModel)1 ProtoApiModel (com.google.api.codegen.config.ProtoApiModel)1 ProtoInterfaceModel (com.google.api.codegen.config.ProtoInterfaceModel)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1 LongRunningOperationDetailView (com.google.api.codegen.viewmodel.LongRunningOperationDetailView)1 PageStreamingDescriptorClassView (com.google.api.codegen.viewmodel.PageStreamingDescriptorClassView)1 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)1 StaticLangClientFileView (com.google.api.codegen.viewmodel.StaticLangClientFileView)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1