use of com.squareup.wire.schema.internal.parser.MessageElement 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.squareup.wire.schema.internal.parser.MessageElement in project apicurio-registry by Apicurio.
the class ProtobufContentValidator method validate.
/**
* @see io.apicurio.registry.rules.validity.ContentValidator#validate(ValidityLevel, ContentHandle, Map)
*/
@Override
public void validate(ValidityLevel level, ContentHandle artifactContent, Map<String, ContentHandle> resolvedReferences) throws RuleViolationException {
if (level == ValidityLevel.SYNTAX_ONLY || level == ValidityLevel.FULL) {
try {
if (resolvedReferences == null || resolvedReferences.isEmpty()) {
ProtobufFile.toProtoFileElement(artifactContent.content());
} else {
final ProtoFileElement protoFileElement = ProtobufFile.toProtoFileElement(artifactContent.content());
final Map<String, ProtoFileElement> dependencies = Collections.unmodifiableMap(resolvedReferences.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> ProtobufFile.toProtoFileElement(e.getValue().content()))));
MessageElement firstMessage = FileDescriptorUtils.firstMessage(protoFileElement);
if (firstMessage != null) {
try {
final Descriptors.Descriptor fileDescriptor = FileDescriptorUtils.toDescriptor(firstMessage.getName(), protoFileElement, dependencies);
ContentHandle.create(fileDescriptor.toString());
} catch (IllegalStateException ise) {
// If we fail to init the dynamic schema, try to get the descriptor from the proto element
ContentHandle.create(getFileDescriptorFromElement(protoFileElement).toString());
}
} else {
ContentHandle.create(getFileDescriptorFromElement(protoFileElement).toString());
}
}
} catch (Exception e) {
throw new RuleViolationException("Syntax violation for Protobuf artifact.", RuleType.VALIDITY, level.name(), e);
}
}
}
use of com.squareup.wire.schema.internal.parser.MessageElement in project thingsboard by thingsboard.
the class ProtoTransportPayloadConfiguration method getMessageDefinitions.
private List<MessageDefinition> getMessageDefinitions(List<MessageElement> messageElementsList) {
if (!messageElementsList.isEmpty()) {
List<MessageDefinition> messageDefinitions = new ArrayList<>();
messageElementsList.forEach(messageElement -> {
MessageDefinition.Builder messageDefinitionBuilder = MessageDefinition.newBuilder(messageElement.getName());
List<TypeElement> nestedTypes = messageElement.getNestedTypes();
if (!nestedTypes.isEmpty()) {
List<EnumElement> nestedEnumTypes = getEnumElements(nestedTypes);
if (!nestedEnumTypes.isEmpty()) {
nestedEnumTypes.forEach(enumElement -> {
EnumDefinition nestedEnumDefinition = getEnumDefinition(enumElement);
messageDefinitionBuilder.addEnumDefinition(nestedEnumDefinition);
});
}
List<MessageElement> nestedMessageTypes = getMessageTypes(nestedTypes);
List<MessageDefinition> nestedMessageDefinitions = getMessageDefinitions(nestedMessageTypes);
nestedMessageDefinitions.forEach(messageDefinitionBuilder::addMessageDefinition);
}
List<FieldElement> messageElementFields = messageElement.getFields();
List<OneOfElement> oneOfs = messageElement.getOneOfs();
if (!oneOfs.isEmpty()) {
for (OneOfElement oneOfelement : oneOfs) {
MessageDefinition.OneofBuilder oneofBuilder = messageDefinitionBuilder.addOneof(oneOfelement.getName());
addMessageFieldsToTheOneOfDefinition(oneOfelement.getFields(), oneofBuilder);
}
}
if (!messageElementFields.isEmpty()) {
addMessageFieldsToTheMessageDefinition(messageElementFields, messageDefinitionBuilder);
}
messageDefinitions.add(messageDefinitionBuilder.build());
});
return messageDefinitions;
} else {
return Collections.emptyList();
}
}
use of com.squareup.wire.schema.internal.parser.MessageElement in project schema-registry by confluentinc.
the class ProtobufSchema method toDynamicSchema.
private static DynamicSchema toDynamicSchema(String name, ProtoFileElement rootElem, Map<String, ProtoFileElement> dependencies) {
if (log.isTraceEnabled()) {
log.trace("*** toDynamicSchema: {}", ProtobufSchemaUtils.toString(rootElem));
}
DynamicSchema.Builder schema = DynamicSchema.newBuilder();
try {
Syntax syntax = rootElem.getSyntax();
if (syntax != null) {
schema.setSyntax(syntax.toString());
}
if (rootElem.getPackageName() != null) {
schema.setPackage(rootElem.getPackageName());
}
for (TypeElement typeElem : rootElem.getTypes()) {
if (typeElem instanceof MessageElement) {
MessageDefinition message = toDynamicMessage(syntax, (MessageElement) typeElem);
schema.addMessageDefinition(message);
} else if (typeElem instanceof EnumElement) {
EnumDefinition enumer = toDynamicEnum((EnumElement) typeElem);
schema.addEnumDefinition(enumer);
}
}
for (ServiceElement serviceElement : rootElem.getServices()) {
ServiceDefinition service = toDynamicService(serviceElement);
schema.addServiceDefinition(service);
}
for (String ref : rootElem.getImports()) {
ProtoFileElement dep = dependencies.get(ref);
if (dep != null) {
schema.addDependency(ref);
schema.addSchema(toDynamicSchema(ref, dep, dependencies));
}
}
for (String ref : rootElem.getPublicImports()) {
ProtoFileElement dep = dependencies.get(ref);
if (dep != null) {
schema.addPublicDependency(ref);
schema.addSchema(toDynamicSchema(ref, dep, dependencies));
}
}
Map<String, OptionElement> options = mergeOptions(rootElem.getOptions());
OptionElement javaPackageName = options.get(JAVA_PACKAGE);
if (javaPackageName != null) {
schema.setJavaPackage(javaPackageName.getValue().toString());
}
OptionElement javaOuterClassname = options.get(JAVA_OUTER_CLASSNAME);
if (javaOuterClassname != null) {
schema.setJavaOuterClassname(javaOuterClassname.getValue().toString());
}
OptionElement javaMultipleFiles = options.get(JAVA_MULTIPLE_FILES);
if (javaMultipleFiles != null) {
schema.setJavaMultipleFiles(Boolean.parseBoolean(javaMultipleFiles.getValue().toString()));
}
OptionElement javaStringCheckUtf8 = options.get(JAVA_STRING_CHECK_UTF8);
if (javaStringCheckUtf8 != null) {
schema.setJavaStringCheckUtf8(Boolean.parseBoolean(javaStringCheckUtf8.getValue().toString()));
}
OptionElement optimizeFor = options.get(OPTIMIZE_FOR);
if (optimizeFor != null) {
schema.setOptimizeFor(OptimizeMode.valueOf(optimizeFor.getValue().toString()));
}
OptionElement goPackage = options.get(GO_PACKAGE);
if (goPackage != null) {
schema.setGoPackage(goPackage.getValue().toString());
}
OptionElement ccGenericServices = options.get(CC_GENERIC_SERVICES);
if (ccGenericServices != null) {
schema.setCcGenericServices(Boolean.parseBoolean(ccGenericServices.getValue().toString()));
}
OptionElement javaGenericServices = options.get(JAVA_GENERIC_SERVICES);
if (javaGenericServices != null) {
schema.setJavaGenericServices(Boolean.parseBoolean(javaGenericServices.getValue().toString()));
}
OptionElement pyGenericServices = options.get(PY_GENERIC_SERVICES);
if (pyGenericServices != null) {
schema.setPyGenericServices(Boolean.parseBoolean(pyGenericServices.getValue().toString()));
}
OptionElement phpGenericServices = options.get(PHP_GENERIC_SERVICES);
if (phpGenericServices != null) {
schema.setPhpGenericServices(Boolean.parseBoolean(phpGenericServices.getValue().toString()));
}
OptionElement isDeprecated = options.get(DEPRECATED);
if (isDeprecated != null) {
schema.setDeprecated(Boolean.parseBoolean(isDeprecated.getValue().toString()));
}
OptionElement ccEnableArenas = options.get(CC_ENABLE_ARENAS);
if (ccEnableArenas != null) {
schema.setCcEnableArenas(Boolean.parseBoolean(ccEnableArenas.getValue().toString()));
}
OptionElement objcClassPrefix = options.get(OBJC_CLASS_PREFIX);
if (objcClassPrefix != null) {
schema.setObjcClassPrefix(objcClassPrefix.getValue().toString());
}
OptionElement csharpNamespace = options.get(CSHARP_NAMESPACE);
if (csharpNamespace != null) {
schema.setCsharpNamespace(csharpNamespace.getValue().toString());
}
OptionElement swiftPrefix = options.get(SWIFT_PREFIX);
if (swiftPrefix != null) {
schema.setSwiftPrefix(swiftPrefix.getValue().toString());
}
OptionElement phpClassPrefix = options.get(PHP_CLASS_PREFIX);
if (phpClassPrefix != null) {
schema.setPhpClassPrefix(phpClassPrefix.getValue().toString());
}
OptionElement phpNamespace = options.get(PHP_NAMESPACE);
if (phpNamespace != null) {
schema.setPhpNamespace(phpNamespace.getValue().toString());
}
OptionElement phpMetadataNamespace = options.get(PHP_METADATA_NAMESPACE);
if (phpMetadataNamespace != null) {
schema.setPhpMetadataNamespace(phpMetadataNamespace.getValue().toString());
}
OptionElement rubyPackage = options.get(RUBY_PACKAGE);
if (rubyPackage != null) {
schema.setRubyPackage(rubyPackage.getValue().toString());
}
Optional<OptionElement> meta = findOption(CONFLUENT_FILE_META, options);
String doc = findDoc(meta);
Map<String, String> params = findParams(meta);
schema.setMeta(doc, params);
schema.setName(name);
return schema.build();
} catch (Descriptors.DescriptorValidationException e) {
throw new IllegalStateException(e);
}
}
use of com.squareup.wire.schema.internal.parser.MessageElement in project schema-registry by confluentinc.
the class ProtobufSchema method toProtoFile.
private static ProtoFileElement toProtoFile(FileDescriptorProto file) {
String packageName = file.getPackage();
// Don't set empty package name
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 sd : file.getServiceList()) {
ServiceElement service = toService(sd);
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()) {
options.add(new OptionElement(JAVA_PACKAGE, Kind.STRING, file.getOptions().getJavaPackage(), false));
}
if (file.getOptions().hasJavaOuterClassname()) {
options.add(new OptionElement(JAVA_OUTER_CLASSNAME, Kind.STRING, file.getOptions().getJavaOuterClassname(), false));
}
if (file.getOptions().hasJavaMultipleFiles()) {
options.add(new OptionElement(JAVA_MULTIPLE_FILES, Kind.BOOLEAN, file.getOptions().getJavaMultipleFiles(), false));
}
if (file.getOptions().hasJavaStringCheckUtf8()) {
options.add(new OptionElement(JAVA_STRING_CHECK_UTF8, Kind.BOOLEAN, file.getOptions().getJavaStringCheckUtf8(), false));
}
if (file.getOptions().hasOptimizeFor()) {
options.add(new OptionElement(OPTIMIZE_FOR, Kind.ENUM, file.getOptions().getOptimizeFor(), false));
}
if (file.getOptions().hasGoPackage()) {
options.add(new OptionElement(GO_PACKAGE, Kind.STRING, file.getOptions().getGoPackage(), false));
}
if (file.getOptions().hasCcGenericServices()) {
options.add(new OptionElement(CC_GENERIC_SERVICES, Kind.BOOLEAN, file.getOptions().getCcGenericServices(), false));
}
if (file.getOptions().hasJavaGenericServices()) {
options.add(new OptionElement(JAVA_GENERIC_SERVICES, Kind.BOOLEAN, file.getOptions().getJavaGenericServices(), false));
}
if (file.getOptions().hasPyGenericServices()) {
options.add(new OptionElement(PY_GENERIC_SERVICES, Kind.BOOLEAN, file.getOptions().getPyGenericServices(), false));
}
if (file.getOptions().hasPhpGenericServices()) {
options.add(new OptionElement(PHP_GENERIC_SERVICES, Kind.BOOLEAN, file.getOptions().getPhpGenericServices(), false));
}
if (file.getOptions().hasDeprecated()) {
options.add(new OptionElement(DEPRECATED, Kind.BOOLEAN, file.getOptions().getDeprecated(), false));
}
if (file.getOptions().hasCcEnableArenas()) {
options.add(new OptionElement(CC_ENABLE_ARENAS, Kind.BOOLEAN, file.getOptions().getCcEnableArenas(), false));
}
if (file.getOptions().hasObjcClassPrefix()) {
options.add(new OptionElement(OBJC_CLASS_PREFIX, Kind.STRING, file.getOptions().getObjcClassPrefix(), false));
}
if (file.getOptions().hasCsharpNamespace()) {
options.add(new OptionElement(CSHARP_NAMESPACE, Kind.STRING, file.getOptions().getCsharpNamespace(), false));
}
if (file.getOptions().hasSwiftPrefix()) {
options.add(new OptionElement(SWIFT_PREFIX, Kind.STRING, file.getOptions().getSwiftPrefix(), false));
}
if (file.getOptions().hasPhpClassPrefix()) {
options.add(new OptionElement(PHP_CLASS_PREFIX, Kind.STRING, file.getOptions().getPhpClassPrefix(), false));
}
if (file.getOptions().hasPhpNamespace()) {
options.add(new OptionElement(PHP_NAMESPACE, Kind.STRING, file.getOptions().getPhpNamespace(), false));
}
if (file.getOptions().hasPhpMetadataNamespace()) {
options.add(new OptionElement(PHP_METADATA_NAMESPACE, Kind.STRING, file.getOptions().getPhpMetadataNamespace(), false));
}
if (file.getOptions().hasRubyPackage()) {
options.add(new OptionElement(RUBY_PACKAGE, Kind.STRING, file.getOptions().getRubyPackage(), false));
}
if (file.getOptions().hasExtension(MetaProto.fileMeta)) {
Meta meta = file.getOptions().getExtension(MetaProto.fileMeta);
OptionElement option = toOption(CONFLUENT_FILE_META, meta);
if (option != null) {
options.add(option);
}
}
// NOTE: skip extensions
return new ProtoFileElement(DEFAULT_LOCATION, packageName, syntax, imports.build(), publicImports.build(), types.build(), services.build(), Collections.emptyList(), options.build());
}
Aggregations