Search in sources :

Example 1 with PSet

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

the class ProgramRegistryTest method testGetProvider_set.

@Test
public void testGetProvider_set() {
    PDescriptorProvider p1 = registry.getProvider("set<I>", "number", ImmutableMap.of("container", "sorted"));
    assertThat(p1.descriptor().getType(), is(PType.SET));
    PSet set = (PSet) p1.descriptor();
    assertThat(set.itemDescriptor().getQualifiedName(), is(Imaginary.kDescriptor.getQualifiedName()));
    p1 = registry.getProvider("set<set<i32>>", "number", ImmutableMap.of("container", "ordered"));
    assertThat(p1.descriptor().getType(), is(PType.SET));
    set = (PSet) p1.descriptor();
    assertThat(set.itemDescriptor().getType(), is(PType.SET));
    PSet list = (PSet) set.itemDescriptor();
    assertThat(list.itemDescriptor(), is(PPrimitive.I32));
}
Also used : PSet(net.morimekta.providence.descriptor.PSet) PDescriptorProvider(net.morimekta.providence.descriptor.PDescriptorProvider) Test(org.junit.Test)

Example 2 with PSet

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

the class ProvidenceConfigParser method parseFieldValue.

@SuppressWarnings("unchecked")
Object parseFieldValue(Token next, Tokenizer tokenizer, ProvidenceConfigContext context, PDescriptor descriptor, boolean requireEnumValue) throws IOException {
    try {
        switch(descriptor.getType()) {
            case BOOL:
                if (TRUE.equals(next.asString())) {
                    return true;
                } else if (FALSE.equals(next.asString())) {
                    return false;
                } else if (next.isReferenceIdentifier()) {
                    return resolve(context, next, tokenizer, descriptor);
                }
                break;
            case BYTE:
                if (next.isReferenceIdentifier()) {
                    return resolve(context, next, tokenizer, descriptor);
                } else if (next.isInteger()) {
                    return (byte) next.parseInteger();
                }
                break;
            case I16:
                if (next.isReferenceIdentifier()) {
                    return resolve(context, next, tokenizer, descriptor);
                } else if (next.isInteger()) {
                    return (short) next.parseInteger();
                }
                break;
            case I32:
                if (next.isReferenceIdentifier()) {
                    return resolve(context, next, tokenizer, descriptor);
                } else if (next.isInteger()) {
                    return (int) next.parseInteger();
                }
                break;
            case I64:
                if (next.isReferenceIdentifier()) {
                    return resolve(context, next, tokenizer, descriptor);
                } else if (next.isInteger()) {
                    return next.parseInteger();
                }
                break;
            case DOUBLE:
                if (next.isReferenceIdentifier()) {
                    return resolve(context, next, tokenizer, descriptor);
                } else if (next.isInteger() || next.isReal()) {
                    return next.parseDouble();
                }
                break;
            case STRING:
                if (next.isReferenceIdentifier()) {
                    return resolve(context, next, tokenizer, descriptor);
                } else if (next.isStringLiteral()) {
                    return next.decodeLiteral(strict);
                }
                break;
            case BINARY:
                if (Token.B64.equals(next.asString())) {
                    tokenizer.expectSymbol("binary data enclosing start", Token.kParamsStart);
                    return Binary.fromBase64(tokenizer.readBinary(Token.kParamsEnd));
                } else if (Token.HEX.equals(next.asString())) {
                    tokenizer.expectSymbol("binary data enclosing start", Token.kParamsStart);
                    return Binary.fromHexString(tokenizer.readBinary(Token.kParamsEnd));
                } else if (next.isReferenceIdentifier()) {
                    return resolve(context, next, tokenizer, descriptor);
                }
                break;
            case ENUM:
                {
                    PEnumDescriptor ed = (PEnumDescriptor) descriptor;
                    PEnumValue value;
                    String name = next.asString();
                    if (next.isInteger()) {
                        value = ed.findById((int) next.parseInteger());
                    } else if (next.isIdentifier()) {
                        value = ed.findByName(name);
                        if (value == null && context.containsReference(name)) {
                            value = resolve(context, next, tokenizer, ed);
                        }
                    } else if (next.isReferenceIdentifier()) {
                        value = resolve(context, next, tokenizer, descriptor);
                    } else {
                        break;
                    }
                    if (value == null && (strict || requireEnumValue)) {
                        PEnumValue option = null;
                        if (next.isIdentifier()) {
                            for (PEnumValue o : ed.getValues()) {
                                if (o.getName().equalsIgnoreCase(name)) {
                                    option = o;
                                    break;
                                }
                            }
                        }
                        if (option != null) {
                            throw new TokenizerException(next, "No such enum value '%s' for %s, did you mean '%s'?", name, ed.getQualifiedName(), option.getName()).setLine(tokenizer.getLine());
                        }
                        throw new TokenizerException(next, "No such enum value '%s' for %s.", name, ed.getQualifiedName()).setLine(tokenizer.getLine());
                    }
                    return value;
                }
            case MESSAGE:
                if (next.isReferenceIdentifier()) {
                    return resolve(context, next, tokenizer, descriptor);
                } else if (next.isSymbol(Token.kMessageStart)) {
                    return parseMessage(tokenizer, context, ((PMessageDescriptor) descriptor).builder());
                }
                break;
            case MAP:
                {
                    if (next.isReferenceIdentifier()) {
                        Map resolved;
                        try {
                            // Make sure the reference is to a map.
                            resolved = resolve(context, next, tokenizer, descriptor);
                        } catch (ClassCastException e) {
                            throw new TokenizerException(next, "Reference %s is not a map field ", next.asString()).setLine(tokenizer.getLine());
                        }
                        return resolved;
                    } else if (next.isSymbol(Token.kMessageStart)) {
                        return parseMapValue(tokenizer, context, (PMap) descriptor, new LinkedHashMap());
                    }
                    break;
                }
            case SET:
                {
                    if (next.isReferenceIdentifier()) {
                        return resolve(context, next, tokenizer, descriptor);
                    } else if (next.isSymbol(Token.kListStart)) {
                        @SuppressWarnings("unchecked") PSet<Object> ct = (PSet) descriptor;
                        Set<Object> value = new LinkedHashSet<>();
                        next = tokenizer.expect("set value or end");
                        while (!next.isSymbol(Token.kListEnd)) {
                            Object item = parseFieldValue(next, tokenizer, context, ct.itemDescriptor(), strict);
                            if (item != null) {
                                value.add(item);
                            }
                            // sets require separator, and allows separator after last.
                            if (tokenizer.expectSymbol("set separator or end", Token.kLineSep1, Token.kListEnd) == Token.kListEnd) {
                                break;
                            }
                            next = tokenizer.expect("set value or end");
                        }
                        return ct.builder().addAll(value).build();
                    }
                    break;
                }
            case LIST:
                {
                    if (next.isReferenceIdentifier()) {
                        return resolve(context, next, tokenizer, descriptor);
                    } else if (next.isSymbol(Token.kListStart)) {
                        @SuppressWarnings("unchecked") PList<Object> ct = (PList) descriptor;
                        PList.Builder<Object> builder = ct.builder();
                        next = tokenizer.expect("list value or end");
                        while (!next.isSymbol(Token.kListEnd)) {
                            Object item = parseFieldValue(next, tokenizer, context, ct.itemDescriptor(), strict);
                            if (item != null) {
                                builder.add(item);
                            }
                            // lists require separator, and allows separator after last.
                            if (tokenizer.expectSymbol("list separator or end", Token.kLineSep1, Token.kListEnd) == Token.kListEnd) {
                                break;
                            }
                            next = tokenizer.expect("list value or end");
                        }
                        return builder.build();
                    }
                    break;
                }
            default:
                {
                    throw new TokenizerException(next, descriptor.getType() + " not supported!").setLine(tokenizer.getLine());
                }
        }
    } catch (ProvidenceConfigException e) {
        throw new TokenizerException(next, e.getMessage()).setLine(tokenizer.getLine());
    }
    throw new TokenizerException(next, "Unhandled value \"%s\" for type %s", next.asString(), descriptor.getType()).setLine(tokenizer.getLine());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PList(net.morimekta.providence.descriptor.PList) PEnumValue(net.morimekta.providence.PEnumValue) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) LinkedHashMap(java.util.LinkedHashMap) PSet(net.morimekta.providence.descriptor.PSet) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) PMap(net.morimekta.providence.descriptor.PMap)

