Search in sources :

Example 16 with SurfaceNamer

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

the class CSharpGapicUnitTestTransformer method transform.

@Override
public List<ViewModel> transform(ApiModel model, GapicProductConfig productConfig) {
    List<ViewModel> surfaceDocs = new ArrayList<>();
    SurfaceNamer namer = new CSharpSurfaceNamer(productConfig.getPackageName());
    for (InterfaceModel apiInterface : model.getInterfaces()) {
        GapicInterfaceContext context = GapicInterfaceContext.create(apiInterface, productConfig, csharpCommonTransformer.createTypeTable(namer.getPackageName()), namer, new CSharpFeatureConfig());
        csharpCommonTransformer.addCommonImports(context);
        ModelTypeTable typeTable = context.getImportTypeTable();
        typeTable.saveNicknameFor("Xunit.FactAttribute");
        typeTable.saveNicknameFor("Moq.Mock");
        if (context.getLongRunningMethods().iterator().hasNext()) {
            typeTable.saveNicknameFor("Google.LongRunning.Operations");
        }
        surfaceDocs.add(generateUnitTest(context));
        surfaceDocs.add(generateUnitTestCsproj(context));
    }
    return surfaceDocs;
}
Also used : InterfaceModel(com.google.api.codegen.config.InterfaceModel) ArrayList(java.util.ArrayList) ModelTypeTable(com.google.api.codegen.transformer.ModelTypeTable) GapicInterfaceContext(com.google.api.codegen.transformer.GapicInterfaceContext) ViewModel(com.google.api.codegen.viewmodel.ViewModel) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Example 17 with SurfaceNamer

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

the class GoGapicSurfaceTransformer method generateExample.

private StaticLangClientExampleFileView generateExample(InterfaceContext context) {
    StaticLangClientExampleFileView.Builder view = StaticLangClientExampleFileView.newBuilder();
    SurfaceNamer namer = context.getNamer();
    InterfaceModel apiInterface = context.getInterfaceModel();
    ProductConfig productConfig = context.getProductConfig();
    InterfaceConfig interfaceConfig = context.getInterfaceConfig();
    view.templateFileName(SAMPLE_TEMPLATE_FILENAME);
    String outputPath = pathMapper.getOutputPath(apiInterface.getFullName(), productConfig);
    String fileName = namer.getExampleFileName(context.getInterfaceConfig());
    view.outputPath(outputPath + File.separator + fileName);
    view.clientTypeName(namer.getApiWrapperClassName(context.getInterfaceConfig()));
    view.clientConstructorName(namer.getApiWrapperClassConstructorName(interfaceConfig));
    view.clientConstructorExampleName(namer.getApiWrapperClassConstructorExampleName(interfaceConfig));
    view.apiMethods(generateApiMethods(context, context.getPublicMethods()));
    view.iamResources(iamResourceTransformer.generateIamResources(context));
    // Examples are different from the API. In particular, we use short declaration
    // and so we omit most type names. We only need
    // - Context, to initialize the client
    // - The VKit generated library, that's what the sample is for
    // - The input types of the methods, to initialize the requests
    // So, we clear all imports; addXExampleImports will add back the ones we want.
    context.getImportTypeTable().getImports().clear();
    addXExampleImports(context, context.getPublicMethods());
    view.fileHeader(fileHeaderTransformer.generateFileHeader(context));
    return view.build();
}
Also used : GapicInterfaceConfig(com.google.api.codegen.config.GapicInterfaceConfig) InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) InterfaceModel(com.google.api.codegen.config.InterfaceModel) ProtoInterfaceModel(com.google.api.codegen.config.ProtoInterfaceModel) StaticLangClientExampleFileView(com.google.api.codegen.viewmodel.StaticLangClientExampleFileView) ProductConfig(com.google.api.codegen.config.ProductConfig) GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Example 18 with SurfaceNamer

use of com.google.api.codegen.transformer.SurfaceNamer 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);
    // If any methods have header request params, "fmt" is needed for `fmt.Sprintf` calls.
    if (apiMethods.stream().anyMatch(m -> !m.headerRequestParams().isEmpty())) {
        context.getImportTypeTable().saveNicknameFor("fmt;;;");
    }
    view.iamResources(iamResourceTransformer.generateIamResources(context));
    if (!((GapicInterfaceConfig) productConfig.getInterfaceConfig(apiInterface.getFullName())).getIamResources().isEmpty()) {
        context.getImportTypeTable().saveNicknameFor("cloud.google.com/go/iam;;;");
    }
    // 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.serviceAddress(context.getApiModel().getServiceAddress());
    view.servicePort(model.getServicePort());
    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) 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 19 with SurfaceNamer

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

the class JavaSurfaceTestTransformer method createMockServiceImplFileView.

private MockServiceImplFileView createMockServiceImplFileView(InterfaceContext context) {
    addMockServiceImplImports(context);
    SurfaceNamer namer = context.getNamer();
    String outputPath = pathMapper.getOutputPath(context.getInterfaceModel().getFullName(), context.getProductConfig());
    String name = namer.getMockGrpcServiceImplName(context.getInterfaceModel());
    String grpcClassName = context.getImportTypeTable().getAndSaveNicknameFor(namer.getGrpcServiceClassName(context.getInterfaceModel()));
    MockServiceImplFileView.Builder mockServiceImplFile = MockServiceImplFileView.newBuilder();
    mockServiceImplFile.serviceImpl(MockServiceImplView.newBuilder().name(name).mockGrpcClassName(grpcClassName).grpcMethods(mockServiceTransformer.createMockGrpcMethodViews(context)).build());
    mockServiceImplFile.outputPath(namer.getSourceFilePath(outputPath, name));
    mockServiceImplFile.templateFileName(MOCK_SERVICE_IMPL_FILE);
    // Imports must be done as the last step to catch all imports.
    FileHeaderView fileHeader = fileHeaderTransformer.generateFileHeader(context);
    mockServiceImplFile.fileHeader(fileHeader);
    return mockServiceImplFile.build();
}
Also used : MockServiceImplFileView(com.google.api.codegen.viewmodel.testing.MockServiceImplFileView) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) FileHeaderView(com.google.api.codegen.viewmodel.FileHeaderView)

