Search in sources :

Example 6 with PDescriptor

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

the class JacksonMessageFormatter method appendReadValue.

private void appendReadValue(JField field) throws GeneratorException {
    switch(field.type()) {
        case MAP:
            {
                PMap mType = (PMap) field.field().getDescriptor();
                PDescriptor kType = mType.keyDescriptor();
                writer.formatln("%s kType = ctxt.getTypeFactory().constructSimpleType(%s.class, null);", JavaType.class.getName(), helper.getFieldType(kType));
                PDescriptor iType = mType.itemDescriptor();
                if (iType instanceof PMap) {
                    PMap imType = (PMap) iType;
                    PDescriptor ikType = imType.keyDescriptor();
                    PDescriptor iiType = imType.itemDescriptor();
                    if (iiType instanceof PContainer) {
                        throw new GeneratorException("Too many levels of containers: " + field.toString());
                    }
                    // double level of container...
                    writer.formatln("%s iType = ctxt.getTypeFactory().constructMapType(%s.class, %s.class, %s.class);", MapType.class.getName(), HashMap.class.getName(), helper.getFieldType(ikType), helper.getFieldType(iiType));
                } else if (iType instanceof PContainer) {
                    PContainer icType = (PContainer) iType;
                    PDescriptor iiType = icType.itemDescriptor();
                    if (iiType instanceof PContainer) {
                        throw new GeneratorException("Too many levels of containers: " + field.toString());
                    }
                    // double level of container...
                    writer.formatln("%s iType = ctxt.getTypeFactory().constructArrayType(%s.class);", MapType.class.getName(), helper.getFieldType(iiType));
                } else {
                    writer.formatln("%s iType = ctxt.getTypeFactory().constructSimpleType(%s.class, null);", JavaType.class.getName(), helper.getFieldType(iType));
                }
                writer.formatln("%s type = ctxt.getTypeFactory().constructMapType(%s.class, kType, iType);", MapType.class.getName(), HashMap.class.getName());
                writer.formatln("builder.%s(ctxt.readValue(jp, type));", field.setter());
                break;
            }
        case SET:
        case LIST:
            {
                PContainer cType = (PContainer) field.field().getDescriptor();
                PDescriptor iType = cType.itemDescriptor();
                if (iType instanceof PMap) {
                    PMap imType = (PMap) iType;
                    PDescriptor ikType = imType.keyDescriptor();
                    PDescriptor iiType = imType.itemDescriptor();
                    // double level of container...
                    writer.formatln("%s itype = ctxt.getTypeFactory().constructMapType(%s.class, %s.class, %s.class);", MapType.class.getName(), LinkedHashMap.class.getName(), helper.getFieldType(ikType), helper.getFieldType(iiType));
                    writer.formatln("%s type = ctxt.getTypeFactory().constructArrayType(itype);", ArrayType.class.getName(), helper.getFieldType(iType));
                    writer.formatln("builder.%s(%s.asList(ctxt.readValue(jp, type)));", field.setter(), Arrays.class.getName());
                } else if (iType instanceof PContainer) {
                    PContainer icType = (PContainer) iType;
                    PDescriptor iiType = icType.itemDescriptor();
                    // double level of container...
                    writer.formatln("%s itype = ctxt.getTypeFactory().constructArrayType(%s.class);", ArrayType.class.getName(), helper.getFieldType(iiType));
                    writer.formatln("%s type = ctxt.getTypeFactory().constructArrayType(itype);", ArrayType.class.getName(), helper.getFieldType(iType));
                    writer.formatln("builder.%s(%s.asList(ctxt.readValue(jp, type)));", field.setter(), Arrays.class.getName());
                } else {
                    writer.formatln("%s type = ctxt.getTypeFactory().constructArrayType(%s.class);", ArrayType.class.getName(), helper.getFieldType(iType));
                    writer.formatln("builder.%s(%s.asList(ctxt.readValue(jp, type)));", field.setter(), Arrays.class.getName());
                }
                break;
            }
        case BINARY:
        case STRING:
        case MESSAGE:
        case ENUM:
            writer.formatln("builder.%s(ctxt.readValue(jp, %s.class));", field.setter(), field.instanceType());
            break;
        default:
            writer.formatln("builder.%s(ctxt.readValue(jp, %s.TYPE));", field.setter(), field.instanceType());
            break;
    }
}
Also used : ArrayType(com.fasterxml.jackson.databind.type.ArrayType) PDescriptor(net.morimekta.providence.descriptor.PDescriptor) JavaType(com.fasterxml.jackson.databind.JavaType) PContainer(net.morimekta.providence.descriptor.PContainer) PMap(net.morimekta.providence.descriptor.PMap) GeneratorException(net.morimekta.providence.generator.GeneratorException) Arrays(java.util.Arrays) MapType(com.fasterxml.jackson.databind.type.MapType)

