use of com.google.protobuf.DescriptorProtos.OneofDescriptorProto in project aws-glue-schema-registry by awslabs.
the class FileDescriptorUtils method toMessage.
private static MessageElement toMessage(FileDescriptorProto file, DescriptorProto descriptor) {
String name = descriptor.getName();
ImmutableList.Builder<FieldElement> fields = ImmutableList.builder();
ImmutableList.Builder<TypeElement> nested = ImmutableList.builder();
ImmutableList.Builder<ReservedElement> reserved = ImmutableList.builder();
ImmutableList.Builder<ExtensionsElement> extensions = ImmutableList.builder();
LinkedHashMap<String, ImmutableList.Builder<FieldElement>> oneofsMap = new LinkedHashMap<>();
for (OneofDescriptorProto od : descriptor.getOneofDeclList()) {
oneofsMap.put(od.getName(), ImmutableList.builder());
}
List<Map.Entry<String, ImmutableList.Builder<FieldElement>>> oneofs = new ArrayList<>(oneofsMap.entrySet());
List<FieldElement> proto3OptionalFields = new ArrayList<>();
for (FieldDescriptorProto fd : descriptor.getFieldList()) {
if (fd.hasProto3Optional()) {
proto3OptionalFields.add(toField(file, fd, false));
continue;
}
if (fd.hasOneofIndex()) {
FieldElement field = toField(file, fd, true);
oneofs.get(fd.getOneofIndex()).getValue().add(field);
} else {
FieldElement field = toField(file, fd, false);
fields.add(field);
}
}
fields.addAll(proto3OptionalFields);
for (DescriptorProto nestedDesc : descriptor.getNestedTypeList()) {
MessageElement nestedMessage = toMessage(file, nestedDesc);
nested.add(nestedMessage);
}
for (EnumDescriptorProto nestedDesc : descriptor.getEnumTypeList()) {
EnumElement nestedEnum = toEnum(nestedDesc);
nested.add(nestedEnum);
}
for (String reservedName : descriptor.getReservedNameList()) {
ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", Collections.singletonList(reservedName));
reserved.add(reservedElem);
}
for (DescriptorProto.ReservedRange reservedRange : descriptor.getReservedRangeList()) {
List<IntRange> values = new ArrayList<>();
int start = reservedRange.getStart();
int end = reservedRange.getEnd() - 1;
values.add(new IntRange(start, end));
ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", values);
reserved.add(reservedElem);
}
for (DescriptorProto.ExtensionRange extensionRange : descriptor.getExtensionRangeList()) {
List<IntRange> values = new ArrayList<>();
int start = extensionRange.getStart();
int end = extensionRange.getEnd() - 1;
values.add(new IntRange(start, end));
ExtensionsElement extensionsElement = new ExtensionsElement(DEFAULT_LOCATION, "", values);
extensions.add(extensionsElement);
}
ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
if (descriptor.getOptions().hasMapEntry()) {
OptionElement option = new OptionElement(MAP_ENTRY_OPTION, booleanKind, descriptor.getOptions().getMapEntry(), false);
options.add(option);
}
if (descriptor.getOptions().hasNoStandardDescriptorAccessor()) {
OptionElement option = new OptionElement(NO_STANDARD_DESCRIPTOR_OPTION, booleanKind, descriptor.getOptions().getNoStandardDescriptorAccessor(), false);
options.add(option);
}
return new MessageElement(DEFAULT_LOCATION, name, "", nested.build(), options.build(), reserved.build(), fields.build(), oneofs.stream().filter(e -> e.getValue().build().size() != 0).map(e -> toOneof(e.getKey(), e.getValue())).collect(Collectors.toList()), extensions.build(), Collections.emptyList());
}
use of com.google.protobuf.DescriptorProtos.OneofDescriptorProto in project apicurio-registry by Apicurio.
the class FileDescriptorUtils method toMessage.
private static MessageElement toMessage(FileDescriptorProto file, DescriptorProto descriptor) {
String name = descriptor.getName();
ImmutableList.Builder<FieldElement> fields = ImmutableList.builder();
ImmutableList.Builder<TypeElement> nested = ImmutableList.builder();
ImmutableList.Builder<ReservedElement> reserved = ImmutableList.builder();
ImmutableList.Builder<ExtensionsElement> extensions = ImmutableList.builder();
LinkedHashMap<String, ImmutableList.Builder<FieldElement>> oneofsMap = new LinkedHashMap<>();
for (OneofDescriptorProto od : descriptor.getOneofDeclList()) {
oneofsMap.put(od.getName(), ImmutableList.builder());
}
List<Map.Entry<String, ImmutableList.Builder<FieldElement>>> oneofs = new ArrayList<>(oneofsMap.entrySet());
List<FieldElement> proto3OptionalFields = new ArrayList<>();
for (FieldDescriptorProto fd : descriptor.getFieldList()) {
if (fd.hasProto3Optional()) {
proto3OptionalFields.add(toField(file, fd, false));
continue;
}
if (fd.hasOneofIndex()) {
FieldElement field = toField(file, fd, true);
oneofs.get(fd.getOneofIndex()).getValue().add(field);
} else {
FieldElement field = toField(file, fd, false);
fields.add(field);
}
}
fields.addAll(proto3OptionalFields);
for (DescriptorProto nestedDesc : descriptor.getNestedTypeList()) {
MessageElement nestedMessage = toMessage(file, nestedDesc);
nested.add(nestedMessage);
}
for (EnumDescriptorProto nestedDesc : descriptor.getEnumTypeList()) {
EnumElement nestedEnum = toEnum(nestedDesc);
nested.add(nestedEnum);
}
for (String reservedName : descriptor.getReservedNameList()) {
ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", Collections.singletonList(reservedName));
reserved.add(reservedElem);
}
for (DescriptorProto.ReservedRange reservedRange : descriptor.getReservedRangeList()) {
List<IntRange> values = new ArrayList<>();
int start = reservedRange.getStart();
int end = reservedRange.getEnd() - 1;
values.add(new IntRange(start, end));
ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", values);
reserved.add(reservedElem);
}
for (DescriptorProto.ExtensionRange extensionRange : descriptor.getExtensionRangeList()) {
List<IntRange> values = new ArrayList<>();
int start = extensionRange.getStart();
int end = extensionRange.getEnd() - 1;
values.add(new IntRange(start, end));
ExtensionsElement extensionsElement = new ExtensionsElement(DEFAULT_LOCATION, "", values);
extensions.add(extensionsElement);
}
ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
if (descriptor.getOptions().hasMapEntry()) {
OptionElement option = new OptionElement(MAP_ENTRY_OPTION, booleanKind, descriptor.getOptions().getMapEntry(), false);
options.add(option);
}
if (descriptor.getOptions().hasNoStandardDescriptorAccessor()) {
OptionElement option = new OptionElement(NO_STANDARD_DESCRIPTOR_OPTION, booleanKind, descriptor.getOptions().getNoStandardDescriptorAccessor(), false);
options.add(option);
}
return new MessageElement(DEFAULT_LOCATION, name, "", nested.build(), options.build(), reserved.build(), fields.build(), oneofs.stream().filter(e -> e.getValue().build().size() != 0).map(e -> toOneof(e.getKey(), e.getValue())).collect(Collectors.toList()), extensions.build(), Collections.emptyList());
}
use of com.google.protobuf.DescriptorProtos.OneofDescriptorProto in project schema-registry by confluentinc.
the class ProtobufSchema method toMessage.
private static MessageElement toMessage(FileDescriptorProto file, DescriptorProto descriptor) {
String name = descriptor.getName();
log.trace("*** msg name: {}", name);
ImmutableList.Builder<FieldElement> fields = ImmutableList.builder();
ImmutableList.Builder<TypeElement> nested = ImmutableList.builder();
ImmutableList.Builder<ReservedElement> reserved = ImmutableList.builder();
LinkedHashMap<String, ImmutableList.Builder<FieldElement>> oneofsMap = new LinkedHashMap<>();
for (OneofDescriptorProto od : descriptor.getOneofDeclList()) {
oneofsMap.put(od.getName(), ImmutableList.builder());
}
List<Map.Entry<String, ImmutableList.Builder<FieldElement>>> oneofs = new ArrayList<>(oneofsMap.entrySet());
for (FieldDescriptorProto fd : descriptor.getFieldList()) {
if (fd.hasOneofIndex() && !fd.getProto3Optional()) {
FieldElement field = toField(file, fd, true);
oneofs.get(fd.getOneofIndex()).getValue().add(field);
} else {
FieldElement field = toField(file, fd, false);
fields.add(field);
}
}
for (DescriptorProto nestedDesc : descriptor.getNestedTypeList()) {
MessageElement nestedMessage = toMessage(file, nestedDesc);
nested.add(nestedMessage);
}
for (EnumDescriptorProto nestedDesc : descriptor.getEnumTypeList()) {
EnumElement nestedEnum = toEnum(nestedDesc);
nested.add(nestedEnum);
}
for (ReservedRange range : descriptor.getReservedRangeList()) {
ReservedElement reservedElem = toReserved(range);
reserved.add(reservedElem);
}
for (String reservedName : descriptor.getReservedNameList()) {
ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", Collections.singletonList(reservedName));
reserved.add(reservedElem);
}
ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
if (descriptor.getOptions().hasNoStandardDescriptorAccessor()) {
OptionElement option = new OptionElement(NO_STANDARD_DESCRIPTOR_ACCESSOR, Kind.BOOLEAN, descriptor.getOptions().getNoStandardDescriptorAccessor(), false);
options.add(option);
}
if (descriptor.getOptions().hasDeprecated()) {
OptionElement option = new OptionElement(DEPRECATED, Kind.BOOLEAN, descriptor.getOptions().getDeprecated(), false);
options.add(option);
}
if (descriptor.getOptions().hasMapEntry()) {
OptionElement option = new OptionElement(MAP_ENTRY, Kind.BOOLEAN, descriptor.getOptions().getMapEntry(), false);
options.add(option);
}
if (descriptor.getOptions().hasExtension(MetaProto.messageMeta)) {
Meta meta = descriptor.getOptions().getExtension(MetaProto.messageMeta);
OptionElement option = toOption(CONFLUENT_MESSAGE_META, meta);
if (option != null) {
options.add(option);
}
}
// NOTE: skip extensions, groups
return new MessageElement(DEFAULT_LOCATION, name, "", nested.build(), options.build(), reserved.build(), fields.build(), oneofs.stream().map(e -> toOneof(e.getKey(), e.getValue())).filter(e -> !e.getFields().isEmpty()).collect(Collectors.toList()), Collections.emptyList(), Collections.emptyList());
}
Aggregations