Example 20 with SurfaceNamer

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

the class JavaSurfaceTestTransformer method addUnitTestImports.

// ///////////////////////////////// Imports //////////////////////////////////////
private void addUnitTestImports(InterfaceContext context) {
    ImportTypeTable typeTable = context.getImportTypeTable();
    typeTable.saveNicknameFor("com.google.api.gax.rpc.InvalidArgumentException");
    typeTable.saveNicknameFor("com.google.common.collect.Lists");
    typeTable.saveNicknameFor("java.io.IOException");
    typeTable.saveNicknameFor("java.util.Arrays");
    typeTable.saveNicknameFor("java.util.List");
    typeTable.saveNicknameFor("org.junit.After");
    typeTable.saveNicknameFor("org.junit.AfterClass");
    typeTable.saveNicknameFor("org.junit.Assert");
    typeTable.saveNicknameFor("org.junit.BeforeClass");
    typeTable.saveNicknameFor("org.junit.Test");
    if (context.getInterfaceConfig().hasLongRunningOperations()) {
        typeTable.saveNicknameFor("com.google.protobuf.Any");
    }
    switch(context.getProductConfig().getTransportProtocol()) {
        case GRPC:
            typeTable.saveNicknameFor("com.google.api.gax.core.NoCredentialsProvider");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.ApiClientHeaderProvider");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.StatusCode");
            typeTable.saveNicknameFor("com.google.api.gax.grpc.GaxGrpcProperties");
            typeTable.saveNicknameFor("com.google.api.gax.grpc.GrpcStatusCode");
            typeTable.saveNicknameFor("com.google.api.gax.grpc.testing.LocalChannelProvider");
            typeTable.saveNicknameFor("com.google.api.gax.grpc.testing.MockGrpcService");
            typeTable.saveNicknameFor("com.google.api.gax.grpc.testing.MockServiceHelper");
            typeTable.saveNicknameFor("com.google.protobuf.GeneratedMessageV3");
            typeTable.saveNicknameFor("io.grpc.Status");
            typeTable.saveNicknameFor("io.grpc.StatusRuntimeException");
            typeTable.saveNicknameFor("java.util.ArrayList");
            typeTable.saveNicknameFor("java.util.Objects");
            typeTable.saveNicknameFor("java.util.concurrent.ExecutionException");
            typeTable.saveNicknameFor("org.junit.Before");
            break;
        case HTTP:
            typeTable.saveNicknameFor("com.google.api.gax.httpjson.ApiMethodDescriptor");
            typeTable.saveNicknameFor("com.google.api.gax.httpjson.GaxHttpJsonProperties");
            typeTable.saveNicknameFor("com.google.api.gax.httpjson.testing.MockHttpService");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.ApiClientHeaderProvider");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.ApiException");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.ApiExceptionFactory");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.StatusCode.Code");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.testing.FakeStatusCode");
            typeTable.saveNicknameFor("com.google.common.collect.ImmutableList");
            // Import stub settings class in unit test file.
            SurfaceNamer stubNamer = context.getNamer().cloneWithPackageName(context.getNamer().getStubPackageName());
            TypeName rpcStubClassName = stubNamer.getTypeNameConverter().getTypeNameInImplicitPackage(stubNamer.getApiStubSettingsClassName(context.getInterfaceConfig()));
            rpcStubClassName.getAndSaveNicknameIn(typeTable.getTypeTable());
            break;
    }
}
Also used : TypeName(com.google.api.codegen.util.TypeName) ImportTypeTable(com.google.api.codegen.transformer.ImportTypeTable) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Aggregations

SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)91 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)22 InterfaceModel (com.google.api.codegen.config.InterfaceModel)18 ImmutableList (com.google.common.collect.ImmutableList)16 ViewModel (com.google.api.codegen.viewmodel.ViewModel)14 ArrayList (java.util.ArrayList)14 GapicInterfaceContext (com.google.api.codegen.transformer.GapicInterfaceContext)13 MethodModel (com.google.api.codegen.config.MethodModel)12 FileHeaderView (com.google.api.codegen.viewmodel.FileHeaderView)10 ImportSectionView (com.google.api.codegen.viewmodel.ImportSectionView)10 MethodConfig (com.google.api.codegen.config.MethodConfig)9 GapicMethodContext (com.google.api.codegen.transformer.GapicMethodContext)9 ApiModel (com.google.api.codegen.config.ApiModel)8 InterfaceConfig (com.google.api.codegen.config.InterfaceConfig)8 ModelTypeTable (com.google.api.codegen.transformer.ModelTypeTable)8 SmokeTestClassView (com.google.api.codegen.viewmodel.testing.SmokeTestClassView)8 FieldConfig (com.google.api.codegen.config.FieldConfig)6 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)6 ImportTypeTable (com.google.api.codegen.transformer.ImportTypeTable)5 PackageMetadataNamer (com.google.api.codegen.transformer.PackageMetadataNamer)5