Example 3 with PSet

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

the class HazelcastPortableProgramFormatter method appendTypeField.

private void appendTypeField(JField field) {
    if (field.portableRequiresBinarySerialization()) {
        writer.formatln(".addByteArrayField(\"%s\")", field.name());
        return;
    }
    switch(field.type()) {
        case BINARY:
            writer.formatln(".addByteArrayField(\"%s\")", field.name());
            break;
        case BYTE:
            writer.formatln(".addByteField(\"%s\")", field.name());
            break;
        case BOOL:
            writer.formatln(".addBooleanField(\"%s\")", field.name());
            break;
        case DOUBLE:
            writer.formatln(".addDoubleField(\"%s\")", field.name());
            break;
        case ENUM:
        case I32:
            writer.formatln(".addIntField(\"%s\")", field.name());
            break;
        case I16:
            writer.formatln(".addShortField(\"%s\")", field.name());
            break;
        case I64:
            writer.formatln(".addLongField(\"%s\")", field.name());
            break;
        case STRING:
            writer.formatln(".addUTFField(\"%s\")", field.name());
            break;
        case LIST:
            final PList pList = field.toPList();
            appendCollectionTypeField(field, pList.itemDescriptor());
            break;
        case SET:
            final PSet pSet = field.toPSet();
            appendCollectionTypeField(field, pSet.itemDescriptor());
            break;
        case MESSAGE:
            writer.formatln(".addPortableField(\"%s\", %s(%s))", field.name(), camelCase("get", field.field().getDescriptor().getName() + "Definition"), PORTABLE_VERSION);
            break;
        default:
            throw new GeneratorException("Not implemented appendTypeField for type: " + field.type() + " in " + this.getClass().getSimpleName());
    }
}
Also used : PList(net.morimekta.providence.descriptor.PList) PSet(net.morimekta.providence.descriptor.PSet) GeneratorException(net.morimekta.providence.generator.GeneratorException)

