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;
}
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();
}
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;
}
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);
}
}
Aggregations