Search in sources :

Example 1 with TypeAlias

use of com.google.api.codegen.util.TypeAlias in project toolkit by googleapis.

the class JavaSurfaceTransformer method generateRpcStubClass.

private StaticLangRpcStubView generateRpcStubClass(InterfaceContext context, GapicProductConfig productConfig) {
    SurfaceNamer namer = context.getNamer();
    InterfaceConfig interfaceConfig = context.getInterfaceConfig();
    addRpcStubImports(context);
    // Stub class has different default package name from method, request, and resource classes.
    InterfaceContext apiMethodsContext = context.withNewTypeTable(context.getNamer().getRootPackageName());
    List<StaticLangApiMethodView> methods = javaApiMethodTransformer.generateApiMethods(apiMethodsContext);
    StaticLangRpcStubView.Builder stubClass = StaticLangRpcStubView.newBuilder();
    stubClass.doc(serviceTransformer.generateServiceDoc(context, null, productConfig));
    String name = namer.getApiRpcStubClassName(interfaceConfig.getInterfaceModel(), productConfig.getTransportProtocol());
    stubClass.releaseLevelAnnotation(namer.getReleaseAnnotation(ReleaseLevel.BETA));
    stubClass.name(name);
    stubClass.parentName(namer.getApiStubInterfaceName(interfaceConfig));
    stubClass.settingsClassName(getAndSaveNicknameForRootType(apiMethodsContext, namer.getApiSettingsClassName(interfaceConfig)));
    stubClass.stubSettingsClassName(getAndSaveNicknameForStubType(apiMethodsContext, namer.getApiStubSettingsClassName(interfaceConfig)));
    stubClass.callableFactoryClassName(getAndSaveNicknameForStubType(apiMethodsContext, namer.getCallableFactoryClassName(interfaceConfig, productConfig.getTransportProtocol())));
    stubClass.methodDescriptors(apiCallableTransformer.generateMethodDescriptors(apiMethodsContext));
    stubClass.apiCallables(apiCallableTransformer.generateStaticLangApiCallables(apiMethodsContext));
    stubClass.callableMethods(filterIncludeCallableMethods(methods));
    stubClass.hasDefaultInstance(interfaceConfig.hasDefaultInstance());
    stubClass.hasLongRunningOperations(interfaceConfig.hasLongRunningOperations());
    stubClass.transportProtocol(productConfig.getTransportProtocol());
    if (productConfig.getTransportProtocol() == TransportProtocol.HTTP) {
        stubClass.callSettingsClassName("HttpJsonCallSettings");
        stubClass.stubCallableFactoryClassName("HttpJsonStubCallableFactory");
    } else {
        stubClass.callSettingsClassName("GrpcCallSettings");
        stubClass.stubCallableFactoryClassName("GrpcStubCallableFactory");
    }
    for (TypeAlias alias : apiMethodsContext.getImportTypeTable().getTypeTable().getAllImports().values()) {
        context.getImportTypeTable().getAndSaveNicknameFor(alias);
    }
    return stubClass.build();
}
Also used : InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) TypeAlias(com.google.api.codegen.util.TypeAlias) InterfaceContext(com.google.api.codegen.config.InterfaceContext) StaticLangRpcStubView(com.google.api.codegen.viewmodel.StaticLangRpcStubView) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Example 2 with TypeAlias

use of com.google.api.codegen.util.TypeAlias in project toolkit by googleapis.

the class StandardImportSectionTransformer method generateImportSection.

public ImportSectionView generateImportSection(Map<String, TypeAlias> typeImports, String className) {
    HashSet<String> allFullNames = new HashSet<>();
    ImmutableList.Builder<ImportFileView> appImports = ImmutableList.builder();
    for (TypeAlias alias : typeImports.values()) {
        if (excludeAppImport(alias, className)) {
            continue;
        }
        ImportTypeView.Builder imp = ImportTypeView.newBuilder();
        String fullName;
        switch(alias.getImportType()) {
            case OuterImport:
                fullName = alias.getParentFullName();
                break;
            default:
                fullName = alias.getFullName();
                break;
        }
        if (allFullNames.contains(fullName)) {
            continue;
        }
        allFullNames.add(fullName);
        imp.fullName(fullName);
        imp.nickname(alias.getNickname());
        imp.type(alias.getImportType());
        appImports.add(ImportFileView.newBuilder().types(ImmutableList.of(imp.build())).build());
    }
    return ImportSectionView.newBuilder().appImports(appImports.build()).build();
}
Also used : ImportFileView(com.google.api.codegen.viewmodel.ImportFileView) ImmutableList(com.google.common.collect.ImmutableList) ImportTypeView(com.google.api.codegen.viewmodel.ImportTypeView) TypeAlias(com.google.api.codegen.util.TypeAlias) HashSet(java.util.HashSet)

