Search in sources :

Example 6 with PEnumDescriptor

use of net.morimekta.providence.descriptor.PEnumDescriptor in project providence by morimekta.

the class BinaryFormatUtils method readFieldValue.

/**
 * Read a field value from stream.
 *
 * @param in        The stream to consume.
 * @param fieldInfo The field info about the content.
 * @param fieldType The type to generate content for.
 * @param strict    If the field should be read strictly.
 * @return The field value, or null if no type.
 * @throws IOException If unable to read from stream or invalid field type.
 */
public static Object readFieldValue(BigEndianBinaryReader in, FieldInfo fieldInfo, PDescriptor fieldType, boolean strict) throws IOException {
    if (fieldType != null && forType(fieldType.getType()) != fieldInfo.type) {
        throw new SerializerException("Wrong field type for id=%d: expected %s, got %s", fieldInfo.id, asString(forType(fieldType.getType())), asString(fieldInfo.getType()));
    }
    switch(fieldInfo.type) {
        case BinaryType.VOID:
            return Boolean.TRUE;
        case BinaryType.BOOL:
            return in.expectByte() != 0;
        case BinaryType.BYTE:
            return in.expectByte();
        case BinaryType.I16:
            return in.expectShort();
        case BinaryType.I32:
            int val = in.expectInt();
            if (fieldType != null && fieldType instanceof PEnumDescriptor) {
                @SuppressWarnings("unchecked") PEnumBuilder builder = ((PEnumDescriptor<?>) fieldType).builder();
                builder.setById(val);
                return builder.build();
            } else {
                return val;
            }
        case BinaryType.I64:
            return in.expectLong();
        case BinaryType.DOUBLE:
            return in.expectDouble();
        case BinaryType.STRING:
            int len = in.expectUInt32();
            byte[] data = in.expectBytes(len);
            if (fieldType != null && fieldType.getType() == PType.STRING) {
                return new String(data, StandardCharsets.UTF_8);
            } else {
                return Binary.wrap(data);
            }
        case BinaryType.STRUCT:
            {
                if (fieldType == null) {
                    consumeMessage(in);
                    return null;
                }
                return readMessage(in, (PMessageDescriptor<?, ?>) fieldType, strict);
            }
        case BinaryType.MAP:
            {
                final byte keyT = in.expectByte();
                final byte itemT = in.expectByte();
                final int size = in.expectUInt32();
                PDescriptor keyType = null;
                PDescriptor valueType = null;
                PMap.Builder<Object, Object> out;
                if (fieldType != null) {
                    @SuppressWarnings("unchecked") PMap<Object, Object> mapType = (PMap<Object, Object>) fieldType;
                    keyType = mapType.keyDescriptor();
                    valueType = mapType.itemDescriptor();
                    out = mapType.builder();
                } else {
                    out = new PMap.DefaultBuilder<>();
                }
                FieldInfo keyInfo = new FieldInfo(1, keyT);
                FieldInfo itemInfo = new FieldInfo(2, itemT);
                for (int i = 0; i < size; ++i) {
                    Object key = readFieldValue(in, keyInfo, keyType, strict);
                    Object value = readFieldValue(in, itemInfo, valueType, strict);
                    if (key != null && value != null) {
                        out.put(key, value);
                    } else if (strict) {
                        if (key == null) {
                            throw new SerializerException("Null key in map");
                        } else {
                            throw new SerializerException("Null value in map");
                        }
                    }
                }
                return out.build();
            }
        case BinaryType.SET:
            {
                final byte itemT = in.expectByte();
                final int size = in.expectUInt32();
                PDescriptor entryType = null;
                PSet.Builder<Object> out;
                if (fieldType != null) {
                    @SuppressWarnings("unchecked") PSet<Object> setType = (PSet<Object>) fieldType;
                    entryType = setType.itemDescriptor();
                    out = setType.builder();
                } else {
                    out = new PSet.DefaultBuilder<>();
                }
                FieldInfo itemInfo = new FieldInfo(0, itemT);
                for (int i = 0; i < size; ++i) {
                    Object value = readFieldValue(in, itemInfo, entryType, strict);
                    if (value != null) {
                        out.add(value);
                    } else if (strict) {
                        throw new SerializerException("Null value in set");
                    }
                }
                return out.build();
            }
        case BinaryType.LIST:
            {
                final byte itemT = in.expectByte();
                final int size = in.expectUInt32();
                PDescriptor entryType = null;
                PList.Builder<Object> out;
                if (fieldType != null) {
                    @SuppressWarnings("unchecked") PList<Object> listType = (PList<Object>) fieldType;
                    entryType = listType.itemDescriptor();
                    out = listType.builder();
                } else {
                    out = new PList.DefaultBuilder<>();
                }
                FieldInfo itemInfo = new FieldInfo(0, itemT);
                for (int i = 0; i < size; ++i) {
                    Object value = readFieldValue(in, itemInfo, entryType, strict);
                    if (value != null) {
                        out.add(value);
                    } else if (strict) {
                        throw new SerializerException("Null value in list");
                    }
                }
                return out.build();
            }
        default:
            throw new SerializerException("unknown data type: " + fieldInfo.getType());
    }
}
Also used : PList(net.morimekta.providence.descriptor.PList) PEnumBuilder(net.morimekta.providence.PEnumBuilder) PMessageBuilder(net.morimekta.providence.PMessageBuilder) PMap(net.morimekta.providence.descriptor.PMap) BinaryType.asString(net.morimekta.providence.serializer.binary.BinaryType.asString) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor) SerializerException(net.morimekta.providence.serializer.SerializerException) PDescriptor(net.morimekta.providence.descriptor.PDescriptor) PEnumBuilder(net.morimekta.providence.PEnumBuilder) PSet(net.morimekta.providence.descriptor.PSet) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor)

