Search in sources :

Example 1 with EnumValueDescriptor

use of org.infinispan.protostream.descriptors.EnumValueDescriptor in project kogito-runtimes by kiegroup.

the class ProtostreamProtobufAdapterTypeProvider method buildEnum.

private EnumDescriptorProto buildEnum(EnumDescriptor enumDescriptor) {
    EnumDescriptorProto.Builder enumBuilder = EnumDescriptorProto.newBuilder();
    enumBuilder.setName(enumDescriptor.getName());
    for (EnumValueDescriptor enumValueDescriptor : enumDescriptor.getValues()) {
        enumBuilder.addValue(buildEnumValue(enumValueDescriptor));
    }
    return enumBuilder.build();
}
Also used : EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor)

Example 2 with EnumValueDescriptor

use of org.infinispan.protostream.descriptors.EnumValueDescriptor in project protostream by infinispan.

the class SerializationContextImpl method unregisterFileDescriptorTypes.

@GuardedBy("descriptorLock")
private void unregisterFileDescriptorTypes(FileDescriptor fileDescriptor) {
    if (fileDescriptor.isResolved()) {
        for (GenericDescriptor d : fileDescriptor.getTypes().values()) {
            Integer typeId = d.getTypeId();
            if (typeId != null) {
                typeIds.remove(typeId);
            }
            if (d instanceof EnumDescriptor) {
                for (EnumValueDescriptor ev : ((EnumDescriptor) d).getValues()) {
                    enumValueDescriptors.remove(ev.getScopedName());
                }
            }
        }
        genericDescriptors.keySet().removeAll(fileDescriptor.getTypes().keySet());
        fileDescriptor.markUnresolved();
    }
    for (FileDescriptor fd : fileDescriptor.getDependants().values()) {
        unregisterFileDescriptorTypes(fd);
    }
}
Also used : GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) GuardedBy(net.jcip.annotations.GuardedBy)

Example 3 with EnumValueDescriptor

use of org.infinispan.protostream.descriptors.EnumValueDescriptor in project protostream by infinispan.

the class JsonUtils method writeEnumField.

private static void writeEnumField(JsonParser parser, TagWriter writer, FieldDescriptor fd) throws IOException {
    String value = parser.getText();
    EnumDescriptor enumDescriptor = fd.getEnumType();
    EnumValueDescriptor valueDescriptor = enumDescriptor.findValueByName(value);
    if (valueDescriptor == null) {
        throw new IllegalStateException("Invalid enum value '" + value + "'");
    }
    int choice = valueDescriptor.getNumber();
    writer.writeEnum(fd.getNumber(), choice);
}
Also used : EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor)

Example 4 with EnumValueDescriptor

use of org.infinispan.protostream.descriptors.EnumValueDescriptor in project protostream by infinispan.

the class JsonUtils method toCanonicalJSON.

