use of net.morimekta.providence.PUnion in project providence by morimekta.
the class EqualToMessage method collectMismatches.
private static <T extends PMessage<T, F>, F extends PField> void collectMismatches(String xPath, T expected, T actual, ArrayList<String> mismatches) {
// mismatch / test failure, it should be fine.
if (expected.descriptor().getVariant() == PMessageVariant.UNION) {
PUnion<?, ?> eu = (PUnion) expected;
PUnion<?, ?> ac = (PUnion) actual;
if (!eu.unionField().equals(ac.unionField())) {
mismatches.add(String.format(Locale.US, "%s to have %s, but had %s", xPath, eu.unionField().getName(), ac.unionField().getName()));
}
}
for (PField field : expected.descriptor().getFields()) {
int key = field.getId();
String fieldXPath = xPath.isEmpty() ? field.getName() : xPath + "." + field.getName();
if (expected.has(key) != actual.has(key)) {
if (!expected.has(key)) {
mismatches.add(String.format(Locale.US, "%s to be missing, but was %s", fieldXPath, toString(actual.get(field.getId()))));
} else if (!actual.has(key)) {
mismatches.add(String.format(Locale.US, "%s to be %s, but was missing", fieldXPath, toString(expected.get(field.getId()))));
}
} else if (!Objects.equals(expected.get(key), actual.get(key))) {
switch(field.getType()) {
case MESSAGE:
{
collectMismatches(fieldXPath, expected.get(key), actual.get(key), mismatches);
break;
}
case LIST:
{
collectListMismatches(fieldXPath, expected.get(key), actual.get(key), mismatches);
break;
}
case SET:
{
collectSetMismatches(fieldXPath, expected.get(key), actual.get(key), mismatches);
break;
}
case MAP:
{
collectMapMismatches(fieldXPath, expected.get(key), actual.get(key), mismatches);
break;
}
default:
{
mismatches.add(String.format(Locale.US, "%s was %s, expected %s", fieldXPath, toString(actual.get(field.getId())), toString(expected.get(field.getId()))));
break;
}
}
}
}
}
use of net.morimekta.providence.PUnion in project providence by morimekta.
the class LogFormatter method appendMessage.
private void appendMessage(IndentedPrintWriter writer, PMessage<?, ?> message) {
PMessageDescriptor<?, ?> type = message.descriptor();
writer.append(Token.kMessageStart).begin();
if (message instanceof PUnion) {
if (((PUnion) message).unionFieldIsSet()) {
PField field = ((PUnion) message).unionField();
Object o = message.get(field.getId());
writer.appendln().append(field.getName()).append(space).append(Token.kFieldValueSep).append(space);
appendFieldValue(writer, field, o);
}
} else {
boolean first = true;
for (PField field : type.getFields()) {
if (message.has(field.getId())) {
if (first) {
first = false;
} else {
writer.append(entrySep);
}
Object o = message.get(field.getId());
writer.appendln().append(field.getName()).append(space).append(Token.kFieldValueSep).append(space);
appendFieldValue(writer, field, o);
}
}
}
writer.end().appendln(Token.kMessageEnd);
}
use of net.morimekta.providence.PUnion in project providence by morimekta.
the class TTupleProtocolSerializer method writeMessage.
private void writeMessage(PMessage<?, ?> message, TTupleProtocol protocol) throws TException, SerializerException {
PMessageDescriptor<?, ?> descriptor = message.descriptor();
if (descriptor.getVariant() == PMessageVariant.UNION) {
if (((PUnion<?, ?>) message).unionFieldIsSet()) {
PField fld = ((PUnion<?, ?>) message).unionField();
protocol.writeI16((short) fld.getId());
writeTypedValue(message.get(fld.getId()), fld.getDescriptor(), protocol);
} else {
throw new SerializerException("Unable to write " + descriptor.getQualifiedName() + " without set union field.");
}
} else {
PField[] fields = descriptor.getFields();
Arrays.sort(fields, Comparator.comparingInt(PField::getId));
int numOptionals = countOptionals(fields);
BitSet optionals = new BitSet();
if (numOptionals > 0) {
int optionalPos = 0;
for (PField fld : fields) {
if (fld.getRequirement() != PRequirement.REQUIRED) {
if (message.has(fld.getId())) {
optionals.set(optionalPos);
}
++optionalPos;
}
}
}
boolean shouldWriteOptionals = true;
int optionalPos = 0;
for (PField fld : fields) {
if (fld.getRequirement() == PRequirement.REQUIRED) {
writeTypedValue(message.get(fld.getId()), fld.getDescriptor(), protocol);
} else {
// non-required field.
if (shouldWriteOptionals) {
protocol.writeBitSet(optionals, numOptionals);
shouldWriteOptionals = false;
}
if (optionals.get(optionalPos)) {
writeTypedValue(message.get(fld.getId()), fld.getDescriptor(), protocol);
}
++optionalPos;
}
}
}
}
use of net.morimekta.providence.PUnion in project providence by morimekta.
the class FastBinarySerializer method writeMessage.
// --- MESSAGE ---
private <Message extends PMessage<Message, Field>, Field extends PField> int writeMessage(LittleEndianBinaryWriter out, Message message) throws IOException {
int len = 0;
if (message instanceof PUnion) {
if (((PUnion) message).unionFieldIsSet()) {
PField field = ((PUnion) message).unionField();
len += writeFieldValue(out, field.getId(), field.getDescriptor(), message.get(field.getId()));
}
} else {
for (PField field : message.descriptor().getFields()) {
if (message.has(field.getId())) {
len += writeFieldValue(out, field.getId(), field.getDescriptor(), message.get(field.getId()));
}
}
}
// write STOP field.
return len + out.writeVarint(STOP);
}
use of net.morimekta.providence.PUnion in project providence by morimekta.
the class JsonSerializer method appendMessage.
private void appendMessage(JsonWriter writer, PMessage<?, ?> message) throws SerializerException {
PMessageDescriptor<?, ?> type = message.descriptor();
if (message instanceof PUnion) {
writer.object();
if (((PUnion) message).unionFieldIsSet()) {
PField field = ((PUnion) message).unionField();
Object value = message.get(field.getId());
if (IdType.ID.equals(fieldIdType)) {
writer.key(field.getId());
} else {
writer.keyUnescaped(field.getName());
}
appendTypedValue(writer, field.getDescriptor(), value);
}
writer.endObject();
} else {
if (isCompact(message)) {
writer.array();
for (PField field : type.getFields()) {
if (message.has(field.getId())) {
appendTypedValue(writer, field.getDescriptor(), message.get(field.getId()));
} else {
break;
}
}
writer.endArray();
} else {
writer.object();
for (PField field : type.getFields()) {
if (message.has(field.getId())) {
Object value = message.get(field.getId());
if (IdType.ID.equals(fieldIdType)) {
writer.key(field.getId());
} else {
writer.keyUnescaped(field.getName());
}
appendTypedValue(writer, field.getDescriptor(), value);
}
}
writer.endObject();
}
}
}
Aggregations