use of com.google.api.codegen.viewmodel.ResourceProtoFieldView in project toolkit by googleapis.
the class PathTemplateTransformer method generateResourceProtos.
public List<ResourceProtoView> generateResourceProtos(GapicInterfaceContext context) {
SurfaceNamer namer = context.getNamer();
ResourceNameMessageConfigs resourceConfigs = context.getProductConfig().getResourceNameMessageConfigs();
ListMultimap<String, FieldModel> fieldsByMessage = resourceConfigs.getFieldsWithResourceNamesByMessage();
Map<String, FieldConfig> fieldConfigMap = context.getProductConfig().getDefaultResourceNameFieldConfigMap();
List<ResourceProtoView> protos = new ArrayList<>();
for (Entry<String, Collection<FieldModel>> entry : fieldsByMessage.asMap().entrySet()) {
String msgName = entry.getKey();
Collection<FieldModel> fields = new ArrayList<>(entry.getValue());
ResourceProtoView.Builder protoBuilder = ResourceProtoView.newBuilder();
protoBuilder.protoClassName(namer.getTypeNameConverter().getTypeName(msgName).getNickname());
List<ResourceProtoFieldView> fieldViews = new ArrayList<>();
for (FieldModel field : fields) {
FieldConfig fieldConfig = fieldConfigMap.get(field.getFullName());
String fieldTypeSimpleName = namer.getResourceTypeName(fieldConfig.getResourceNameConfig());
String fieldTypeName = context.getImportTypeTable().getAndSaveNicknameForTypedResourceName(fieldConfig, fieldTypeSimpleName);
if (field.isRepeated()) {
fieldTypeName = fieldTypeName.replaceFirst("IEnumerable", "ResourceNameList");
}
String fieldDocTypeName = fieldTypeName.replace('<', '{').replace('>', '}');
String fieldElementTypeName = context.getImportTypeTable().getAndSaveNicknameForResourceNameElementType(fieldConfig, fieldTypeSimpleName);
ResourceProtoFieldView fieldView = ResourceProtoFieldView.newBuilder().typeName(fieldTypeName).parseMethodTypeName(namer.getPackageName() + "." + fieldTypeName).docTypeName(fieldDocTypeName).elementTypeName(fieldElementTypeName).isAny(fieldConfig.getResourceNameType() == ResourceNameType.ANY).isRepeated(field.isRepeated()).isOneof(fieldConfig.getResourceNameType() == ResourceNameType.ONEOF).propertyName(namer.getResourceNameFieldGetFunctionName(fieldConfig)).underlyingPropertyName(namer.publicMethodName(Name.from(field.getSimpleName()))).build();
fieldViews.add(fieldView);
}
protoBuilder.fields(fieldViews);
protos.add(protoBuilder.build());
}
Collections.sort(protos, new Comparator<ResourceProtoView>() {
@Override
public int compare(ResourceProtoView a, ResourceProtoView b) {
return a.protoClassName().compareTo(b.protoClassName());
}
});
return protos;
}
use of com.google.api.codegen.viewmodel.ResourceProtoFieldView in project toolkit by googleapis.
the class CSharpGapicClientTransformer method generateResourceProtos.
private List<ResourceProtoView> generateResourceProtos(GapicInterfaceContext context) {
SurfaceNamer namer = context.getNamer();
ResourceNameMessageConfigs resourceConfigs = context.getProductConfig().getResourceNameMessageConfigs();
String csharpDefaultNamespace = context.getProductConfig().getPackageName();
ListMultimap<String, FieldModel> fieldsByMessage = resourceConfigs.getFieldsWithResourceNamesByMessage();
Map<String, FieldConfig> fieldConfigMap = context.getProductConfig().getDefaultResourceNameFieldConfigMap();
List<ResourceProtoView> protos = new ArrayList<>();
// Get the default proto package name for this interface.
String defaultPackageName = context.getInterfaceModel().getParentFullName();
for (Entry<String, Collection<FieldModel>> entry : fieldsByMessage.asMap().entrySet()) {
String msgName = entry.getKey();
if (!msgName.startsWith(defaultPackageName)) {
// If the proto is not in this package, don't generate a partial class for it.
continue;
}
// msgName is the "<proto package name>.<C# proto class name>".
// Remove the proto package name, and prepend the C# namespace
msgName = csharpDefaultNamespace + msgName.substring(msgName.lastIndexOf('.'));
Collection<FieldModel> fields = new ArrayList<>(entry.getValue());
ResourceProtoView.Builder protoBuilder = ResourceProtoView.newBuilder();
protoBuilder.protoClassName(namer.getTypeNameConverter().getTypeName(msgName).getNickname());
List<ResourceProtoFieldView> fieldViews = new ArrayList<>();
for (FieldModel field : fields) {
FieldConfig fieldConfig = fieldConfigMap.get(field.getFullName());
ResourceNameConfig resourceNameConfig = fieldConfig.getResourceNameConfig();
String fieldTypeSimpleName = namer.getResourceTypeName(resourceNameConfig);
boolean isAny = false;
if (fieldTypeSimpleName.equals("IResourceName")) {
fieldTypeSimpleName = CSharpTypeTable.ALIAS_GAX + "::IResourceName";
isAny = true;
}
String fieldTypeName = context.getImportTypeTable().getAndSaveNicknameForTypedResourceName(fieldConfig, fieldTypeSimpleName);
if (field.isRepeated()) {
fieldTypeName = fieldTypeName.replaceFirst(CSharpTypeTable.ALIAS_SYSTEM_COLLECTIONS_GENERIC + "::IEnumerable", CSharpTypeTable.ALIAS_GAX + "::ResourceNameList");
} else if (resourceNameConfig.getCommonResourceName() == null && !isAny) {
// Needs to be fully qualifed because the 'fieldTypeName' class name will be
// the same as a property name on this proto message.
fieldTypeName = namer.getPackageName() + "." + fieldTypeName;
}
String fieldDocTypeName = fieldTypeName.replace('<', '{').replace('>', '}');
String fieldElementTypeName = context.getImportTypeTable().getAndSaveNicknameForResourceNameElementType(fieldConfig, fieldTypeSimpleName);
ResourceProtoFieldView fieldView = ResourceProtoFieldView.newBuilder().typeName(fieldTypeName).parseMethodTypeName(fieldTypeName).docTypeName(fieldDocTypeName).elementTypeName(fieldElementTypeName).isAny(fieldConfig.getResourceNameType() == ResourceNameType.ANY).isRepeated(field.isRepeated()).isOneof(fieldConfig.getResourceNameType() == ResourceNameType.ONEOF).propertyName(namer.getResourceNameFieldGetFunctionName(fieldConfig)).underlyingPropertyName(namer.publicMethodName(Name.from(field.getSimpleName()))).build();
fieldViews.add(fieldView);
}
protoBuilder.fields(fieldViews);
protos.add(protoBuilder.build());
}
// Sort alphabetically, to make generated code deterministic.
Collections.sort(protos, new Comparator<ResourceProtoView>() {
@Override
public int compare(ResourceProtoView a, ResourceProtoView b) {
return a.protoClassName().compareTo(b.protoClassName());
}
});
return protos;
}
Aggregations