Example 7 with PEnumDescriptor

use of net.morimekta.providence.descriptor.PEnumDescriptor in project providence by morimekta.

the class MessageRowMapper method map.

@Override
public M map(int idx, ResultSet rs, StatementContext ctx) throws SQLException {
    PMessageBuilder<M, F> builder = descriptor.builder();
    for (int i = 1; i <= rs.getMetaData().getColumnCount(); ++i) {
        if (!tableName.isEmpty() && !tableName.equalsIgnoreCase(rs.getMetaData().getTableName(i))) {
            continue;
        }
        String name = rs.getMetaData().getColumnLabel(i).toUpperCase(Locale.US);
        F field = fieldNameMapping.get(name);
        if (field != null) {
            int columnType = rs.getMetaData().getColumnType(i);
            switch(field.getType()) {
                case BOOL:
                    {
                        if (columnType == Types.BOOLEAN || columnType == Types.BIT) {
                            boolean b = rs.getBoolean(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b);
                            }
                        } else {
                            int b = rs.getInt(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b != 0);
                            }
                        }
                        break;
                    }
                case BYTE:
                    {
                        byte b = rs.getByte(i);
                        if (!rs.wasNull()) {
                            builder.set(field, b);
                        }
                        break;
                    }
                case I16:
                    {
                        short b = rs.getShort(i);
                        if (!rs.wasNull()) {
                            builder.set(field, b);
                        }
                        break;
                    }
                case I32:
                    {
                        if (columnType == Types.TIMESTAMP) {
                            Timestamp ts = rs.getTimestamp(i);
                            if (ts != null) {
                                builder.set(field, (int) (ts.getTime() / 1000L));
                            }
                        } else {
                            int b = rs.getInt(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b);
                            }
                        }
                        break;
                    }
                case I64:
                    {
                        if (columnType == Types.TIMESTAMP) {
                            Timestamp ts = rs.getTimestamp(i);
                            if (ts != null) {
                                builder.set(field, ts.getTime());
                            }
                        } else {
                            long b = rs.getLong(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b);
                            }
                        }
                        break;
                    }
                case DOUBLE:
                    {
                        double b = rs.getDouble(i);
                        if (!rs.wasNull()) {
                            builder.set(field, b);
                        }
                        break;
                    }
                case STRING:
                    {
                        builder.set(field, rs.getString(i));
                        break;
                    }
                case BINARY:
                    {
                        switch(columnType) {
                            case Types.BINARY:
                            case Types.VARBINARY:
                                byte[] ts = rs.getBytes(i);
                                if (ts != null) {
                                    builder.set(field, Binary.copy(ts));
                                }
                                break;
                            case Types.BLOB:
                                Blob blob = rs.getBlob(i);
                                if (blob != null) {
                                    try {
                                        builder.set(field, Binary.read(blob.getBinaryStream(), (int) blob.length()));
                                    } catch (IOException e) {
                                        throw new UncheckedIOException(e.getMessage(), e);
                                    }
                                }
                                break;
                            case Types.CHAR:
                            case Types.VARCHAR:
                            case Types.NCHAR:
                            case Types.NVARCHAR:
                                {
                                    String tmp = rs.getString(i);
                                    if (tmp != null) {
                                        builder.set(field, Binary.fromBase64(tmp));
                                    }
                                    break;
                                }
                            case Types.NULL:
                                break;
                            default:
                                throw new SQLDataException("Unknown column type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName());
                        }
                        break;
                    }
                case ENUM:
                    {
                        int val = rs.getInt(i);
                        if (!rs.wasNull()) {
                            PEnumDescriptor ed = (PEnumDescriptor) field.getDescriptor();
                            builder.set(field, ed.findById(val));
                        }
                        break;
                    }
                case MESSAGE:
                    {
                        try {
                            PMessageDescriptor<?, ?> md = (PMessageDescriptor) field.getDescriptor();
                            switch(columnType) {
                                case Types.BINARY:
                                case Types.VARBINARY:
                                    byte[] data = rs.getBytes(i);
                                    if (data != null) {
                                        ByteArrayInputStream in = new ByteArrayInputStream(data);
                                        builder.set(field, BINARY.deserialize(in, md));
                                    }
                                    break;
                                case Types.BLOB:
                                    {
                                        Blob blob = rs.getBlob(i);
                                        if (blob != null) {
                                            builder.set(field, BINARY.deserialize(blob.getBinaryStream(), md));
                                        }
                                        break;
                                    }
                                case Types.CHAR:
                                case Types.VARCHAR:
                                case Types.NCHAR:
                                case Types.NVARCHAR:
                                    {
                                        String tmp = rs.getString(i);
                                        if (tmp != null) {
                                            StringReader reader = new StringReader(tmp);
                                            builder.set(field, JSON.deserialize(reader, md));
                                        }
                                        break;
                                    }
                                case Types.CLOB:
                                    {
                                        Clob clob = rs.getClob(i);
                                        if (clob != null) {
                                            builder.set(field, JSON.deserialize(clob.getCharacterStream(), md));
                                        }
                                        break;
                                    }
                                case Types.NULL:
                                    break;
                                default:
                                    throw new SQLDataException("Unknown column type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName());
                            }
                        } catch (IOException e) {
                            throw new UncheckedIOException(e.getMessage(), e);
                        }
                        break;
                    }
                case LIST:
                case SET:
                case MAP:
                    {
                    // ... woot?
                    }
                case VOID:
                default:
                    {
                        throw new SQLDataException("Unhandled column of type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName());
                    }
            }
        }
    }
    return builder.build();
}
Also used : Blob(java.sql.Blob) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor) Timestamp(java.sql.Timestamp) SQLDataException(java.sql.SQLDataException) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) Clob(java.sql.Clob)