Example 7 with PDescriptor

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

the class ProvidenceConfigUtil method getInMessage.

/**
 * Look up a key in the message structure. If the key is not found, return
 * the default value. Note that the default value will be converted to the
 * type of the declared field, not returned verbatim.
 *
 * @param message The message to look up into.
 * @param key The key to look up.
 * @param defValue The default value.
 * @return The value found or the default.
 * @throws ProvidenceConfigException When unable to get value from message.
 */
static Object getInMessage(PMessage message, final String key, Object defValue) throws ProvidenceConfigException {
    String sub = key;
    String name;
    PMessageDescriptor descriptor = message.descriptor();
    while (sub.contains(".")) {
        int idx = sub.indexOf(".");
        name = sub.substring(0, idx);
        sub = sub.substring(idx + 1);
        PField field = descriptor.findFieldByName(name);
        if (field == null) {
            throw new ProvidenceConfigException("Message " + descriptor.getQualifiedName() + " has no field named " + name);
        }
        PDescriptor fieldDesc = field.getDescriptor();
        if (fieldDesc.getType() != PType.MESSAGE) {
            throw new ProvidenceConfigException("Field '" + name + "' is not of message type in " + descriptor.getQualifiedName());
        }
        descriptor = (PMessageDescriptor) fieldDesc;
        if (message != null) {
            message = (PMessage) message.get(field.getId());
        }
    }
    PField field = descriptor.findFieldByName(sub);
    if (field == null) {
        throw new ProvidenceConfigException("Message " + descriptor.getQualifiedName() + " has no field named " + sub);
    }
    if (message == null || !message.has(field.getId())) {
        return asType(field.getDescriptor(), defValue);
    }
    return message.get(field.getId());
}
Also used : PDescriptor(net.morimekta.providence.descriptor.PDescriptor) PField(net.morimekta.providence.descriptor.PField) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException)

Example 8 with PDescriptor

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

the class ConstParserTest method parse.

private Object parse(String context, String type, String text) throws IOException {
    ConstParser parser = new ConstParser(registry, context, type.length(), text.length());
    PDescriptor descriptor = registry.getProvider(type, context, ImmutableMap.of()).descriptor();
    ByteArrayInputStream bais = new ByteArrayInputStream(text.getBytes(UTF_8));
    return parser.parse(bais, descriptor);
}
Also used : PDescriptor(net.morimekta.providence.descriptor.PDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

PDescriptor (net.morimekta.providence.descriptor.PDescriptor)8 PMap (net.morimekta.providence.descriptor.PMap)4 PMessageBuilder (net.morimekta.providence.PMessageBuilder)3 PEnumDescriptor (net.morimekta.providence.descriptor.PEnumDescriptor)3 PList (net.morimekta.providence.descriptor.PList)3 PSet (net.morimekta.providence.descriptor.PSet)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 PEnumBuilder (net.morimekta.providence.PEnumBuilder)2 PContainer (net.morimekta.providence.descriptor.PContainer)2 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)2 GeneratorException (net.morimekta.providence.generator.GeneratorException)2 SerializerException (net.morimekta.providence.serializer.SerializerException)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 ArrayType (com.fasterxml.jackson.databind.type.ArrayType)1 MapType (com.fasterxml.jackson.databind.type.MapType)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1