Example 4 with PSet

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

the class TProtocolSerializer method readTypedValue.

private Object readTypedValue(byte tType, PDescriptor type, TProtocol protocol, boolean allowNull) throws TException, SerializerException {
    if (tType != forType(type.getType())) {
        throw new SerializerException("Expected type " + asString(forType(type.getType())) + " but found " + asString(tType));
    }
    switch(tType) {
        case BinaryType.BOOL:
            return protocol.readBool();
        case BinaryType.BYTE:
            return protocol.readByte();
        case BinaryType.I16:
            return protocol.readI16();
        case BinaryType.I32:
            if (PType.ENUM == type.getType()) {
                PEnumDescriptor<?> et = (PEnumDescriptor<?>) type;
                PEnumBuilder<?> eb = et.builder();
                int value = protocol.readI32();
                eb.setById(value);
                if (!eb.valid() && !allowNull) {
                    throw new SerializerException("Invalid enum value " + value + " for " + et.getQualifiedName());
                }
                return eb.build();
            } else {
                return protocol.readI32();
            }
        case BinaryType.I64:
            return protocol.readI64();
        case BinaryType.DOUBLE:
            return protocol.readDouble();
        case BinaryType.STRING:
            if (type == PPrimitive.BINARY) {
                ByteBuffer buffer = protocol.readBinary();
                return Binary.wrap(buffer.array());
            }
            return protocol.readString();
        case BinaryType.STRUCT:
            return readMessage(protocol, (PMessageDescriptor<?, ?>) type);
        case BinaryType.LIST:
            TList listInfo = protocol.readListBegin();
            PList<Object> lDesc = (PList<Object>) type;
            PDescriptor liDesc = lDesc.itemDescriptor();
            PList.Builder<Object> list = lDesc.builder();
            for (int i = 0; i < listInfo.size; ++i) {
                list.add(readTypedValue(listInfo.elemType, liDesc, protocol, false));
            }
            protocol.readListEnd();
            return list.build();
        case BinaryType.SET:
            TSet setInfo = protocol.readSetBegin();
            PSet<Object> sDesc = (PSet<Object>) type;
            PDescriptor siDesc = sDesc.itemDescriptor();
            PSet.Builder<Object> set = sDesc.builder();
            for (int i = 0; i < setInfo.size; ++i) {
                set.add(readTypedValue(setInfo.elemType, siDesc, protocol, false));
            }
            protocol.readSetEnd();
            return set.build();
        case BinaryType.MAP:
            TMap mapInfo = protocol.readMapBegin();
            PMap<Object, Object> mDesc = (PMap<Object, Object>) type;
            PDescriptor mkDesc = mDesc.keyDescriptor();
            PDescriptor miDesc = mDesc.itemDescriptor();
            PMap.Builder<Object, Object> map = mDesc.builder();
            for (int i = 0; i < mapInfo.size; ++i) {
                Object key = readTypedValue(mapInfo.keyType, mkDesc, protocol, false);
                Object val = readTypedValue(mapInfo.valueType, miDesc, protocol, false);
                map.put(key, val);
            }
            protocol.readMapEnd();
            return map.build();
        default:
            throw new SerializerException("Unsupported protocol field type: " + tType);
    }
}
Also used : PList(net.morimekta.providence.descriptor.PList) TSet(org.apache.thrift.protocol.TSet) PMap(net.morimekta.providence.descriptor.PMap) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor) SerializerException(net.morimekta.providence.serializer.SerializerException) ByteBuffer(java.nio.ByteBuffer) TMap(org.apache.thrift.protocol.TMap) TList(org.apache.thrift.protocol.TList) PDescriptor(net.morimekta.providence.descriptor.PDescriptor) PSet(net.morimekta.providence.descriptor.PSet)

