Search in sources :

Example 1 with Pair

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);
}
Also used : java.util(java.util) JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) BiFunction(java.util.function.BiFunction) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Matcher(java.util.regex.Matcher) cz.habarta.typescript.generator.emitter(cz.habarta.typescript.generator.emitter) SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) JsonFormat(com.fasterxml.jackson.annotation.JsonFormat) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) Annotated(com.fasterxml.jackson.databind.introspect.Annotated) cz.habarta.typescript.generator(cz.habarta.typescript.generator) ModelTransformer(cz.habarta.typescript.generator.compiler.ModelTransformer) Type(java.lang.reflect.Type) Pair(cz.habarta.typescript.generator.util.Pair) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) Utils(cz.habarta.typescript.generator.util.Utils) Pattern(java.util.regex.Pattern) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) Field(java.lang.reflect.Field) Pair(cz.habarta.typescript.generator.util.Pair)

Aggregations

JsonFormat (com.fasterxml.jackson.annotation.JsonFormat)1 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 Annotated (com.fasterxml.jackson.databind.introspect.Annotated)1 JacksonAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 cz.habarta.typescript.generator (cz.habarta.typescript.generator)1 ModelCompiler (cz.habarta.typescript.generator.compiler.ModelCompiler)1 ModelTransformer (cz.habarta.typescript.generator.compiler.ModelTransformer)1 SymbolTable (cz.habarta.typescript.generator.compiler.SymbolTable)1 cz.habarta.typescript.generator.emitter (cz.habarta.typescript.generator.emitter)1 Pair (cz.habarta.typescript.generator.util.Pair)1 Utils (cz.habarta.typescript.generator.util.Utils)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1