Example 8 with PEnumDescriptor

use of net.morimekta.providence.descriptor.PEnumDescriptor in project providence by morimekta.

the class ProvidenceConfigParser method parseDefinitionValue.

@SuppressWarnings("unchecked")
Object parseDefinitionValue(ProvidenceConfigContext context, Tokenizer tokenizer) throws IOException {
    Token token = tokenizer.expect("Start of def value");
    if (token.isReal()) {
        return Double.parseDouble(token.asString());
    } else if (token.isInteger()) {
        return Long.parseLong(token.asString());
    } else if (token.isStringLiteral()) {
        return token.decodeLiteral(strict);
    } else if (TRUE.equalsIgnoreCase(token.asString())) {
        return Boolean.TRUE;
    } else if (FALSE.equalsIgnoreCase(token.asString())) {
        return Boolean.FALSE;
    } else if (Token.B64.equals(token.asString())) {
        tokenizer.expectSymbol("binary data enclosing start", Token.kParamsStart);
        return Binary.fromBase64(tokenizer.readBinary(Token.kParamsEnd));
    } else if (Token.HEX.equals(token.asString())) {
        tokenizer.expectSymbol("binary data enclosing start", Token.kParamsStart);
        return Binary.fromHexString(tokenizer.readBinary(Token.kParamsEnd));
    } else if (token.isDoubleQualifiedIdentifier()) {
        // this may be an enum reference, must be
        // - package.EnumType.IDENTIFIER
        String id = token.asString();
        int l = id.lastIndexOf(Token.kIdentifierSep);
        try {
            PEnumDescriptor ed = registry.getEnumType(id.substring(0, l));
            PEnumValue val = ed.findByName(id.substring(l + 1));
            if (val == null && strict) {
                throw new TokenizerException(token, "Unknown %s value: %s", id.substring(0, l), id.substring(l + 1)).setLine(tokenizer.getLine());
            }
            // Note that unknown enum value results in null. Therefore we don't catch null values here.
            return val;
        } catch (IllegalArgumentException e) {
            // No such declared type.
            if (strict) {
                throw new TokenizerException(token, "Unknown enum identifier: %s", id.substring(0, l)).setLine(tokenizer.getLine());
            }
            consumeValue(context, tokenizer, token);
        } catch (ClassCastException e) {
            // Not an enum.
            throw new TokenizerException(token, "Identifier " + id + " does not reference an enum, from " + token.asString()).setLine(tokenizer.getLine());
        }
    } else if (token.isQualifiedIdentifier()) {
        // Message type.
        PMessageDescriptor descriptor;
        try {
            descriptor = registry.getMessageType(token.asString());
        } catch (IllegalArgumentException e) {
            // - strict mode: all types must be known.
            if (strict) {
                throw new TokenizerException(token, "Unknown declared type: %s", token.asString()).setLine(tokenizer.getLine());
            }
            consumeValue(context, tokenizer, token);
            return null;
        }
        PMessageBuilder builder = descriptor.builder();
        if (tokenizer.expectSymbol("message start or inherits", '{', ':') == ':') {
            token = tokenizer.expect("inherits reference");
            PMessage inheritsFrom = resolve(context, token, tokenizer, descriptor);
            if (inheritsFrom == null) {
                throw new TokenizerException(token, "Inheriting from null reference: %s", token.asString()).setLine(tokenizer.getLine());
            }
            builder.merge(inheritsFrom);
            tokenizer.expectSymbol("message start", '{');
        }
        return parseMessage(tokenizer, context, builder);
    } else {
        throw new TokenizerException(token, "Invalid define value " + token.asString()).setLine(tokenizer.getLine());
    }
    return null;
}
Also used : PMessageBuilder(net.morimekta.providence.PMessageBuilder) PMessage(net.morimekta.providence.PMessage) PEnumValue(net.morimekta.providence.PEnumValue) Token(net.morimekta.providence.serializer.pretty.Token) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor)

