Skip to content

Commit

Permalink
feat: add generation of enum classes for model (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenischev authored May 21, 2020
1 parent 5b3d055 commit c9eddf9
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions template/src/main/java/com/asyncapi/model/$$schema$$.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import javax.validation.constraints.*;
import javax.validation.Valid;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Objects;

{% if schema.description() or schema.examples() %}/**{% for line in schema.description() | splitByLines %}
* {{ line | safe}}{% endfor %}{% if schema.examples() %}
Expand All @@ -18,6 +23,43 @@ public class {{schemaName | camelCase | upperFirst}} {
{%- else %}
private @Valid {{prop.items().type() | toJavaType}}[] {{propName | camelCase}}Array;
{%- endif %}
{%- elif prop.enum() and (prop.type() === 'string' or prop.type() === 'integer') %}
public enum {{propName | camelCase | upperFirst}}Enum {
{% for e in prop.enum() %}
{%- if prop.type() === 'string'%}
{{e | upper | replace(' ', '_')}}(String.valueOf("{{e}}")){% if not loop.last %},{% else %};{% endif %}
{%- else %}
NUMBER_{{e}}({{e}}){% if not loop.last %},{% else %};{% endif %}
{%- endif %}
{% endfor %}
private {% if prop.type() === 'string'%}String{% else %}Integer{% endif %} value;

{{propName | camelCase | upperFirst}}Enum ({% if prop.type() === 'string'%}String{% else %}Integer{% endif %} v) {
value = v;
}

public {% if prop.type() === 'string'%}String{% else %}Integer{% endif %} value() {
return value;
}

@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static {{propName | camelCase | upperFirst}}Enum fromValue({% if prop.type() === 'string'%}String{% else %}Integer{% endif %} value) {
for ( {{propName | camelCase | upperFirst}}Enum b : {{propName | camelCase | upperFirst}}Enum.values()) {
if (Objects.equals(b.value, value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}

private @Valid {{propName | camelCase | upperFirst}}Enum {{propName | camelCase}};
{%- else %}
{%- if prop.format() %}
private @Valid {{prop.format() | toJavaType}} {{propName | camelCase}};
Expand All @@ -39,6 +81,8 @@ public class {{schemaName | camelCase | upperFirst}} {
{%- else %}
{%- set propType = prop.items().type() | toJavaType + '[]' %}
{%- endif %}
{%- elif prop.enum() and (prop.type() === 'string' or prop.type() === 'integer') %}
{%- set propType = (propName | camelCase | upperFirst) + 'Enum' %}
{%- else %}
{%- if prop.format() %}
{%- set propType = prop.format() | toJavaType %}
Expand Down

0 comments on commit c9eddf9

Please sign in to comment.