use of cz.habarta.typescript.generator.util.Pair in project openremote by openremote.
the class EnumWithInterfacesExtension method parseEnum.
/**
* This method builds up a model that can be used to populate the enum class template;
*/
// TODO: Is there a nicer way of doing some of the below with existing classes?
protected EnumInterfaceModel parseEnum(TsEnumModel enm, TsModel model, Settings settings) {
List<TsPropertyModel> interfacePropertyModels = Arrays.stream(enm.getOrigin().getInterfaces()).map(model::getBean).filter(b -> !Objects.isNull(b)).flatMap(tsBean -> tsBean.getProperties().stream()).collect(Collectors.toList());
// Get field info from implemented interfaces and ensure optional fields are at the end
List<Pair<Field, Optional<TsPropertyModel>>> enumFieldInfos = Arrays.stream(enm.getOrigin().getDeclaredFields()).filter(interfaceFieldPredicate).map(field -> {
// Ignore case in comparison as TsPropertyModel has the first two letters as lower case for some reason might be coming from jackson - not investigated (e.g. getAArray() -> aarray)
Optional<TsPropertyModel> matchingPropertyModel = interfacePropertyModels.stream().filter(ipm -> ipm.getName().equalsIgnoreCase(field.getName())).findFirst();
return Pair.of(field, matchingPropertyModel);
}).sorted((pair1, pair2) -> {
boolean pair1IsOptional = pair1.getValue2().map(mpm -> mpm.getTsType() instanceof TsType.OptionalType).orElse(false);
boolean pair2IsOptional = pair2.getValue2().map(mpm -> mpm.getTsType() instanceof TsType.OptionalType).orElse(false);
return pair1IsOptional ? pair2IsOptional ? 0 : 1 : pair2IsOptional ? -1 : 0;
}).collect(Collectors.toList());
Map<String, String> fieldTypeMap = enumFieldInfos.stream().collect(LinkedHashMap::new, (map, enumFieldInfo) -> {
Field field = enumFieldInfo.getValue1();
Optional<TsPropertyModel> tsPropertyModel = enumFieldInfo.getValue2();
String fieldTypeStr;
boolean propertyOptional = tsPropertyModel.map(mpm -> mpm.getTsType() instanceof TsType.OptionalType).orElseGet(() -> settings.optionalProperties == OptionalProperties.all);
// TODO: Need access to the DefaultTypeProcessor known types here but it is private
if (tsPropertyModel.isPresent()) {
fieldTypeStr = tsPropertyModel.get().getTsType().toString();
} else if (field.getType() == String.class) {
fieldTypeStr = TsType.String.toString();
} else if (Number.class.isAssignableFrom(field.getType())) {
fieldTypeStr = TsType.Number.toString();
} else if (field.getType() == Boolean.class) {
fieldTypeStr = TsType.Boolean.toString();
} else if (field.getType() == Date.class) {
fieldTypeStr = TsType.Date.toString();
} else if (field.getType() == Void.class) {
fieldTypeStr = TsType.Void.toString();
} else {
fieldTypeStr = field.getType().getSimpleName();
}
map.put(propertyOptional ? field.getName() + "?" : field.getName(), fieldTypeStr);
}, HashMap::putAll);
List<EnumInterfaceMemberModel> memberModels = Arrays.stream(enm.getOrigin().getEnumConstants()).map(m -> {
String memberName = ((Enum<?>) m).name();
Map<String, Object> memberFieldValues = enumFieldInfos.stream().collect(LinkedHashMap::new, (map, enumFieldInfo) -> {
Field field = enumFieldInfo.getValue1();
Object val = null;
try {
field.setAccessible(true);
val = field.get(m);
} catch (IllegalAccessException ex) {
TypeScriptGenerator.getLogger().verbose("Failed to get field '" + field.getName() + "' value: " + ex.getMessage());
}
map.put(field.getName(), val);
}, HashMap::putAll);
return new EnumInterfaceMemberModel(memberName, memberFieldValues);
}).collect(Collectors.toList());
return new EnumInterfaceModel(memberModels, fieldTypeMap);
}
Aggregations