use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class ToPojo method readObjectArrayValue.
/**
* Returns the string representation of the code that reads an object array property.
*
* @param ref The reference.
* @param source The type of the reference.
* @param property The property to read.
* @return The code.
*/
private static String readObjectArrayValue(String ref, TypeDef source, Property property) {
StringBuilder sb = new StringBuilder();
Method getter = getterOf(source, property);
TypeRef getterTypeRef = getter.getReturnType();
TypeRef propertyTypeRef = property.getTypeRef();
if (propertyTypeRef instanceof ClassRef && getterTypeRef instanceof ClassRef) {
String nextRef = variables.pop();
try {
TypeDef propertyType = GetDefinition.of((ClassRef) propertyTypeRef);
TypeDef getterType = GetDefinition.of((ClassRef) getterTypeRef);
if (Types.STRING.equals(getterType)) {
sb.append(ref + " instanceof Map ? toStringArray(((Map)" + ref + ").get(\"" + getter.getName() + "\")) : toStringArray(" + ref + ")");
} else {
sb.append(indent(ref)).append("Arrays.stream(");
sb.append("(Map[])(" + ref + " instanceof Map ? ((Map)" + ref + ").getOrDefault(\"" + getter.getName() + "\" , new Map[0]) : new Map[0]))");
sb.append(".map(").append(nextRef).append(" ->").append(convertMap(nextRef, getterType, propertyType)).append(")");
sb.append(".toArray(size-> new " + propertyType.getFullyQualifiedName() + "[size])");
}
} finally {
variables.push(nextRef);
}
return sb.toString();
}
throw new IllegalArgumentException("Expected an object property and a matching object getter!!");
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class ToPojo method readObjectArrayProperty.
/**
* Returns the string representation of the code that reads an object array property.
*
* @param ref The reference.
* @param source The type of the reference.
* @param property The property to read.
* @return The code.
*/
private static String readObjectArrayProperty(String ref, TypeDef source, Property property) {
StringBuilder sb = new StringBuilder();
Method getter = getterOf(source, property);
TypeRef getterTypeRef = getter.getReturnType();
TypeRef propertyTypeRef = property.getTypeRef();
if (propertyTypeRef instanceof ClassRef && getterTypeRef instanceof ClassRef) {
String nextRef = variables.pop();
try {
TypeDef propertyType = GetDefinition.of((ClassRef) propertyTypeRef);
TypeDef getterType = GetDefinition.of((ClassRef) getterTypeRef);
sb.append("Arrays.asList(").append(ref).append(".").append(getter.getName()).append("())").append(".stream().map(").append(nextRef).append(" ->").append(convertReference(nextRef, getterType, propertyType)).append(")").append(".collect(Collectors.toList()).toArray(new " + propertyType.getFullyQualifiedName() + "[0])");
} finally {
variables.push(nextRef);
}
return sb.toString();
}
throw new IllegalArgumentException("Expected an object property and a matching object getter!!");
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class ToPojo method getDefaultValue.
private static String getDefaultValue(Attributeable attributeable) {
Object value = attributeable.getAttribute(DEFAULT_VALUE);
String stringVal = value != null ? String.valueOf(value) : null;
TypeRef typeRef = null;
if (attributeable instanceof Method) {
typeRef = ((Method) attributeable).getReturnType();
} else if (attributeable instanceof Property) {
typeRef = ((Property) attributeable).getTypeRef();
} else {
throw new IllegalArgumentException("Method 'getDefaultValue' accepts Property or Method as argument!");
}
if (typeRef.getDimensions() > 0) {
StringBuilder sb = new StringBuilder();
if (typeRef instanceof PrimitiveRef) {
sb.append(((PrimitiveRef) typeRef).getName());
} else if (typeRef instanceof ClassRef) {
sb.append("new ").append(((ClassRef) typeRef).getFullyQualifiedName());
}
for (int d = 0; d < typeRef.getDimensions(); d++) {
sb.append("[0]");
}
return sb.toString();
}
if (Types.STRING_REF.equals(typeRef) && value != null && !String.valueOf(value).startsWith("\"")) {
return "\"" + value + "\"";
} else if (value instanceof Element) {
Element element = (Element) value;
if (element.getKind() == ElementKind.ENUM_CONSTANT) {
return element.getEnclosingElement() + "." + element.getSimpleName();
}
} else if (stringVal != null && stringVal.startsWith("@")) {
String annotationFQCN = stringVal.substring(1);
TypeDef annotationDef = DefinitionRepository.getRepository().getDefinition(annotationFQCN);
if (annotationDef != null) {
TypeDef pojoDef = ClazzAs.POJO.apply(TypeArguments.apply(annotationDef));
Optional<AnnotationRef> pojoAnnotation = getPojoAnnotation(pojoDef);
Optional<Method> builderFromDefaults = getBuilderFromDefaults(pojoDef);
if (pojoAnnotation.isPresent() && builderFromDefaults.isPresent()) {
return pojoDef.getFullyQualifiedName() + "." + builderFromDefaults.get().getName() + "().build()";
} else if (BuilderUtils.hasDefaultConstructor(pojoDef)) {
return "new " + pojoDef.getFullyQualifiedName() + "()";
}
}
return "null";
} else if (stringVal == null && typeRef instanceof PrimitiveRef) {
if (((PrimitiveRef) typeRef).getName().equals("boolean")) {
return "false";
} else {
return "0";
}
}
return stringVal;
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class ToPojo method convertReference.
/**
* Converts a reference from the source type, to the target.
*
* @param ref The ref.
* @param source The source type of the reference..
* @param target The target type.
* @return
*/
private static String convertReference(String ref, TypeDef source, TypeDef target) {
Method ctor = BuilderUtils.findBuildableConstructor(target);
String arguments = ctor.getArguments().stream().map(p -> readProperty(ref, source, p)).collect(joining(",\n "));
StringBuilder sb = new StringBuilder();
sb.append("new ").append(target.getFullyQualifiedName()).append("(").append(arguments).append(")");
return sb.toString();
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class ToPojo method convertMap.
/**
* Converts a map describing the source type, to the target.
*
* @param ref The ref.
* @param source The source type of the reference (e.g. the annotation).
* @param target The target type (e.g. the generated pojo).
* @return
*/
private static String convertMap(String ref, TypeDef source, TypeDef target) {
Method ctor = BuilderUtils.findBuildableConstructor(target);
String arguments = ctor.getArguments().stream().map(p -> readMapValue(ref, source, p)).collect(joining(",\n", "\n", ""));
StringBuilder sb = new StringBuilder();
sb.append("new ").append(target.getFullyQualifiedName()).append("(").append(arguments).append(")");
return sb.toString();
}
Aggregations