use of com.squareup.wire.schema.internal.parser.EnumConstantElement in project schema-registry by confluentinc.
the class EnumSchemaDiff method compare.
static void compare(final Context ctx, final EnumElement original, final EnumElement update) {
Map<Integer, EnumConstantElement> originalByTag = new HashMap<>();
for (EnumConstantElement enumer : original.getConstants()) {
originalByTag.put(enumer.getTag(), enumer);
}
Map<Integer, EnumConstantElement> updateByTag = new HashMap<>();
for (EnumConstantElement enumer : update.getConstants()) {
updateByTag.put(enumer.getTag(), enumer);
}
Set<Integer> allTags = new HashSet<>(originalByTag.keySet());
allTags.addAll(updateByTag.keySet());
for (Integer tag : allTags) {
try (Context.PathScope pathScope = ctx.enterPath(tag.toString())) {
EnumConstantElement originalEnumConstant = originalByTag.get(tag);
EnumConstantElement updateEnumConstant = updateByTag.get(tag);
if (updateEnumConstant == null) {
ctx.addDifference(ENUM_CONST_REMOVED);
} else if (originalEnumConstant == null) {
ctx.addDifference(ENUM_CONST_ADDED);
} else if (!originalEnumConstant.getName().equals(updateEnumConstant.getName())) {
ctx.addDifference(ENUM_CONST_CHANGED);
}
}
}
}
use of com.squareup.wire.schema.internal.parser.EnumConstantElement 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();
}
use of com.squareup.wire.schema.internal.parser.EnumConstantElement in project apicurio-registry by Apicurio.
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.EnumConstantElement in project apicurio-registry by Apicurio.
the class ProtobufCompatibilityCheckerLibrary method checkNoChangingFieldIDs.
/**
* Determine if any field ID number has been changed.
*
* @return differences list
*/
public List<ProtobufDifference> checkNoChangingFieldIDs() {
List<ProtobufDifference> issues = new ArrayList<>();
Map<String, Map<String, FieldElement>> before = fileBefore.getFieldMap();
Map<String, Map<String, FieldElement>> after = fileAfter.getFieldMap();
for (Map.Entry<String, Map<String, FieldElement>> entry : before.entrySet()) {
Map<String, FieldElement> afterMap = after.get(entry.getKey());
if (afterMap != null) {
for (Map.Entry<String, FieldElement> beforeKV : entry.getValue().entrySet()) {
FieldElement afterFE = afterMap.get(beforeKV.getKey());
if (afterFE != null && beforeKV.getValue().getTag() != afterFE.getTag()) {
issues.add(ProtobufDifference.from(String.format("Conflict, field id changed, message %s , before: %s , after %s", entry.getKey(), beforeKV.getValue().getTag(), afterFE.getTag())));
}
}
}
}
Map<String, Map<String, EnumConstantElement>> beforeEnum = fileBefore.getEnumFieldMap();
Map<String, Map<String, EnumConstantElement>> afterEnum = fileAfter.getEnumFieldMap();
for (Map.Entry<String, Map<String, EnumConstantElement>> entry : beforeEnum.entrySet()) {
Map<String, EnumConstantElement> afterMap = afterEnum.get(entry.getKey());
if (afterMap != null) {
for (Map.Entry<String, EnumConstantElement> beforeKV : entry.getValue().entrySet()) {
EnumConstantElement afterECE = afterMap.get(beforeKV.getKey());
if (afterECE != null && beforeKV.getValue().getTag() != afterECE.getTag()) {
issues.add(ProtobufDifference.from(String.format("Conflict, field id changed, message %s , before: %s , after %s", entry.getKey(), beforeKV.getValue().getTag(), afterECE.getTag())));
}
}
}
}
return issues;
}
use of com.squareup.wire.schema.internal.parser.EnumConstantElement in project schema-registry by confluentinc.
the class ProtobufSchemaUtils method toString.
private static String toString(Context ctx, EnumElement type, boolean normalize) {
StringBuilder sb = new StringBuilder();
sb.append("enum ");
sb.append(type.getName());
sb.append(" {");
if (!type.getReserveds().isEmpty()) {
sb.append('\n');
List<ReservedElement> reserveds = type.getReserveds();
if (normalize) {
reserveds = reserveds.stream().flatMap(r -> r.getValues().stream().map(o -> new ReservedElement(r.getLocation(), r.getDocumentation(), Collections.singletonList(o)))).collect(Collectors.toList());
Comparator<Object> cmp = Comparator.comparing(r -> {
Object o = ((ReservedElement) r).getValues().get(0);
if (o instanceof IntRange) {
return ((IntRange) o).getStart();
} else if (o instanceof Integer) {
return (Integer) o;
} else {
return Integer.MAX_VALUE;
}
}).thenComparing(r -> ((ReservedElement) r).getValues().get(0).toString());
reserveds.sort(cmp);
}
for (ReservedElement reserved : reserveds) {
appendIndented(sb, toString(ctx, reserved, normalize));
}
}
if (type.getReserveds().isEmpty() && (!type.getOptions().isEmpty() || !type.getConstants().isEmpty())) {
sb.append('\n');
}
if (!type.getOptions().isEmpty()) {
List<OptionElement> options = type.getOptions();
if (normalize) {
options = new ArrayList<>(options);
options.sort(Comparator.comparing(OptionElement::getName));
}
for (OptionElement option : options) {
appendIndented(sb, toOptionString(option, normalize));
}
}
if (!type.getConstants().isEmpty()) {
List<EnumConstantElement> constants = type.getConstants();
if (normalize) {
constants = new ArrayList<>(constants);
constants.sort(Comparator.comparing(EnumConstantElement::getTag).thenComparing(EnumConstantElement::getName));
}
for (EnumConstantElement constant : constants) {
appendIndented(sb, toString(ctx, constant, normalize));
}
}
sb.append("}\n");
return sb.toString();
}
Aggregations