EnumDefinitionToDataModelMapper.java 2.8 KB
Newer Older
1 2
package com.kobylynskyi.graphql.codegen.mapper;

3
import com.kobylynskyi.graphql.codegen.model.EnumValueDefinition;
4
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
5
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedEnumTypeDefinition;
6
import graphql.language.Comment;
7 8
import graphql.language.Directive;
import graphql.language.DirectivesContainer;
9

10
import java.util.*;
11
import java.util.stream.Collectors;
12 13 14 15 16 17 18 19 20 21 22 23 24

import static com.kobylynskyi.graphql.codegen.model.DataModelFields.*;

/**
 * Map enum definition to a Freemarker data model
 *
 * @author kobylynskyi
 */
public class EnumDefinitionToDataModelMapper {

    /**
     * Map field definition to a Freemarker data model
     *
25 26
     * @param mappingConfig Global mapping configuration
     * @param definition    Definition of enum type including base definition and its extensions
27 28
     * @return Freemarker data model of the GraphQL enum
     */
29
    public static Map<String, Object> map(MappingConfig mappingConfig, ExtendedEnumTypeDefinition definition) {
30
        Map<String, Object> dataModel = new HashMap<>();
31 32
        // type/enum/input/interface/union classes do not require any imports
        dataModel.put(PACKAGE, MapperUtils.getModelPackageName(mappingConfig));
33
        dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingConfig, definition));
34
        dataModel.put(JAVA_DOC, definition.getJavaDoc());
35
        dataModel.put(FIELDS, map(definition.getValueDefinitions()));
36 37 38
        return dataModel;
    }

39 40 41 42 43 44
    /**
     * Mapper from GraphQL's EnumValueDefinition to a Freemarker-understandable format
     *
     * @param enumValueDefinitions list of GraphQL EnumValueDefinition types
     * @return list of strings
     */
45
    private static List<EnumValueDefinition> map(List<graphql.language.EnumValueDefinition> enumValueDefinitions) {
46
        return enumValueDefinitions.stream()
47 48
                .map(f -> new EnumValueDefinition(
                        MapperUtils.capitalizeIfRestricted(f.getName()),
49 50
                        getJavaDoc(f.getComments()),
                        isDeprecated(f)))
51 52 53
                .collect(Collectors.toList());
    }

54 55 56 57 58 59
    private static boolean isDeprecated(DirectivesContainer<?> directivesContainer) {
        return directivesContainer.getDirectives().stream()
                .map(Directive::getName)
                .anyMatch(Deprecated.class.getSimpleName()::equalsIgnoreCase);
    }

60 61 62 63 64 65 66 67 68
    private static List<String> getJavaDoc(List<Comment> comments) {
        if (comments == null) {
            return Collections.emptyList();
        }
        return comments.stream()
                .map(Comment::getContent)
                .filter(Objects::nonNull)
                .map(String::trim)
                .filter(s -> !s.isEmpty())
69 70 71
                .collect(Collectors.toList());
    }

72
}