Example 3 with TypeAlias

use of com.google.api.codegen.util.TypeAlias in project toolkit by googleapis.

the class PythonTypeTable method updateOldImports.

private void updateOldImports(String shortName, Collection<TypeAlias> aliases) {
    for (TypeAlias alias : aliases) {
        String className = alias.getNickname().substring(alias.getNickname().indexOf(".") + 1);
        getAndSaveNicknameFor(TypeAlias.createAliasedImport(alias.getFullName(), shortName + "." + className));
    }
}
Also used : TypeAlias(com.google.api.codegen.util.TypeAlias)

Example 4 with TypeAlias

use of com.google.api.codegen.util.TypeAlias in project toolkit by googleapis.

the class PhpImportSectionTransformer method generateImportSection.

/**
 * Package-private for use in PhpGapicSurfaceTestTransformer
 */
ImportSectionView generateImportSection(Map<String, TypeAlias> typeImports) {
    ImmutableList.Builder<ImportFileView> appImports = ImmutableList.builder();
    for (Map.Entry<String, TypeAlias> entry : typeImports.entrySet()) {
        String key = entry.getKey();
        TypeAlias alias = entry.getValue();
        // Remove leading backslash because it is not required by PHP use statements
        String fullName = key.startsWith("\\") ? key.substring(1) : key;
        ImportTypeView.Builder imp = ImportTypeView.newBuilder();
        imp.fullName(fullName);
        imp.nickname(alias.getNickname());
        imp.type(alias.getImportType());
        appImports.add(ImportFileView.newBuilder().types(ImmutableList.of(imp.build())).build());
    }
    return ImportSectionView.newBuilder().appImports(appImports.build()).build();
}
Also used : ImportFileView(com.google.api.codegen.viewmodel.ImportFileView) ImmutableList(com.google.common.collect.ImmutableList) ImportTypeView(com.google.api.codegen.viewmodel.ImportTypeView) TypeAlias(com.google.api.codegen.util.TypeAlias) Map(java.util.Map)

Example 5 with TypeAlias

use of com.google.api.codegen.util.TypeAlias in project toolkit by googleapis.

the class PythonImportSectionTransformer method generateTypesProtoImports.

