Skip to content

Commit

Permalink
Merge pull request #177 from networknt/feature/#176-enum-class
Browse files Browse the repository at this point in the history
#176 support seperate enum classes
  • Loading branch information
stevehu authored Sep 6, 2018
2 parents 613bc39 + eb13679 commit 0086e6c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
String key = entry.getKey();
Map<String, Any> value = entry.getValue().asMap();
String type = null;
String enums = null;
boolean isEnum = false;
boolean isEnumClass = false;
Map<String, Any> properties = null;
List<Any> required = null;

Expand All @@ -177,6 +179,11 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
type = entrySchema.getValue().toString();
if("enum".equals(type)) isEnum = true;
}
if("enum".equals(entrySchema.getKey())) {
isEnumClass = true;
enums = entrySchema.getValue().asList().toString();
enums = enums.substring(enums.indexOf("[") + 1, enums.indexOf("]"));
}
if("properties".equals(entrySchema.getKey())) {
properties = entrySchema.getValue().asMap();
// transform properties
Expand Down Expand Up @@ -304,6 +311,10 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
if(!overwriteModel && checkExist(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java")) {
continue;
}
if (isEnumClass) {
transfer(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java", templates.rest.enumClass.template(modelPackage, modelFileName, enums));
continue;
}
transfer(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java", templates.rest.pojo.template(modelPackage, modelFileName, classVarName, props));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
@import com.jsoniter.any.Any
@import java.util.Map
@import java.util.List
@args (Map<String, Any> prop)
@with (v = prop.get("nameWithEnum") + ".values()", value = prop.get("value").asList()) {
public enum @prop.get("nameWithEnum") {
@for((i, item) : value) {
@with(u = item.toString().toUpperCase().replaceAll("-", "_")) {@if (i.index() < value.size() - 1) {@u ("@item"),}@if(i.index() == value.size() - 1) {@u ("@item");}}
}
@args (String modelPackage, String className, String enums)
package @modelPackage;
import java.util.Enumeration;

private final @prop.get("type") value;
public enum @className {
@enums;

@prop.get("nameWithEnum")(@prop.get("type") value) {
this.value = value;
}

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

public static @prop.get("nameWithEnum") fromValue(String text) {
for (@prop.get("nameWithEnum") b : @v) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}

private @prop.get("nameWithEnum") @prop.get("name");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@import com.jsoniter.any.Any
@import java.util.Map
@import java.util.List
@args (Map<String, Any> prop)
@with (v = prop.get("nameWithEnum") + ".values()", value = prop.get("value").asList()) {
public enum @prop.get("nameWithEnum") {
@for((i, item) : value) {
@with(u = item.toString().toUpperCase().replaceAll("-", "_")) {@if (i.index() < value.size() - 1) {@u ("@item"),}@if(i.index() == value.size() - 1) {@u ("@item");}}
}

private final @prop.get("type") value;

@prop.get("nameWithEnum")(@prop.get("type") value) {
this.value = value;
}

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

public static @prop.get("nameWithEnum") fromValue(String text) {
for (@prop.get("nameWithEnum") b : @v) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}

private @prop.get("nameWithEnum") @prop.get("name");

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
public class @className {

@for (prop: props) {
@if(prop.get("isEnum").toBoolean()) {@templates.rest.enumClass.template(prop)} else {private @prop.get("type") @prop.get("name");}
@if(prop.get("isEnum").toBoolean()) {@templates.rest.enumInline.template(prop)} else {private @prop.get("type") @prop.get("name");}
}

public @className () {
Expand Down

0 comments on commit 0086e6c

Please sign in to comment.