use of io.confluent.kafka.schemaregistry.protobuf.diff.Context in project schema-registry by confluentinc.
the class ProtobufSchemaUtils method toNormalizedString.
protected static String toNormalizedString(ProtobufSchema schema) {
Context ctx = new Context();
ctx.collectTypeInfo(schema, true);
return toString(ctx, schema.rawSchema(), true);
}
use of io.confluent.kafka.schemaregistry.protobuf.diff.Context 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 io.confluent.kafka.schemaregistry.protobuf.diff.Context 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();
}
use of io.confluent.kafka.schemaregistry.protobuf.diff.Context in project schema-registry by confluentinc.
the class ProtobufSchemaUtils method toString.
private static String toString(Context ctx, MessageElement type, boolean normalize) {
StringBuilder sb = new StringBuilder();
sb.append("message ");
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.getOptions().isEmpty()) {
sb.append('\n');
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.getFields().isEmpty()) {
sb.append('\n');
List<FieldElement> fields = type.getFields();
if (normalize) {
fields = new ArrayList<>(fields);
fields.sort(Comparator.comparing(FieldElement::getTag));
}
for (FieldElement field : fields) {
appendIndented(sb, toString(ctx, field, normalize));
}
}
if (!type.getOneOfs().isEmpty()) {
sb.append('\n');
List<OneOfElement> oneOfs = type.getOneOfs();
if (normalize) {
oneOfs = oneOfs.stream().filter(o -> !o.getFields().isEmpty()).map(o -> {
List<FieldElement> fields = new ArrayList<>(o.getFields());
fields.sort(Comparator.comparing(FieldElement::getTag));
return new OneOfElement(o.getName(), o.getDocumentation(), fields, o.getGroups(), o.getOptions());
}).collect(Collectors.toList());
oneOfs.sort(Comparator.comparing(o -> o.getFields().get(0).getTag()));
}
for (OneOfElement oneOf : oneOfs) {
appendIndented(sb, toString(ctx, oneOf, normalize));
}
}
if (!type.getGroups().isEmpty()) {
sb.append('\n');
for (GroupElement group : type.getGroups()) {
appendIndented(sb, group.toSchema());
}
}
if (!type.getExtensions().isEmpty()) {
sb.append('\n');
for (ExtensionsElement extension : type.getExtensions()) {
appendIndented(sb, extension.toSchema());
}
}
if (!type.getNestedTypes().isEmpty()) {
sb.append('\n');
// the non-normalized schema to serialize message indexes
for (TypeElement typeElement : type.getNestedTypes()) {
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())) {
appendIndented(sb, toString(ctx, (MessageElement) typeElement, normalize));
}
}
}
for (TypeElement typeElement : type.getNestedTypes()) {
if (typeElement instanceof EnumElement) {
try (Context.NamedScope nameScope = ctx.enterName(typeElement.getName())) {
appendIndented(sb, toString(ctx, (EnumElement) typeElement, normalize));
}
}
}
}
sb.append("}\n");
return sb.toString();
}
Aggregations