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;
}
}
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());
}
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);
}
Aggregations