Example 9 with PEnumDescriptor

use of net.morimekta.providence.descriptor.PEnumDescriptor in project providence by morimekta.

the class ConstParser method findEnumValue.

private int findEnumValue(String identifier, Token token, ThriftTokenizer tokenizer, String expectedType) throws IOException {
    String[] parts = identifier.split("[.]");
    String typeName;
    String valueName;
    if (parts.length == 3) {
        typeName = parts[0] + "." + parts[1];
        valueName = parts[2];
    } else if (parts.length == 2) {
        typeName = parts[0];
        valueName = parts[1];
    } else {
        throw tokenizer.failure(token, identifier + " is not a valid " + expectedType + " value.");
    }
    try {
        @SuppressWarnings("unchecked") PDeclaredDescriptor descriptor = registry.getDeclaredType(typeName, programContext);
        if (descriptor instanceof PEnumDescriptor) {
            PEnumDescriptor desc = (PEnumDescriptor) descriptor;
            PEnumValue value = desc.findByName(valueName);
            if (value != null) {
                return value.asInteger();
            }
        }
        throw tokenizer.failure(token, typeName + " is not an enum.");
    } catch (IllegalArgumentException e) {
        throw tokenizer.failure(token, "No type named " + typeName + ".");
    }
}
Also used : PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) PEnumValue(net.morimekta.providence.PEnumValue) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor)

Aggregations

PEnumDescriptor (net.morimekta.providence.descriptor.PEnumDescriptor)9 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)6 PList (net.morimekta.providence.descriptor.PList)4 PMap (net.morimekta.providence.descriptor.PMap)4 PSet (net.morimekta.providence.descriptor.PSet)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 PEnumValue (net.morimekta.providence.PEnumValue)3 PMessageBuilder (net.morimekta.providence.PMessageBuilder)3 PDescriptor (net.morimekta.providence.descriptor.PDescriptor)3 StringReader (java.io.StringReader)2 UncheckedIOException (java.io.UncheckedIOException)2 Blob (java.sql.Blob)2 Clob (java.sql.Clob)2 Timestamp (java.sql.Timestamp)2 PEnumBuilder (net.morimekta.providence.PEnumBuilder)2 SerializerException (net.morimekta.providence.serializer.SerializerException)2 TokenizerException (net.morimekta.providence.serializer.pretty.TokenizerException)2 ByteBuffer (java.nio.ByteBuffer)1 SQLDataException (java.sql.SQLDataException)1