use of com.squareup.wire.schema.internal.parser.OneOfElement in project apicurio-registry by Apicurio.
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.OneOfElement 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.OneOfElement in project schema-registry by confluentinc.
the class MessageSchemaDiff method compare.
static void compare(final Context ctx, final MessageElement original, final MessageElement update) {
try (Context.SchemaScope schemaScope = ctx.enterSchema(original)) {
if (schemaScope != null) {
Map<Integer, FieldElement> originalByTag = new HashMap<>();
for (FieldElement field : original.getFields()) {
originalByTag.put(field.getTag(), field);
}
Map<Integer, FieldElement> updateByTag = new HashMap<>();
for (FieldElement field : update.getFields()) {
updateByTag.put(field.getTag(), field);
}
Map<String, OneOfElement> originalOneOfs = new HashMap<>();
Map<String, OneOfElement> updateOneOfs = new HashMap<>();
for (OneOfElement oneOf : original.getOneOfs()) {
originalOneOfs.put(oneOf.getName(), oneOf);
}
for (OneOfElement oneOf : update.getOneOfs()) {
updateOneOfs.put(oneOf.getName(), oneOf);
}
// at most one field in the original message
for (OneOfElement oneOf : update.getOneOfs()) {
try (Context.PathScope pathScope = ctx.enterPath(oneOf.getName())) {
int numMatchingOriginal = 0;
for (FieldElement oneOfField : oneOf.getFields()) {
// Remove the field so that a FIELD_REMOVED difference is not generated
FieldElement originalField = originalByTag.remove(oneOfField.getTag());
if (originalField != null) {
numMatchingOriginal++;
}
}
if (numMatchingOriginal > 1) {
ctx.addDifference(MULTIPLE_FIELDS_MOVED_TO_ONEOF);
}
}
}
Set<String> allOneOfs = new HashSet<>(originalOneOfs.keySet());
allOneOfs.addAll(updateOneOfs.keySet());
for (String oneOfName : allOneOfs) {
try (Context.PathScope pathScope = ctx.enterPath(oneOfName)) {
OneOfElement originalOneOf = originalOneOfs.get(oneOfName);
OneOfElement updateOneOf = updateOneOfs.get(oneOfName);
if (updateOneOf == null) {
ctx.addDifference(ONEOF_REMOVED);
} else if (originalOneOf == null) {
ctx.addDifference(ONEOF_ADDED);
} else {
OneOfDiff.compare(ctx, originalOneOf, updateOneOf);
}
}
}
Set<Integer> allTags = new HashSet<>(originalByTag.keySet());
allTags.addAll(updateByTag.keySet());
for (Integer tag : allTags) {
try (Context.PathScope pathScope = ctx.enterPath(tag.toString())) {
FieldElement originalField = originalByTag.get(tag);
FieldElement updateField = updateByTag.get(tag);
if (updateField == null) {
if (originalField.getLabel() == Label.REQUIRED) {
ctx.addDifference(REQUIRED_FIELD_REMOVED);
} else {
ctx.addDifference(FIELD_REMOVED);
}
} else if (originalField == null) {
if (updateField.getLabel() == Label.REQUIRED) {
ctx.addDifference(REQUIRED_FIELD_ADDED);
} else {
ctx.addDifference(FIELD_ADDED);
}
} else {
FieldSchemaDiff.compare(ctx, originalField, updateField);
}
}
}
}
SchemaDiff.compareTypeElements(ctx, original.getNestedTypes(), update.getNestedTypes());
}
}
use of com.squareup.wire.schema.internal.parser.OneOfElement in project apicurio-registry by Apicurio.
the class FileDescriptorUtils method toDynamicMessage.
private static MessageDefinition toDynamicMessage(MessageElement messageElem) {
MessageDefinition.Builder message = MessageDefinition.newBuilder(messageElem.getName());
for (TypeElement type : messageElem.getNestedTypes()) {
if (type instanceof MessageElement) {
message.addMessageDefinition(toDynamicMessage((MessageElement) type));
} else if (type instanceof EnumElement) {
message.addEnumDefinition(toDynamicEnum((EnumElement) type));
}
}
Set<String> added = new HashSet<>();
for (OneOfElement oneof : messageElem.getOneOfs()) {
MessageDefinition.OneofBuilder oneofBuilder = message.addOneof(oneof.getName());
for (FieldElement field : oneof.getFields()) {
String defaultVal = field.getDefaultValue();
String jsonName = findOption("json_name", field.getOptions()).map(o -> o.getValue().toString()).orElse(null);
oneofBuilder.addField(field.getType(), field.getName(), field.getTag(), defaultVal, jsonName);
added.add(field.getName());
}
}
// Process fields after messages so that any newly created map entry messages are at the end
for (FieldElement field : messageElem.getFields()) {
if (added.contains(field.getName())) {
continue;
}
Field.Label fieldLabel = field.getLabel();
String label = fieldLabel != null ? fieldLabel.toString().toLowerCase() : null;
String fieldType = field.getType();
String defaultVal = field.getDefaultValue();
String jsonName = field.getJsonName();
Boolean isPacked = findOption("packed", field.getOptions()).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
ProtoType protoType = ProtoType.get(fieldType);
ProtoType keyType = protoType.getKeyType();
ProtoType valueType = protoType.getValueType();
// Map fields are only permitted in messages
if (protoType.isMap() && keyType != null && valueType != null) {
label = "repeated";
fieldType = toMapEntry(field.getName());
MessageDefinition.Builder mapMessage = MessageDefinition.newBuilder(fieldType);
mapMessage.setMapEntry(true);
mapMessage.addField(null, keyType.getSimpleName(), KEY_FIELD, 1, null);
mapMessage.addField(null, valueType.getSimpleName(), VALUE_FIELD, 2, null);
message.addMessageDefinition(mapMessage.build());
}
message.addField(label, fieldType, field.getName(), field.getTag(), defaultVal, jsonName, isPacked);
}
for (ReservedElement reserved : messageElem.getReserveds()) {
for (Object elem : reserved.getValues()) {
if (elem instanceof String) {
message.addReservedName((String) elem);
} else if (elem instanceof Integer) {
int tag = (Integer) elem;
message.addReservedRange(tag, tag);
} else if (elem instanceof IntRange) {
IntRange range = (IntRange) elem;
message.addReservedRange(range.getStart(), range.getEndInclusive());
} else {
throw new IllegalStateException("Unsupported reserved type: " + elem.getClass().getName());
}
}
}
Boolean isMapEntry = findOption("map_entry", messageElem.getOptions()).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
if (isMapEntry != null) {
message.setMapEntry(isMapEntry);
}
return message.build();
}
use of com.squareup.wire.schema.internal.parser.OneOfElement 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();
}
}
Aggregations