Search in sources :

Example 1 with EnumValueDefinition

use of org.jsonschema2pojo.model.EnumValueDefinition in project jsonschema2pojo by joelittlejohn.

the class EnumRule method buildEnumDefinitionWithNoExtensions.

protected EnumDefinition buildEnumDefinitionWithNoExtensions(String nodeName, JsonNode parentNode, JsonNode enums, JType backingType) {
    ArrayList<EnumValueDefinition> enumValues = new ArrayList<>();
    Collection<String> existingConstantNames = new ArrayList<>();
    for (int i = 0; i < enums.size(); i++) {
        JsonNode value = enums.path(i);
        if (!value.isNull()) {
            String constantName = getConstantName(value.asText(), null);
            constantName = makeUnique(constantName, existingConstantNames);
            existingConstantNames.add(constantName);
            enumValues.add(new EnumValueDefinition(constantName, value.asText()));
        }
    }
    return new EnumDefinition(nodeName, parentNode, backingType, enumValues, EnumDefinitionExtensionType.NONE);
}
Also used : EnumValueDefinition(org.jsonschema2pojo.model.EnumValueDefinition) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) EnumDefinition(org.jsonschema2pojo.model.EnumDefinition)

Example 2 with EnumValueDefinition

use of org.jsonschema2pojo.model.EnumValueDefinition in project jsonschema2pojo by joelittlejohn.

the class EnumRule method addEnumConstants.

protected void addEnumConstants(EnumDefinition enumDefinition, JDefinedClass _enum, Schema schema) {
    JType type = enumDefinition.getBackingType();
    String nodeName = enumDefinition.getNodeName();
    JsonNode parentNode = enumDefinition.getEnumNode();
    for (EnumValueDefinition enumValueDefinition : enumDefinition.values()) {
        JEnumConstant constant = _enum.enumConstant(enumValueDefinition.getName());
        String value = enumValueDefinition.getValue();
        constant.arg(DefaultRule.getDefaultValue(type, value));
        Annotator annotator = ruleFactory.getAnnotator();
        annotator.enumConstant(_enum, constant, value);
        String enumNodeName = nodeName + "#" + value;
        if (enumValueDefinition.hasTitle()) {
            JsonNode titleNode = enumValueDefinition.getTitleNode();
            ruleFactory.getTitleRule().apply(enumNodeName, titleNode, parentNode, constant, schema);
        }
        if (enumValueDefinition.hasDescription()) {
            JsonNode descriptionNode = enumValueDefinition.getDescriptionNode();
            ruleFactory.getDescriptionRule().apply(enumNodeName, descriptionNode, parentNode, constant, schema);
        }
    }
}
Also used : JEnumConstant(com.sun.codemodel.JEnumConstant) Annotator(org.jsonschema2pojo.Annotator) EnumValueDefinition(org.jsonschema2pojo.model.EnumValueDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode) JType(com.sun.codemodel.JType)

Example 3 with EnumValueDefinition

use of org.jsonschema2pojo.model.EnumValueDefinition in project jsonschema2pojo by joelittlejohn.

the class EnumRule method buildEnumDefinitionWithJavaEnumNamesExtension.

protected EnumDefinition buildEnumDefinitionWithJavaEnumNamesExtension(String nodeName, JsonNode parentNode, JsonNode enums, JsonNode javaEnumNames, JType backingType) {
    ArrayList<EnumValueDefinition> enumValues = new ArrayList<>();
    Collection<String> existingConstantNames = new ArrayList<>();
    for (int i = 0; i < enums.size(); i++) {
        JsonNode value = enums.path(i);
        if (!value.isNull()) {
            String constantName = getConstantName(value.asText(), javaEnumNames.path(i).asText());
            constantName = makeUnique(constantName, existingConstantNames);
            existingConstantNames.add(constantName);
            enumValues.add(new EnumValueDefinition(constantName, value.asText(), javaEnumNames));
        }
    }
    return new EnumDefinition(nodeName, parentNode, backingType, enumValues, EnumDefinitionExtensionType.JAVA_ENUM_NAMES);
}
Also used : EnumValueDefinition(org.jsonschema2pojo.model.EnumValueDefinition) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) EnumDefinition(org.jsonschema2pojo.model.EnumDefinition)

Example 4 with EnumValueDefinition

use of org.jsonschema2pojo.model.EnumValueDefinition in project jsonschema2pojo by joelittlejohn.

the class EnumRule method buildEnumDefinitionWithJavaEnumsExtension.

protected EnumDefinition buildEnumDefinitionWithJavaEnumsExtension(String nodeName, JsonNode enumNode, JsonNode enums, JsonNode javaEnums, JType type) {
    ArrayList<EnumValueDefinition> enumValues = new ArrayList<>();
    Collection<String> existingConstantNames = new ArrayList<>();
    for (int i = 0; i < enums.size(); i++) {
        JsonNode value = enums.path(i);
        if (!value.isNull()) {
            JsonNode javaEnumNode = javaEnums.path(i);
            if (javaEnumNode.isMissingNode()) {
                ruleFactory.getLogger().error("javaEnum entry for " + value.asText() + " was not found.");
            }
            String constantName = getConstantName(value.asText(), javaEnumNode.path("name").asText());
            constantName = makeUnique(constantName, existingConstantNames);
            existingConstantNames.add(constantName);
            JsonNode titleNode = javaEnumNode.path("title");
            JsonNode descriptionNode = javaEnumNode.path("description");
            enumValues.add(new EnumValueDefinition(constantName, value.asText(), javaEnumNode, titleNode, descriptionNode));
        }
    }
    return new EnumDefinition(nodeName, enumNode, type, enumValues, EnumDefinitionExtensionType.JAVA_ENUMS);
}
Also used : EnumValueDefinition(org.jsonschema2pojo.model.EnumValueDefinition) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) EnumDefinition(org.jsonschema2pojo.model.EnumDefinition)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)4 EnumValueDefinition (org.jsonschema2pojo.model.EnumValueDefinition)4 ArrayList (java.util.ArrayList)3 EnumDefinition (org.jsonschema2pojo.model.EnumDefinition)3 JEnumConstant (com.sun.codemodel.JEnumConstant)1 JType (com.sun.codemodel.JType)1 Annotator (org.jsonschema2pojo.Annotator)1