private static void toCanonicalJSON(ImmutableSerializationContext ctx, byte[] bytes, StringBuilder jsonOut, int initNestingLevel) throws IOException {
    if (bytes.length == 0) {
        // only null values get to be encoded to an empty byte array
        jsonOut.append("null");
        return;
    }
    Descriptor wrapperDescriptor = ctx.getMessageDescriptor(WrappedMessage.PROTOBUF_TYPE_NAME);
    boolean prettyPrint = initNestingLevel >= 0;
    TagHandler messageHandler = new TagHandler() {

        private JsonNestingLevel nestingLevel;

        /**
         * Have we written the "_type" field?
         */
        private boolean missingType = true;

        private void indent() {
            jsonOut.append('\n');
            for (int k = initNestingLevel + nestingLevel.indent; k > 0; k--) {
                jsonOut.append("   ");
            }
        }

        @Override
        public void onStart(GenericDescriptor descriptor) {
            nestingLevel = new JsonNestingLevel(null);
            if (prettyPrint) {
                indent();
                nestingLevel.indent++;
            }
            jsonOut.append('{');
            writeType(descriptor);
        }

        private void writeType(AnnotatedDescriptor descriptor) {
            if (descriptor != null && nestingLevel.previous == null && nestingLevel.isFirstField) {
                missingType = false;
                nestingLevel.isFirstField = false;
                if (prettyPrint) {
                    indent();
                }
                jsonOut.append('\"').append("_type").append('\"').append(':');
                if (prettyPrint) {
                    jsonOut.append(' ');
                }
                String type;
                if (descriptor instanceof FieldDescriptor) {
                    type = ((FieldDescriptor) descriptor).getTypeName();
                } else {
                    type = descriptor.getFullName();
                }
                jsonOut.append('\"').append(type).append('\"');
            }
        }

        @Override
        public void onTag(int fieldNumber, FieldDescriptor fieldDescriptor, Object tagValue) {
            if (fieldDescriptor == null) {
                // unknown field, ignore
                return;
            }
            if (missingType) {
                writeType(fieldDescriptor);
            }
            startSlot(fieldDescriptor);
            switch(fieldDescriptor.getType()) {
                case STRING:
                    escapeJson((String) tagValue, jsonOut, true);
                    break;
                case INT64:
                case SINT64:
                case UINT64:
                case FIXED64:
                    jsonOut.append(tagValue);
                    break;
                case FLOAT:
                    Float f = (Float) tagValue;
                    if (f.isInfinite() || f.isNaN()) {
                        // Infinity and NaN need to be quoted
                        jsonOut.append('\"').append(f).append('\"');
                    } else {
                        jsonOut.append(f);
                    }
                    break;
                case DOUBLE:
                    Double d = (Double) tagValue;
                    if (d.isInfinite() || d.isNaN()) {
                        jsonOut.append('\"').append(d).append('\"');
                    } else {
                        jsonOut.append(d);
                    }
                    break;
                case ENUM:
                    EnumValueDescriptor enumValue = fieldDescriptor.getEnumType().findValueByNumber((Integer) tagValue);
                    jsonOut.append('\"').append(enumValue.getName()).append('\"');
                    break;
                case BYTES:
                    String base64encoded = Base64.getEncoder().encodeToString((byte[]) tagValue);
                    jsonOut.append('\"').append(base64encoded).append('\"');
                    break;
                default:
                    if (tagValue instanceof Date) {
                        jsonOut.append('\"').append(formatDate((Date) tagValue)).append('\"');
                    } else if (fieldNumber == WRAPPED_ENUM) {
                        jsonOut.append('\"').append(tagValue).append('\"');
                    } else {
                        jsonOut.append(tagValue);
                    }
            }
        }

        @Override
        public void onStartNested(int fieldNumber, FieldDescriptor fieldDescriptor) {
            if (fieldDescriptor == null) {
                // unknown field, ignore
                return;
            }
            startSlot(fieldDescriptor);
            nestingLevel = new JsonNestingLevel(nestingLevel);
            if (prettyPrint) {
                indent();
                nestingLevel.indent++;
            }
            jsonOut.append('{');
        }

        @Override
        public void onEndNested(int fieldNumber, FieldDescriptor fieldDescriptor) {
            if (nestingLevel.repeatedFieldDescriptor != null) {
                endArraySlot();
            }
            if (prettyPrint) {
                nestingLevel.indent--;
                indent();
            }
            jsonOut.append('}');
            nestingLevel = nestingLevel.previous;
        }

        @Override
        public void onEnd() {
            if (nestingLevel.repeatedFieldDescriptor != null) {
                endArraySlot();
            }
            if (prettyPrint) {
                nestingLevel.indent--;
                indent();
            }
            jsonOut.append('}');
            nestingLevel = null;
            if (prettyPrint) {
                jsonOut.append('\n');
            }
        }

        private void startSlot(FieldDescriptor fieldDescriptor) {
            if (nestingLevel.repeatedFieldDescriptor != null && nestingLevel.repeatedFieldDescriptor != fieldDescriptor) {
                endArraySlot();
            }
            if (nestingLevel.isFirstField) {
                nestingLevel.isFirstField = false;
            } else {
                jsonOut.append(',');
            }
            if (!fieldDescriptor.isRepeated() || nestingLevel.repeatedFieldDescriptor == null) {
                if (prettyPrint) {
                    indent();
                }
                if (fieldDescriptor.getLabel() == Label.ONE_OF) {
                    jsonOut.append('"').append(JSON_VALUE_FIELD).append("\":");
                } else {
                    jsonOut.append('"').append(fieldDescriptor.getName()).append("\":");
                }
            }
            if (prettyPrint) {
                jsonOut.append(' ');
            }
            if (fieldDescriptor.isRepeated() && nestingLevel.repeatedFieldDescriptor == null) {
                nestingLevel.repeatedFieldDescriptor = fieldDescriptor;
                jsonOut.append('[');
            }
        }

        private void endArraySlot() {
            if (prettyPrint && nestingLevel.repeatedFieldDescriptor.getType() == Type.MESSAGE) {
                indent();
            }
            nestingLevel.repeatedFieldDescriptor = null;
            jsonOut.append(']');
        }
    };
    TagHandler wrapperHandler = new TagHandler() {

        private Integer typeId;

        private String typeName;

        private byte[] wrappedMessage;

        private Integer wrappedEnum;

        private GenericDescriptor getDescriptor() {
            return typeId != null ? ctx.getDescriptorByTypeId(typeId) : ctx.getDescriptorByName(typeName);
        }

        @Override
        public void onTag(int fieldNumber, FieldDescriptor fieldDescriptor, Object tagValue) {
            if (fieldDescriptor == null) {
                // ignore unknown fields
                return;
            }
            switch(fieldNumber) {
                case WRAPPED_TYPE_ID:
                    typeId = (Integer) tagValue;
                    break;
                case WRAPPED_TYPE_NAME:
                    typeName = (String) tagValue;
                    break;
                case WRAPPED_MESSAGE:
                    wrappedMessage = (byte[]) tagValue;
                    break;
                case WRAPPED_ENUM:
                    wrappedEnum = (Integer) tagValue;
                    break;
                case WrappedMessage.WRAPPED_DOUBLE:
                case WrappedMessage.WRAPPED_FLOAT:
                case WrappedMessage.WRAPPED_INT64:
                case WrappedMessage.WRAPPED_UINT64:
                case WrappedMessage.WRAPPED_INT32:
                case WrappedMessage.WRAPPED_FIXED64:
                case WrappedMessage.WRAPPED_FIXED32:
                case WrappedMessage.WRAPPED_BOOL:
                case WrappedMessage.WRAPPED_STRING:
                case WrappedMessage.WRAPPED_BYTES:
                case WrappedMessage.WRAPPED_UINT32:
                case WrappedMessage.WRAPPED_SFIXED32:
                case WrappedMessage.WRAPPED_SFIXED64:
                case WrappedMessage.WRAPPED_SINT32:
                case WrappedMessage.WRAPPED_SINT64:
                    messageHandler.onStart(null);
                    messageHandler.onTag(fieldNumber, fieldDescriptor, tagValue);
                    messageHandler.onEnd();
                    break;
            }
        }

        @Override
        public void onEnd() {
            if (wrappedEnum != null) {
                EnumDescriptor enumDescriptor = (EnumDescriptor) getDescriptor();
                String enumConstantName = enumDescriptor.findValueByNumber(wrappedEnum).getName();
                FieldDescriptor fd = wrapperDescriptor.findFieldByNumber(WRAPPED_ENUM);
                messageHandler.onStart(enumDescriptor);
                messageHandler.onTag(WRAPPED_ENUM, fd, enumConstantName);
                messageHandler.onEnd();
            } else if (wrappedMessage != null) {
                try {
                    Descriptor messageDescriptor = (Descriptor) getDescriptor();
                    ProtobufParser.INSTANCE.parse(messageHandler, messageDescriptor, wrappedMessage);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    };
    ProtobufParser.INSTANCE.parse(wrapperHandler, wrapperDescriptor, bytes);
}
Also used : AnnotatedDescriptor(org.infinispan.protostream.descriptors.AnnotatedDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) IOException(java.io.IOException) Date(java.util.Date) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) TagHandler(org.infinispan.protostream.TagHandler) AnnotatedDescriptor(org.infinispan.protostream.descriptors.AnnotatedDescriptor) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor)

Example 5 with EnumValueDescriptor

use of org.infinispan.protostream.descriptors.EnumValueDescriptor in project protostream by infinispan.

the class JsonUtils method processEnum.

private static void processEnum(JsonParser parser, TagWriter writer, EnumDescriptor enumDescriptor) throws IOException {
    while (true) {
        JsonToken token = parser.nextToken();
        if (token == null) {
            break;
        }
        switch(token) {
            case END_OBJECT:
                return;
            case FIELD_NAME:
                String fieldName = parser.getCurrentName();
                expectField(JSON_VALUE_FIELD, fieldName);
                break;
            case VALUE_STRING:
                {
                    String enumValueName = parser.getText();
                    EnumValueDescriptor enumValueDescriptor = enumDescriptor.findValueByName(enumValueName);
                    if (enumValueDescriptor == null) {
                        throw new IllegalStateException("Invalid enum value : '" + enumValueName + "'");
                    }
                    Integer topLevelTypeId = enumDescriptor.getTypeId();
                    if (topLevelTypeId == null) {
                        writer.writeString(WRAPPED_TYPE_NAME, enumDescriptor.getFullName());
                    } else {
                        writer.writeUInt32(WRAPPED_TYPE_ID, topLevelTypeId);
                    }
                    writer.writeEnum(WRAPPED_ENUM, enumValueDescriptor.getNumber());
                    break;
                }
            case VALUE_NUMBER_INT:
                {
                    int enumValueNumber = parser.getIntValue();
                    EnumValueDescriptor enumValueDescriptor = enumDescriptor.findValueByNumber(enumValueNumber);
                    if (enumValueDescriptor == null) {
                        throw new IllegalStateException("Invalid enum value : " + enumValueNumber);
                    }
                    Integer topLevelTypeId = enumDescriptor.getTypeId();
                    if (topLevelTypeId == null) {
                        writer.writeString(WRAPPED_TYPE_NAME, enumDescriptor.getFullName());
                    } else {
                        writer.writeUInt32(WRAPPED_TYPE_ID, topLevelTypeId);
                    }
                    writer.writeEnum(WRAPPED_ENUM, enumValueDescriptor.getNumber());
                    break;
                }
            case VALUE_NULL:
                throw new IllegalStateException("Invalid enum value 'null'");
            case VALUE_TRUE:
            case VALUE_FALSE:
            case VALUE_NUMBER_FLOAT:
                throw new IllegalStateException("Invalid enum value '" + parser.getText() + "'");
            default:
                throw new IllegalStateException("Unexpected token : " + token);
        }
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor)

Aggregations

EnumValueDescriptor (org.infinispan.protostream.descriptors.EnumValueDescriptor)6 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)4 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)2 JsonToken (com.fasterxml.jackson.core.JsonToken)1 EnumDescriptorProto (com.google.protobuf.DescriptorProtos.EnumDescriptorProto)1 IOException (java.io.IOException)1 Date (java.util.Date)1 GuardedBy (net.jcip.annotations.GuardedBy)1 EnumMarshaller (org.infinispan.protostream.EnumMarshaller)1 TagHandler (org.infinispan.protostream.TagHandler)1 AnnotatedDescriptor (org.infinispan.protostream.descriptors.AnnotatedDescriptor)1 Descriptor (org.infinispan.protostream.descriptors.Descriptor)1 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)1 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)1