use of org.webpieces.router.api.extensions.Meta in project webpieces by deanhiller.
the class ParamToObjectTranslatorImpl method translate.
private XFuture<Object> translate(RouterRequest req, Method method, ParamNode valuesToUse, Meta fieldMeta, Validation validator) {
Class<?> fieldClass = fieldMeta.getFieldClass();
ObjectStringConverter<?> converter = objectTranslator.getConverter(fieldClass);
if (converter != null) {
Object convert = convert(req, method, valuesToUse, fieldMeta, converter, validator);
return XFuture.completedFuture(convert);
} else if (fieldClass.isArray()) {
throw new UnsupportedOperationException("not done yet...let me know and I will do it=" + fieldMeta);
} else if (fieldClass.isEnum()) {
throw new UnsupportedOperationException("You need to install a " + ObjectStringConverter.class.getSimpleName() + " for this enum " + fieldClass + " meta class=" + fieldMeta.getFieldClass() + ". This\n" + "can be done like so in one of your guice modules....\n" + "\t\tMultibinder<ObjectStringConverter> conversionBinder = Multibinder.newSetBinder(binder, ObjectStringConverter.class);\n" + "\t\tconversionBinder.addBinding().to({YOUR_ENUM}.WebConverter.class);");
} else if (List.class.isAssignableFrom(fieldClass)) {
if (valuesToUse == null)
return XFuture.completedFuture(new ArrayList<>());
else if (valuesToUse instanceof ArrayNode) {
List<ParamNode> paramNodes = ((ArrayNode) valuesToUse).getList();
return createList(req, method, fieldMeta, validator, paramNodes);
} else if (valuesToUse instanceof ValueNode) {
List<ParamNode> paramNodes = new ArrayList<>();
paramNodes.add(valuesToUse);
return createList(req, method, fieldMeta, validator, paramNodes);
}
throw new IllegalArgumentException("Found List on field or param=" + fieldMeta + " but did not find ArrayNode type");
} else if (valuesToUse instanceof ArrayNode) {
throw new IllegalArgumentException("Incoming array need a type List but instead found type=" + fieldClass + " on field=" + fieldMeta);
} else if (valuesToUse instanceof ValueNode) {
ValueNode v = (ValueNode) valuesToUse;
String fullName = v.getFullName();
throw new IllegalArgumentException("Could not convert incoming value=" + v.getValue() + " of key name=" + fullName + " field=" + fieldMeta);
} else if (valuesToUse == null) {
// validate if null is ok or not
fieldMeta.validateNullValue();
return XFuture.completedFuture(null);
} else if (!(valuesToUse instanceof ParamTreeNode)) {
throw new IllegalStateException("Bug, must be missing a case. v=" + valuesToUse + " type to field=" + fieldMeta);
}
ParamTreeNode tree = (ParamTreeNode) valuesToUse;
EntityLookup pluginLookup = fetchPluginLoader(fieldClass);
XFuture<Object> future = null;
if (pluginLookup != null) {
future = pluginLookup.find(fieldMeta, tree, c -> createBean(c));
if (future == null)
throw new IllegalStateException("plugin=" + pluginLookup.getClass() + " failed to create bean. This is a plugin bug");
} else {
Object newBean = createBean(fieldClass);
future = XFuture.completedFuture(newBean);
}
return future.thenCompose(bean -> {
return fillBeanIn(bean, tree, req, method, validator);
});
}
use of org.webpieces.router.api.extensions.Meta in project webpieces by deanhiller.
the class ParamToObjectTranslatorImpl method createList.
@SuppressWarnings("unchecked")
private XFuture<Object> createList(RouterRequest req, Method method, Meta fieldMeta, Validation validator, List<ParamNode> paramNodes) {
List<Object> list = new ArrayList<>();
ParameterizedType type = (ParameterizedType) fieldMeta.getParameterizedType();
Type[] actualTypeArguments = type.getActualTypeArguments();
Type type2 = actualTypeArguments[0];
@SuppressWarnings("rawtypes") GenericMeta genMeta = new GenericMeta((Class) type2);
XFuture<List<Object>> future = XFuture.completedFuture(list);
for (ParamNode node : paramNodes) {
XFuture<Object> fut;
if (node != null) {
fut = translate(req, method, node, genMeta, validator);
} else {
fut = XFuture.completedFuture(null);
}
future = future.thenCompose(myList -> {
return fut.thenApply(b -> {
myList.add(b);
return myList;
});
});
}
return future.thenApply(l -> (Object) l);
}
Aggregations