private ImportSectionView.Builder generateTypesProtoImports(Model model, GapicProductConfig productConfig) {
    ImportSectionView.Builder importView = ImportSectionView.newBuilder();
    ModelTypeTable allTypeTable = emptyTypeTable(productConfig);
    // Imports from the same API client library package.
    Set<ImportFileView> localImports = new TreeSet<>(importFileViewComparator());
    // Shared imports, e.g. protobuf imports.
    Set<ImportFileView> sharedImports = new TreeSet<>(importFileViewComparator());
    // All imports.
    Set<ImportFileView> appImports = new TreeSet<>(importFileViewComparator());
    Set<String> localImportNames = new HashSet<>();
    Set<String> sharedImportNames = new HashSet<>();
    SymbolTable symbolTable = model.getSymbolTable();
    Collection<ProtoFile> localImportFiles = productConfig.getInterfaceConfigMap().keySet().stream().map(symbolTable::lookupInterface).filter(Objects::nonNull).map(Interface::getFile).collect(ImmutableSet.toImmutableSet());
    Iterable<ProtoFile> allFiles = model.reachable(model.getFiles());
    // Skip API interfaces excluded from interface configs.
    Collection<ProtoFile> skippedFiles = Streams.stream(allFiles).filter(ProtoFile::isSource).flatMap(f -> f.getInterfaces().stream()).filter(Interface::isReachable).filter(i -> productConfig.getInterfaceConfig(i) == null).map(Interface::getFile).collect(ImmutableSet.toImmutableSet());
    // Can't use Collection#removeAll since allFiles is an Iterable.
    Iterable<ProtoFile> protoFileDependencies = Streams.stream(allFiles).filter(f -> !skippedFiles.contains(f)).collect(ImmutableSet.toImmutableSet());
    // Save proto file import names to the type table for disambiguation.
    populateTypeTable(protoFileDependencies, allTypeTable, localImportNames, sharedImportNames, localImportFiles);
    // Get disambiguated imports.
    for (Map.Entry<String, TypeAlias> entry : allTypeTable.getImports().entrySet()) {
        String importFullName = entry.getKey();
        String nickName = entry.getValue().getNickname();
        appImports.add(generateAppImport(importFullName, nickName));
        if (localImportNames.contains(importFullName)) {
            localImports.add(generateAppImport(importFullName, nickName));
        } else if (sharedImportNames.contains(importFullName)) {
            sharedImports.add(generateAppImport(importFullName, nickName));
        }
    }
    importView.localImports(ImmutableList.copyOf(localImports));
    importView.sharedImports(ImmutableList.copyOf(sharedImports));
    importView.appImports(ImmutableList.copyOf(appImports));
    return importView;
}
Also used : TypeRef(com.google.api.tools.framework.model.TypeRef) ImportFileView(com.google.api.codegen.viewmodel.ImportFileView) MessageType(com.google.api.tools.framework.model.MessageType) ProtoFile(com.google.api.tools.framework.model.ProtoFile) ImportSectionView(com.google.api.codegen.viewmodel.ImportSectionView) ImportTypeView(com.google.api.codegen.viewmodel.ImportTypeView) SymbolTable(com.google.api.tools.framework.model.SymbolTable) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) InitCodeLineType(com.google.api.codegen.metacode.InitCodeLineType) Map(java.util.Map) com.google.api.codegen.transformer(com.google.api.codegen.transformer) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) InitCodeNode(com.google.api.codegen.metacode.InitCodeNode) Streams(com.google.common.collect.Streams) PythonTypeTable(com.google.api.codegen.util.py.PythonTypeTable) Objects(java.util.Objects) com.google.api.codegen.config(com.google.api.codegen.config) List(java.util.List) TypeAlias(com.google.api.codegen.util.TypeAlias) TreeMap(java.util.TreeMap) Interface(com.google.api.tools.framework.model.Interface) Comparator(java.util.Comparator) Model(com.google.api.tools.framework.model.Model) Collections(java.util.Collections) ImportFileView(com.google.api.codegen.viewmodel.ImportFileView) ProtoFile(com.google.api.tools.framework.model.ProtoFile) SymbolTable(com.google.api.tools.framework.model.SymbolTable) TypeAlias(com.google.api.codegen.util.TypeAlias) TreeSet(java.util.TreeSet) Objects(java.util.Objects) Map(java.util.Map) TreeMap(java.util.TreeMap) ImportSectionView(com.google.api.codegen.viewmodel.ImportSectionView) HashSet(java.util.HashSet)

Aggregations

TypeAlias (com.google.api.codegen.util.TypeAlias)8 ImportFileView (com.google.api.codegen.viewmodel.ImportFileView)4 ImportTypeView (com.google.api.codegen.viewmodel.ImportTypeView)4 ImmutableList (com.google.common.collect.ImmutableList)4 InterfaceConfig (com.google.api.codegen.config.InterfaceConfig)2 InterfaceContext (com.google.api.codegen.config.InterfaceContext)2 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 com.google.api.codegen.config (com.google.api.codegen.config)1 InitCodeLineType (com.google.api.codegen.metacode.InitCodeLineType)1 InitCodeNode (com.google.api.codegen.metacode.InitCodeNode)1 com.google.api.codegen.transformer (com.google.api.codegen.transformer)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1 PythonTypeTable (com.google.api.codegen.util.py.PythonTypeTable)1 ImportSectionView (com.google.api.codegen.viewmodel.ImportSectionView)1 StaticLangRpcStubView (com.google.api.codegen.viewmodel.StaticLangRpcStubView)1 StaticLangStubInterfaceView (com.google.api.codegen.viewmodel.StaticLangStubInterfaceView)1 Interface (com.google.api.tools.framework.model.Interface)1 MessageType (com.google.api.tools.framework.model.MessageType)1