Search in sources :

Example 16 with ProtoFile

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

the class ResourceNameMessageConfigs method createFieldsByMessage.

private static ListMultimap<String, FieldModel> createFieldsByMessage(List<ProtoFile> protoFiles, Map<String, ResourceNameMessageConfig> messageResourceTypeConfigMap) {
    ListMultimap<String, FieldModel> fieldsByMessage = ArrayListMultimap.create();
    Set<String> seenProtoFiles = new HashSet<>();
    for (ProtoFile protoFile : protoFiles) {
        if (!seenProtoFiles.contains(protoFile.getSimpleName())) {
            seenProtoFiles.add(protoFile.getSimpleName());
            for (MessageType msg : protoFile.getMessages()) {
                ResourceNameMessageConfig messageConfig = messageResourceTypeConfigMap.get(msg.getFullName());
                if (messageConfig == null) {
                    continue;
                }
                for (Field field : msg.getFields()) {
                    if (!messageConfig.getEntityNamesForField(field.getSimpleName()).isEmpty()) {
                        fieldsByMessage.put(msg.getFullName(), new ProtoField(field));
                    }
                }
            }
        }
    }
    return fieldsByMessage;
}
Also used : Field(com.google.api.tools.framework.model.Field) ProtoFile(com.google.api.tools.framework.model.ProtoFile) MessageType(com.google.api.tools.framework.model.MessageType)

Example 17 with ProtoFile

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

the class PythonSurfaceNamer method getFormattedPrintArgName.

@Override
public /**
 * If the argument is a protobuf enum, returns an expression that translates the enum to a
 * descriptive string, such as `enums.message_type.enum_type(var.foo.bar).name()`. Otherwise,
 * returns the argument as it is.
 */
String getFormattedPrintArgName(ImportTypeTable typeTable, TypeModel type, String variable, List<String> accessors) {
    String arg = variable + String.join("", accessors);
    // We print the argument as it is if it's not an enum type
    if (!(type instanceof ProtoTypeRef) || !((ProtoTypeRef) type).isEnum()) {
        return arg;
    }
    // Find all the parent elements of this enum type, stopping at the top-level message or enum
    TypeRef protoType = ((ProtoTypeRef) type).getProtoType();
    ProtoElement t = protoType.getEnumType();
    List<String> names = new ArrayList<>();
    while (!(t instanceof ProtoFile)) {
        names.add(t.getSimpleName());
        t = t.getParent();
    }
    // Wrap the enum value with the helper function that translates it to a descriptive string
    names.add("enums");
    StringBuilder builder = new StringBuilder();
    for (String name : Lists.reverse(names)) {
        builder.append(name).append(".");
    }
    builder.setLength(builder.length() - 1);
    return builder.append("(").append(arg).append(").name").toString();
}
Also used : ProtoTypeRef(com.google.api.codegen.config.ProtoTypeRef) TypeRef(com.google.api.tools.framework.model.TypeRef) ProtoTypeRef(com.google.api.codegen.config.ProtoTypeRef) ProtoElement(com.google.api.tools.framework.model.ProtoElement) ArrayList(java.util.ArrayList) ProtoFile(com.google.api.tools.framework.model.ProtoFile)

Example 18 with ProtoFile

use of com.google.api.tools.framework.model.ProtoFile 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 19 with ProtoFile

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

the class JavaModelTypeNameConverter method getResourceNamePackage.

private static String getResourceNamePackage(FieldConfig fieldConfig) {
    String commonResourceName = fieldConfig.getResourceNameConfig().getCommonResourceName();
    if (commonResourceName != null) {
        // Common resource name is fully-qualified.
        int p = commonResourceName.lastIndexOf(".");
        if (p >= 0) {
            commonResourceName = commonResourceName.substring(0, p);
        }
        return commonResourceName;
    }
    ResourceNameType resourceNameType = fieldConfig.getResourceNameConfig().getResourceNameType();
    switch(resourceNameType) {
        case ANY:
            return "com.google.api.resourcenames";
        case FIXED:
        case SINGLE:
        case ONEOF:
            ProtoFile assignedProtoFile = fieldConfig.getResourceNameConfig().getAssignedProtoFile();
            if (assignedProtoFile == null) {
                FieldModel fm = fieldConfig.getField();
                assignedProtoFile = ((ProtoField) fm).getProtoField().getFile();
            }
            return getJavaPackage(assignedProtoFile);
        case NONE:
        default:
            throw new IllegalArgumentException("Unexpected ResourceNameType: " + resourceNameType);
    }
}
Also used : ProtoField(com.google.api.codegen.config.ProtoField) ProtoFile(com.google.api.tools.framework.model.ProtoFile) ResourceNameType(com.google.api.codegen.config.ResourceNameType) FieldModel(com.google.api.codegen.config.FieldModel)

Aggregations

ProtoFile (com.google.api.tools.framework.model.ProtoFile)19 MessageType (com.google.api.tools.framework.model.MessageType)9 Field (com.google.api.tools.framework.model.Field)5 TypeRef (com.google.api.tools.framework.model.TypeRef)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 ArrayList (java.util.ArrayList)4 ResourceDescriptor (com.google.api.ResourceDescriptor)3 ProtoElement (com.google.api.tools.framework.model.ProtoElement)3 HashSet (java.util.HashSet)3 ProtoFileView (com.google.api.codegen.ProtoFileView)2 ResourceNameMessageConfigProto (com.google.api.codegen.ResourceNameMessageConfigProto)2 ProtoTypeRef (com.google.api.codegen.config.ProtoTypeRef)2 ViewModel (com.google.api.codegen.viewmodel.ViewModel)2 Interface (com.google.api.tools.framework.model.Interface)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Map (java.util.Map)2