Example 5 with PSet

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

the class TProtocolSerializer method writeTypedValue.

private void writeTypedValue(Object item, PDescriptor type, TProtocol protocol) throws TException, SerializerException {
    switch(type.getType()) {
        case BOOL:
            protocol.writeBool((Boolean) item);
            break;
        case BYTE:
            protocol.writeByte((Byte) item);
            break;
        case I16:
            protocol.writeI16((Short) item);
            break;
        case I32:
            protocol.writeI32((Integer) item);
            break;
        case I64:
            protocol.writeI64((Long) item);
            break;
        case DOUBLE:
            protocol.writeDouble((Double) item);
            break;
        case STRING:
            protocol.writeString((String) item);
            break;
        case BINARY:
            protocol.writeBinary(((Binary) item).getByteBuffer());
            break;
        case ENUM:
            PEnumValue<?> value = (PEnumValue<?>) item;
            protocol.writeI32(value.asInteger());
            break;
        case MESSAGE:
            writeMessage((PMessage<?, ?>) item, protocol);
            break;
        case LIST:
            PList<?> lType = (PList<?>) type;
            List<?> list = (List<?>) item;
            TList listInfo = new TList(forType(lType.itemDescriptor().getType()), list.size());
            protocol.writeListBegin(listInfo);
            for (Object i : list) {
                writeTypedValue(i, lType.itemDescriptor(), protocol);
            }
            protocol.writeListEnd();
            break;
        case SET:
            PSet<?> sType = (PSet<?>) type;
            Set<?> set = (Set<?>) item;
            TSet setInfo = new TSet(forType(sType.itemDescriptor().getType()), set.size());
            protocol.writeSetBegin(setInfo);
            for (Object i : set) {
                writeTypedValue(i, sType.itemDescriptor(), protocol);
            }
            protocol.writeSetEnd();
            break;
        case MAP:
            PMap<?, ?> mType = (PMap<?, ?>) type;
            Map<?, ?> map = (Map<?, ?>) item;
            protocol.writeMapBegin(new TMap(forType(mType.keyDescriptor().getType()), forType(mType.itemDescriptor().getType()), map.size()));
            for (Map.Entry<?, ?> entry : map.entrySet()) {
                writeTypedValue(entry.getKey(), mType.keyDescriptor(), protocol);
                writeTypedValue(entry.getValue(), mType.itemDescriptor(), protocol);
            }
            protocol.writeMapEnd();
            break;
        default:
            break;
    }
}
Also used : TSet(org.apache.thrift.protocol.TSet) Set(java.util.Set) PSet(net.morimekta.providence.descriptor.PSet) PList(net.morimekta.providence.descriptor.PList) PEnumValue(net.morimekta.providence.PEnumValue) TSet(org.apache.thrift.protocol.TSet) PMap(net.morimekta.providence.descriptor.PMap) TMap(org.apache.thrift.protocol.TMap) TList(org.apache.thrift.protocol.TList) PSet(net.morimekta.providence.descriptor.PSet) TList(org.apache.thrift.protocol.TList) PList(net.morimekta.providence.descriptor.PList) List(java.util.List) Map(java.util.Map) PMap(net.morimekta.providence.descriptor.PMap) TMap(org.apache.thrift.protocol.TMap)

Aggregations

PSet (net.morimekta.providence.descriptor.PSet)9 PList (net.morimekta.providence.descriptor.PList)7 PMap (net.morimekta.providence.descriptor.PMap)6 PEnumDescriptor (net.morimekta.providence.descriptor.PEnumDescriptor)4 PDescriptor (net.morimekta.providence.descriptor.PDescriptor)3 Map (java.util.Map)2 PEnumBuilder (net.morimekta.providence.PEnumBuilder)2 PEnumValue (net.morimekta.providence.PEnumValue)2 PMessageBuilder (net.morimekta.providence.PMessageBuilder)2 PDescriptorProvider (net.morimekta.providence.descriptor.PDescriptorProvider)2 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)2 SerializerException (net.morimekta.providence.serializer.SerializerException)2 TList (org.apache.thrift.protocol.TList)2 TMap (org.apache.thrift.protocol.TMap)2 TSet (org.apache.thrift.protocol.TSet)2 Test (org.junit.Test)2 ByteBuffer (java.nio.ByteBuffer)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1