dev.sixpack.generator.SchemaBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sixpack-sdk Show documentation
Show all versions of sixpack-sdk Show documentation
SDK to develop generators part of the Sixpack solution
The newest version!
package dev.sixpack.generator;
import dev.sixpack.api.data.Schema;
import dev.sixpack.api.data.Field;
import dev.sixpack.api.data.Item;
import dev.sixpack.api.data.FieldType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static dev.sixpack.utils.FormatUtils.mFormat;
public class SchemaBuilder {
final private Map factories;
SchemaBuilder(Map factories) {
this.factories = factories;
}
public List- build() {
return factories.values().stream()
.map(generatorRecord -> new Item(
generatorRecord.getItem(),
entityFromClass(generatorRecord.getInput()),
null, // TODO: implement
null, // TODO: implement
entityFromClass(generatorRecord.getOutput()),
generatorRecord.getTemplates(),
generatorRecord))
.collect(Collectors.toList());
}
public static Schema entityFromClass(Class> type) {
Schema entity = new Schema(type.getSimpleName(), new ArrayList<>());
Arrays.stream(type.getDeclaredFields())
.forEach(field ->
entity.getFields().add(fieldFromField(field)));
return entity;
}
private static Field fieldFromField(java.lang.reflect.Field field) {
validateField(field);
FieldType type;
List
values;
if (isEnum(field)) {
type = FieldType.Select;
values = Arrays.stream(field.getType().getEnumConstants())
.map(enumConstant -> (String) ((Enum>) enumConstant).name())
.collect(Collectors.toList());
} else if (isInteger(field)) {
type = FieldType.Integer;
values = null;
} else if (isString(field)) {
type = FieldType.String;
values = null;
} else if (isBoolean(field)) {
type = FieldType.Boolean;
values = null;
} else {
type = FieldType.Entity;
values = null;
}
return new Field(field.getName(), type, values);
}
private static void validateField(java.lang.reflect.Field field) {
Class> type = field.getType();
if (int.class.isAssignableFrom(type)
|| long.class.isAssignableFrom(type)
|| short.class.isAssignableFrom(type)
|| boolean.class.isAssignableFrom(field.getType())) {
throw new IllegalArgumentException(
mFormat("Field '{}' is a primitive type. Primitive types int, long, short, boolean should be replaced by their wrapper counterparts Integer, Long, Short, Boolean. Those allow to distinguish between 'zero or false' and 'unspecified'.",
field.getName()));
}
if (!(Integer.class.isAssignableFrom(type)
|| Long.class.isAssignableFrom(type)
|| Short.class.isAssignableFrom(type)
|| Boolean.class.isAssignableFrom(type)
|| String.class.isAssignableFrom(type)
|| Enum.class.isAssignableFrom(type))) {
throw new IllegalArgumentException(
mFormat("Field '{}' of type '{}' is not a supported type. Please use one of the following types: Integer, Long, Short, Boolean, String, enum (simple).",
field.getName(),
type.getSimpleName()));
}
}
private static boolean isEnum(java.lang.reflect.Field field) {
return Enum.class.isAssignableFrom(field.getType());
}
private static boolean isInteger(java.lang.reflect.Field field) {
Class> type = field.getType();
return Integer.class.isAssignableFrom(type)
|| Long.class.isAssignableFrom(type)
|| Short.class.isAssignableFrom(type);
}
private static boolean isString(java.lang.reflect.Field field) {
return String.class.isAssignableFrom(field.getType());
}
private static boolean isBoolean(java.lang.reflect.Field field) {
return Boolean.class.isAssignableFrom(field.getType());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy