use of com.squareup.wire.schema.internal.parser.EnumElement in project schema-registry by confluentinc.
the class ProtobufSchema method toDynamicEnum.
private static EnumDefinition toDynamicEnum(EnumElement enumElem) {
Map<String, OptionElement> enumOptions = mergeOptions(enumElem.getOptions());
Boolean allowAlias = findOption(ALLOW_ALIAS, enumOptions).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
Boolean isDeprecated = findOption(DEPRECATED, enumOptions).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
EnumDefinition.Builder enumer = EnumDefinition.newBuilder(enumElem.getName(), allowAlias, isDeprecated);
for (ReservedElement reserved : enumElem.getReserveds()) {
for (Object elem : reserved.getValues()) {
if (elem instanceof String) {
enumer.addReservedName((String) elem);
} else if (elem instanceof Integer) {
int tag = (Integer) elem;
enumer.addReservedRange(tag, tag + 1);
} else if (elem instanceof IntRange) {
IntRange range = (IntRange) elem;
enumer.addReservedRange(range.getStart(), range.getEndInclusive() + 1);
} else {
throw new IllegalStateException("Unsupported reserved type: " + elem.getClass().getName());
}
}
}
for (EnumConstantElement constant : enumElem.getConstants()) {
Map<String, OptionElement> constantOptions = mergeOptions(constant.getOptions());
Boolean isConstDeprecated = findOption(DEPRECATED, constantOptions).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
Optional<OptionElement> meta = findOption(CONFLUENT_ENUM_VALUE_META, constantOptions);
String doc = findDoc(meta);
Map<String, String> params = findParams(meta);
enumer.addValue(constant.getName(), constant.getTag(), doc, params, isConstDeprecated);
}
Optional<OptionElement> meta = findOption(CONFLUENT_ENUM_META, enumOptions);
String doc = findDoc(meta);
Map<String, String> params = findParams(meta);
enumer.setMeta(doc, params);
return enumer.build();
}
use of com.squareup.wire.schema.internal.parser.EnumElement in project schema-registry by confluentinc.
the class ProtobufSchemaUtils method toString.
private static String toString(Context ctx, ProtoFileElement protoFile, boolean normalize) {
StringBuilder sb = new StringBuilder();
if (protoFile.getSyntax() != null) {
sb.append("syntax = \"");
sb.append(protoFile.getSyntax());
sb.append("\";\n");
}
if (protoFile.getPackageName() != null) {
sb.append("package ");
sb.append(protoFile.getPackageName());
sb.append(";\n");
}
if (!protoFile.getImports().isEmpty() || !protoFile.getPublicImports().isEmpty()) {
sb.append('\n');
List<String> imports = protoFile.getImports();
if (normalize) {
imports = imports.stream().sorted().distinct().collect(Collectors.toList());
}
for (String file : imports) {
sb.append("import \"");
sb.append(file);
sb.append("\";\n");
}
List<String> publicImports = protoFile.getPublicImports();
if (normalize) {
publicImports = publicImports.stream().sorted().distinct().collect(Collectors.toList());
}
for (String file : publicImports) {
sb.append("import public \"");
sb.append(file);
sb.append("\";\n");
}
}
if (!protoFile.getOptions().isEmpty()) {
sb.append('\n');
List<OptionElement> options = protoFile.getOptions();
if (normalize) {
options = new ArrayList<>(options);
options.sort(Comparator.comparing(OptionElement::getName));
}
for (OptionElement option : options) {
sb.append(toOptionString(option, normalize));
}
}
if (!protoFile.getTypes().isEmpty()) {
sb.append('\n');
// the non-normalized schema to serialize message indexes
for (TypeElement typeElement : protoFile.getTypes()) {
if (typeElement instanceof MessageElement) {
if (normalize) {
TypeElementInfo typeInfo = ctx.getType(typeElement.getName(), true);
if (typeInfo != null && typeInfo.isMap()) {
// don't emit synthetic map message
continue;
}
}
try (Context.NamedScope nameScope = ctx.enterName(typeElement.getName())) {
sb.append(toString(ctx, (MessageElement) typeElement, normalize));
}
}
}
for (TypeElement typeElement : protoFile.getTypes()) {
if (typeElement instanceof EnumElement) {
try (Context.NamedScope nameScope = ctx.enterName(typeElement.getName())) {
sb.append(toString(ctx, (EnumElement) typeElement, normalize));
}
}
}
}
if (!protoFile.getExtendDeclarations().isEmpty()) {
sb.append('\n');
for (ExtendElement extendDeclaration : protoFile.getExtendDeclarations()) {
sb.append(extendDeclaration.toSchema());
}
}
if (!protoFile.getServices().isEmpty()) {
sb.append('\n');
// we don't sort message/enum elements
for (ServiceElement service : protoFile.getServices()) {
sb.append(toString(ctx, service, normalize));
}
}
return sb.toString();
}
use of com.squareup.wire.schema.internal.parser.EnumElement in project schema-registry by confluentinc.
the class SchemaDiff method compareMessageElements.
private static void compareMessageElements(List<TypeElement> types, Map<String, MessageElement> messages, Map<String, Integer> messageIndexes, Map<String, EnumElement> enums) {
int index = 0;
for (TypeElement typeElement : types) {
if (typeElement instanceof MessageElement) {
MessageElement messageElement = (MessageElement) typeElement;
messages.put(messageElement.getName(), messageElement);
messageIndexes.put(messageElement.getName(), index++);
} else if (typeElement instanceof EnumElement) {
EnumElement enumElement = (EnumElement) typeElement;
enums.put(enumElement.getName(), enumElement);
}
}
}
use of com.squareup.wire.schema.internal.parser.EnumElement in project schema-registry by confluentinc.
the class SchemaDiff method compareTypeElements.
public static void compareTypeElements(final Context ctx, final List<TypeElement> original, final List<TypeElement> update) {
Map<String, MessageElement> originalMessages = new HashMap<>();
Map<String, MessageElement> updateMessages = new HashMap<>();
Map<String, Integer> originalMessageIndexes = new HashMap<>();
Map<String, Integer> updateMessageIndexes = new HashMap<>();
Map<String, EnumElement> originalEnums = new HashMap<>();
Map<String, EnumElement> updateEnums = new HashMap<>();
compareMessageElements(original, originalMessages, originalMessageIndexes, originalEnums);
compareMessageElements(update, updateMessages, updateMessageIndexes, updateEnums);
Set<String> allMessageNames = new HashSet<>(originalMessages.keySet());
allMessageNames.addAll(updateMessages.keySet());
Set<String> allEnumNames = new HashSet<>(originalEnums.keySet());
allEnumNames.addAll(updateEnums.keySet());
for (String name : allMessageNames) {
try (Context.NamedScope nameScope = ctx.enterName(name)) {
MessageElement originalMessage = originalMessages.get(name);
MessageElement updateMessage = updateMessages.get(name);
if (updateMessage == null) {
TypeElementInfo originalType = ctx.getType(name, true);
if (originalType != null && !originalType.isMap()) {
ctx.addDifference(MESSAGE_REMOVED);
}
} else if (originalMessage == null) {
TypeElementInfo updateType = ctx.getType(name, false);
if (updateType != null && !updateType.isMap()) {
ctx.addDifference(MESSAGE_ADDED);
}
} else {
MessageSchemaDiff.compare(ctx, originalMessage, updateMessage);
Integer originalMessageIndex = originalMessageIndexes.get(name);
Integer updateMessageIndex = updateMessageIndexes.get(name);
if (originalMessageIndex == null || !originalMessageIndex.equals(updateMessageIndex)) {
// Moving or reordering a message is compatible since serialized message indexes
// are w.r.t. the schema of the corresponding ID
ctx.addDifference(MESSAGE_MOVED);
}
}
}
}
for (String name : allEnumNames) {
try (Context.NamedScope nameScope = ctx.enterName(name)) {
EnumElement originalEnum = originalEnums.get(name);
EnumElement updateEnum = updateEnums.get(name);
if (updateEnum == null) {
ctx.addDifference(ENUM_REMOVED);
} else if (originalEnum == null) {
ctx.addDifference(ENUM_ADDED);
} else {
EnumSchemaDiff.compare(ctx, originalEnum, updateEnum);
}
}
}
}
use of com.squareup.wire.schema.internal.parser.EnumElement in project apicurio-registry by Apicurio.
the class FileDescriptorUtils method toDynamicEnum.
private static EnumDefinition toDynamicEnum(EnumElement enumElem) {
Boolean allowAlias = findOption("allow_alias", enumElem.getOptions()).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
EnumDefinition.Builder enumer = EnumDefinition.newBuilder(enumElem.getName(), allowAlias);
for (EnumConstantElement constant : enumElem.getConstants()) {
enumer.addValue(constant.getName(), constant.getTag());
}
return enumer.build();
}
Aggregations