Search in sources :

Example 1 with SymbolTable

use of com.google.api.tools.framework.model.SymbolTable 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)

Example 2 with SymbolTable

use of com.google.api.tools.framework.model.SymbolTable in project toolkit by googleapis.

the class ProtoInterfaceModel method getMethods.

@Override
public List<MethodModel> getMethods() {
    ImmutableList.Builder<MethodModel> methods = ImmutableList.builder();
    for (Method method : protoInterface.getMethods()) {
        methods.add(new ProtoMethodModel(method));
    }
    SymbolTable symbolTable = protoInterface.getModel().getSymbolTable();
    Api protoInterfaceConfig = protoInterface.getConfig();
    if (protoInterfaceConfig != null) {
        for (Mixin mixin : protoInterface.getConfig().getMixinsList()) {
            Interface mixinInterface = symbolTable.lookupInterface(mixin.getName());
            for (Method method : mixinInterface.getMethods()) {
                methods.add(new ProtoMethodModel(method));
            }
        }
    }
    return methods.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SymbolTable(com.google.api.tools.framework.model.SymbolTable) Method(com.google.api.tools.framework.model.Method) Api(com.google.protobuf.Api) Interface(com.google.api.tools.framework.model.Interface) Mixin(com.google.protobuf.Mixin)

Aggregations

Interface (com.google.api.tools.framework.model.Interface)2 SymbolTable (com.google.api.tools.framework.model.SymbolTable)2 ImmutableList (com.google.common.collect.ImmutableList)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 TypeAlias (com.google.api.codegen.util.TypeAlias)1 PythonTypeTable (com.google.api.codegen.util.py.PythonTypeTable)1 ImportFileView (com.google.api.codegen.viewmodel.ImportFileView)1 ImportSectionView (com.google.api.codegen.viewmodel.ImportSectionView)1 ImportTypeView (com.google.api.codegen.viewmodel.ImportTypeView)1 MessageType (com.google.api.tools.framework.model.MessageType)1 Method (com.google.api.tools.framework.model.Method)1 Model (com.google.api.tools.framework.model.Model)1 ProtoFile (com.google.api.tools.framework.model.ProtoFile)1 TypeRef (com.google.api.tools.framework.model.TypeRef)1 Strings (com.google.common.base.Strings)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Streams (com.google.common.collect.Streams)1