use of com.squareup.wire.schema.internal.parser.EnumElement in project aws-glue-schema-registry by awslabs.
the class FileDescriptorUtils method fileDescriptorToProtoFile.
public static ProtoFileElement fileDescriptorToProtoFile(FileDescriptorProto file) {
String packageName = file.getPackage();
if ("".equals(packageName)) {
packageName = null;
}
Syntax syntax = null;
switch(file.getSyntax()) {
case PROTO2:
syntax = Syntax.PROTO_2;
break;
case PROTO3:
syntax = Syntax.PROTO_3;
break;
default:
break;
}
ImmutableList.Builder<TypeElement> types = ImmutableList.builder();
for (DescriptorProto md : file.getMessageTypeList()) {
MessageElement message = toMessage(file, md);
types.add(message);
}
for (EnumDescriptorProto ed : file.getEnumTypeList()) {
EnumElement enumer = toEnum(ed);
types.add(enumer);
}
ImmutableList.Builder<ServiceElement> services = ImmutableList.builder();
for (ServiceDescriptorProto sv : file.getServiceList()) {
ServiceElement service = toService(sv);
services.add(service);
}
ImmutableList.Builder<String> imports = ImmutableList.builder();
ImmutableList.Builder<String> publicImports = ImmutableList.builder();
List<String> dependencyList = file.getDependencyList();
Set<Integer> publicDependencyList = new HashSet<>(file.getPublicDependencyList());
for (int i = 0; i < dependencyList.size(); i++) {
String depName = dependencyList.get(i);
if (publicDependencyList.contains(i)) {
publicImports.add(depName);
} else {
imports.add(depName);
}
}
ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
if (file.getOptions().hasJavaPackage()) {
OptionElement option = new OptionElement(JAVA_PACKAGE_OPTION, stringKind, file.getOptions().getJavaPackage(), false);
options.add(option);
}
if (file.getOptions().hasJavaOuterClassname()) {
OptionElement option = new OptionElement(JAVA_OUTER_CLASSNAME_OPTION, stringKind, file.getOptions().getJavaOuterClassname(), false);
options.add(option);
}
if (file.getOptions().hasJavaMultipleFiles()) {
OptionElement option = new OptionElement(JAVA_MULTIPLE_FILES_OPTION, booleanKind, file.getOptions().getJavaMultipleFiles(), false);
options.add(option);
}
if (file.getOptions().hasJavaGenericServices()) {
OptionElement option = new OptionElement(JAVA_GENERIC_SERVICES_OPTION, booleanKind, file.getOptions().getJavaGenericServices(), false);
options.add(option);
}
if (file.getOptions().hasJavaStringCheckUtf8()) {
OptionElement option = new OptionElement(JAVA_STRING_CHECK_UTF8_OPTION, booleanKind, file.getOptions().getJavaStringCheckUtf8(), false);
options.add(option);
}
if (file.getOptions().hasCcGenericServices()) {
OptionElement option = new OptionElement(CC_GENERIC_SERVICES_OPTION, booleanKind, file.getOptions().getCcGenericServices(), false);
options.add(option);
}
if (file.getOptions().hasCcEnableArenas()) {
OptionElement option = new OptionElement(CC_ENABLE_ARENAS_OPTION, booleanKind, file.getOptions().getCcEnableArenas(), false);
options.add(option);
}
if (file.getOptions().hasCsharpNamespace()) {
OptionElement option = new OptionElement(CSHARP_NAMESPACE_OPTION, stringKind, file.getOptions().getCsharpNamespace(), false);
options.add(option);
}
if (file.getOptions().hasGoPackage()) {
OptionElement option = new OptionElement(GO_PACKAGE_OPTION, stringKind, file.getOptions().getGoPackage(), false);
options.add(option);
}
if (file.getOptions().hasObjcClassPrefix()) {
OptionElement option = new OptionElement(OBJC_CLASS_PREFIX_OPTION, stringKind, file.getOptions().getObjcClassPrefix(), false);
options.add(option);
}
if (file.getOptions().hasPhpClassPrefix()) {
OptionElement option = new OptionElement(PHP_CLASS_PREFIX_OPTION, stringKind, file.getOptions().getPhpClassPrefix(), false);
options.add(option);
}
if (file.getOptions().hasPhpGenericServices()) {
OptionElement option = new OptionElement(PHP_GENERIC_SERVICES_OPTION, booleanKind, file.getOptions().getPhpGenericServices(), false);
options.add(option);
}
if (file.getOptions().hasPhpMetadataNamespace()) {
OptionElement option = new OptionElement(PHP_METADATA_NAMESPACE_OPTION, stringKind, file.getOptions().getPhpMetadataNamespace(), false);
options.add(option);
}
if (file.getOptions().hasPhpNamespace()) {
OptionElement option = new OptionElement(PHP_NAMESPACE_OPTION, stringKind, file.getOptions().getPhpNamespace(), false);
options.add(option);
}
if (file.getOptions().hasPyGenericServices()) {
OptionElement option = new OptionElement(PY_GENERIC_SERVICES_OPTION, booleanKind, file.getOptions().getPyGenericServices(), false);
options.add(option);
}
if (file.getOptions().hasRubyPackage()) {
OptionElement option = new OptionElement(RUBY_PACKAGE_OPTION, stringKind, file.getOptions().getRubyPackage(), false);
options.add(option);
}
if (file.getOptions().hasSwiftPrefix()) {
OptionElement option = new OptionElement(SWIFT_PREFIX_OPTION, stringKind, file.getOptions().getSwiftPrefix(), false);
options.add(option);
}
if (file.getOptions().hasOptimizeFor()) {
OptionElement option = new OptionElement(OPTIMIZE_FOR_OPTION, enumKind, file.getOptions().getOptimizeFor(), false);
options.add(option);
}
return new ProtoFileElement(DEFAULT_LOCATION, packageName, syntax, imports.build(), publicImports.build(), types.build(), services.build(), Collections.emptyList(), options.build());
}
use of com.squareup.wire.schema.internal.parser.EnumElement in project aws-glue-schema-registry by awslabs.
the class FileDescriptorUtils method toEnum.
private static EnumElement toEnum(EnumDescriptorProto ed) {
String name = ed.getName();
ImmutableList.Builder<EnumConstantElement> constants = ImmutableList.builder();
for (EnumValueDescriptorProto ev : ed.getValueList()) {
ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
constants.add(new EnumConstantElement(DEFAULT_LOCATION, ev.getName(), ev.getNumber(), "", options.build()));
}
ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
if (ed.getOptions().hasAllowAlias()) {
OptionElement option = new OptionElement(ALLOW_ALIAS_OPTION, booleanKind, ed.getOptions().getAllowAlias(), false);
options.add(option);
}
return new EnumElement(DEFAULT_LOCATION, name, "", options.build(), constants.build());
}
use of com.squareup.wire.schema.internal.parser.EnumElement 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.squareup.wire.schema.internal.parser.EnumElement in project aws-glue-schema-registry by awslabs.
the class ProtobufFile method processMessageElement.
@SuppressWarnings({ "rawtypes", "unchecked" })
private void processMessageElement(String scope, MessageElement messageElement) {
// reservedFields
Set<Object> reservedFieldSet = new HashSet<>();
for (ReservedElement reservedElement : messageElement.getReserveds()) {
for (Object value : reservedElement.getValues()) {
if (value instanceof Range) {
reservedFieldSet.addAll(ContiguousSet.create((Range) value, DiscreteDomain.integers()));
} else {
reservedFieldSet.add(value);
}
}
}
if (!reservedFieldSet.isEmpty()) {
reservedFields.put(scope + messageElement.getName(), reservedFieldSet);
}
// fieldMap, mapMap, FieldsIDName
Map<String, FieldElement> fieldTypeMap = new HashMap<>();
Map<String, FieldElement> mapMap = new HashMap<>();
Map<Integer, String> idsToNames = new HashMap<>();
for (FieldElement fieldElement : messageElement.getFields()) {
fieldTypeMap.put(fieldElement.getName(), fieldElement);
if (fieldElement.getType().startsWith("map<")) {
mapMap.put(fieldElement.getName(), fieldElement);
}
idsToNames.put(fieldElement.getTag(), fieldElement.getName());
}
for (OneOfElement oneOfElement : messageElement.getOneOfs()) {
for (FieldElement fieldElement : oneOfElement.getFields()) {
fieldTypeMap.put(fieldElement.getName(), fieldElement);
if (fieldElement.getType().startsWith("map<")) {
mapMap.put(fieldElement.getName(), fieldElement);
}
idsToNames.put(fieldElement.getTag(), fieldElement.getName());
}
}
if (!fieldTypeMap.isEmpty()) {
fieldMap.put(scope + messageElement.getName(), fieldTypeMap);
}
if (!mapMap.isEmpty()) {
this.mapMap.put(scope + messageElement.getName(), mapMap);
}
if (!idsToNames.isEmpty()) {
fieldsById.put(scope + messageElement.getName(), idsToNames);
}
// nonReservedFields
Set<Object> fieldKeySet = new HashSet<>();
for (FieldElement fieldElement : messageElement.getFields()) {
fieldKeySet.add(fieldElement.getTag());
fieldKeySet.add(fieldElement.getName());
}
for (OneOfElement oneOfElement : messageElement.getOneOfs()) {
for (FieldElement fieldElement : oneOfElement.getFields()) {
fieldKeySet.add(fieldElement.getTag());
fieldKeySet.add(fieldElement.getName());
}
}
if (!fieldKeySet.isEmpty()) {
nonReservedFields.put(scope + messageElement.getName(), fieldKeySet);
}
for (TypeElement typeElement : messageElement.getNestedTypes()) {
if (typeElement instanceof MessageElement) {
processMessageElement(messageElement.getName() + ".", (MessageElement) typeElement);
} else if (typeElement instanceof EnumElement) {
processEnumElement(messageElement.getName() + ".", (EnumElement) typeElement);
}
}
}
use of com.squareup.wire.schema.internal.parser.EnumElement in project schema-registry by confluentinc.
the class ProtobufSchema method toEnum.
private static EnumElement toEnum(EnumDescriptorProto ed) {
String name = ed.getName();
log.trace("*** enum name: {}", name);
ImmutableList.Builder<EnumConstantElement> constants = ImmutableList.builder();
for (EnumValueDescriptorProto ev : ed.getValueList()) {
ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
if (ev.getOptions().hasDeprecated()) {
OptionElement option = new OptionElement(DEPRECATED, Kind.BOOLEAN, ev.getOptions().getDeprecated(), false);
options.add(option);
}
if (ev.getOptions().hasExtension(MetaProto.enumValueMeta)) {
Meta meta = ev.getOptions().getExtension(MetaProto.enumValueMeta);
OptionElement option = toOption(CONFLUENT_ENUM_VALUE_META, meta);
if (option != null) {
options.add(option);
}
}
constants.add(new EnumConstantElement(DEFAULT_LOCATION, ev.getName(), ev.getNumber(), "", options.build()));
}
ImmutableList.Builder<ReservedElement> reserved = ImmutableList.builder();
for (EnumReservedRange range : ed.getReservedRangeList()) {
ReservedElement reservedElem = toReserved(range);
reserved.add(reservedElem);
}
for (String reservedName : ed.getReservedNameList()) {
ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", Collections.singletonList(reservedName));
reserved.add(reservedElem);
}
ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
if (ed.getOptions().hasAllowAlias()) {
OptionElement option = new OptionElement(ALLOW_ALIAS, Kind.BOOLEAN, ed.getOptions().getAllowAlias(), false);
options.add(option);
}
if (ed.getOptions().hasDeprecated()) {
OptionElement option = new OptionElement(DEPRECATED, Kind.BOOLEAN, ed.getOptions().getDeprecated(), false);
options.add(option);
}
if (ed.getOptions().hasExtension(MetaProto.enumMeta)) {
Meta meta = ed.getOptions().getExtension(MetaProto.enumMeta);
OptionElement option = toOption(CONFLUENT_ENUM_META, meta);
if (option != null) {
options.add(option);
}
}
return new EnumElement(DEFAULT_LOCATION, name, "", options.build(), constants.build(), reserved.build());
}
Aggregations