Skip to content

Commit

Permalink
Refactored String handling so it can be used within array processing …
Browse files Browse the repository at this point in the history
…to fix issue #43
  • Loading branch information
andrewserff committed Apr 23, 2018
1 parent 640f1c4 commit 152e04f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ target/
logs/
.idea/
*.iml
/nbproject/
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public List<Map<String, Object>> testListGenerator(String config) throws IOExcep
}

public static void main(String... args) {
String config = "config6.json";
String config = "config-array-test.json";
// String config = "config1.json";
try {
ObjectMapper mapper = new ObjectMapper();
JsonGenerator gen = new JsonGenerator();
Expand Down
116 changes: 71 additions & 45 deletions src/main/java/net/acesinc/data/json/generator/RandomJsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,49 +71,15 @@ public List<Map<String, Object>> generateJsonList() throws IOException {
}

private javax.json.stream.JsonGenerator processProperties(javax.json.stream.JsonGenerator gen, Map<String, Object> props, String currentContext) {
// Map<String, Object> outputValues = new LinkedHashMap<>();
for (String propName : props.keySet()) {
Object value = props.get(propName);
if (value == null) {
// outputValues.put(propName, null);
generatedValues.put(currentContext + propName, null);
addValue(gen, propName, null);
} else if (String.class.isAssignableFrom(value.getClass())) {
String type = (String) value;

if (type.startsWith("this.") || type.startsWith("cur.")) {
String refPropName = null;
if (type.startsWith("this.")) {
refPropName = type.substring("this.".length(), type.length());
} else if (type.startsWith("cur.")) {
refPropName = currentContext + type.substring("cur.".length(), type.length());
}
Object refPropValue = generatedValues.get(refPropName);
if (refPropValue != null) {
addValue(gen, propName, refPropValue);
} else {
log.warn("Sorry, unable to reference property [ " + refPropName + " ]. Maybe it hasn't been generated yet?");
}
} else {
try {
TypeHandler th = TypeHandlerFactory.getInstance().getTypeHandler(type, generatedValues, currentContext);

if (th != null) {
Object val = th.getNextRandomValue();
// outputValues.put(propName, val);
generatedValues.put(currentContext + propName, val);
addValue(gen, propName, val);
} else {
// log.debug("Unknown Type: [ " + type + " ] for prop [ " + propName + " ]. Attempting to echo literal value.");
// outputValues.put(propName, type);
generatedValues.put(currentContext + propName, type);
addValue(gen, propName, type);
}
} catch (IllegalArgumentException iae) {
log.warn("Error creating type [ " + type + " ]. Prop [ " + propName + " ] being ignored in output. Reason: " + iae.getMessage());
log.debug("Error creating type [ " + type + " ]. Prop [ " + propName + " ] being ignored in output.", iae);
}
}
handleStringGeneration(type, currentContext, gen, propName);
} else if (Map.class.isAssignableFrom(value.getClass())) {
//nested object
Map<String, Object> nestedProps = (Map<String, Object>) value;
Expand All @@ -138,7 +104,7 @@ private javax.json.stream.JsonGenerator processProperties(javax.json.stream.Json
String newContext = "";
if (propName != null) {
gen.writeStartArray(propName);

if (currentContext.isEmpty()) {
newContext = propName;
} else {
Expand All @@ -150,6 +116,7 @@ private javax.json.stream.JsonGenerator processProperties(javax.json.stream.Json

if (!listOfItems.isEmpty()) {
//Check if this is a special function at the start of the array
boolean wasSpecialCase = false;
if (String.class.isAssignableFrom(listOfItems.get(0).getClass()) && ((String) listOfItems.get(0)).contains("(")) {
//special function in array
String name = (String) listOfItems.get(0);
Expand All @@ -175,16 +142,20 @@ private javax.json.stream.JsonGenerator processProperties(javax.json.stream.Json
for (int i = 0; i < timesToRepeat; i++) {
processList(subList, gen, newContext);
}
wasSpecialCase = true;
break;
}
case "random": { //choose one of the items in the list at random
List<Object> subList = listOfItems.subList(1, listOfItems.size());
Object item = subList.get(new RandomDataGenerator().nextInt(0, subList.size() - 1));
processItem(item, gen, newContext + "[0]");
wasSpecialCase = true;
break;
}
}
} else { //it's not a special function, so just add it
}

if (!wasSpecialCase) { //it's not a special function, so process it like normal
processList(listOfItems, gen, newContext);
}
}
Expand All @@ -199,6 +170,42 @@ private javax.json.stream.JsonGenerator processProperties(javax.json.stream.Json
return gen;
}

protected void handleStringGeneration(String type, String currentContext, JsonGenerator gen, String propName) {
if (type.startsWith("this.") || type.startsWith("cur.")) {
String refPropName = null;
if (type.startsWith("this.")) {
refPropName = type.substring("this.".length(), type.length());
} else if (type.startsWith("cur.")) {
refPropName = currentContext + type.substring("cur.".length(), type.length());
}
Object refPropValue = generatedValues.get(refPropName);
if (refPropValue != null) {
addValue(gen, propName, refPropValue);
} else {
log.warn("Sorry, unable to reference property [ " + refPropName + " ]. Maybe it hasn't been generated yet?");
}
} else {
try {
TypeHandler th = TypeHandlerFactory.getInstance().getTypeHandler(type, generatedValues, currentContext);

if (th != null) {
Object val = th.getNextRandomValue();
// outputValues.put(propName, val);
generatedValues.put(currentContext + propName, val);
addValue(gen, propName, val);
} else {
// log.debug("Unknown Type: [ " + type + " ] for prop [ " + propName + " ]. Attempting to echo literal value.");
// outputValues.put(propName, type);
generatedValues.put(currentContext + propName, type);
addValue(gen, propName, type);
}
} catch (IllegalArgumentException iae) {
log.warn("Error creating type [ " + type + " ]. Prop [ " + propName + " ] being ignored in output. Reason: " + iae.getMessage());
log.debug("Error creating type [ " + type + " ]. Prop [ " + propName + " ] being ignored in output.", iae);
}
}
}

protected void processList(List<Object> listOfItems, JsonGenerator gen, String currentContext) {
for (int i = 0; i < listOfItems.size(); i++) {
Object item = listOfItems.get(i);
Expand All @@ -209,9 +216,8 @@ protected void processList(List<Object> listOfItems, JsonGenerator gen, String c

protected void processItem(Object item, JsonGenerator gen, String currentContext) {
if (String.class.isAssignableFrom(item.getClass())) {
//literal string, just add it
addValue(gen, null, (String) item);
generatedValues.put(currentContext, (String) item);
//process it like normal
handleStringGeneration((String) item, currentContext, gen, null);
} else if (Map.class.isAssignableFrom(item.getClass())) {
Map<String, Object> nestedProps = (Map<String, Object>) item;
gen.writeStartObject();
Expand All @@ -231,15 +237,35 @@ private javax.json.stream.JsonGenerator addValue(javax.json.stream.JsonGenerator
gen.write((String) val);
}
} else if (Boolean.class.isAssignableFrom(val.getClass())) {
gen.write(propName, (Boolean) val);
if (propName != null) {
gen.write(propName, (Boolean) val);
} else {
gen.write((Boolean) val);
}
} else if (Long.class.isAssignableFrom(val.getClass())) {
gen.write(propName, (Long) val);
if (propName != null) {
gen.write(propName, (Long) val);
} else {
gen.write((Long) val);
}
} else if (Integer.class.isAssignableFrom(val.getClass())) {
gen.write(propName, (Integer) val);
if (propName != null) {
gen.write(propName, (Integer) val);
} else {
gen.write((Integer) val);
}
} else if (Double.class.isAssignableFrom(val.getClass())) {
gen.write(propName, (Double) val);
if (propName != null) {
gen.write(propName, (Double) val);
} else {
gen.write((Double) val);
}
} else if (Date.class.isAssignableFrom(val.getClass())) {
gen.write(propName, iso8601DF.format((Date) val));
if (propName != null) {
gen.write(propName, iso8601DF.format((Date) val));
} else {
gen.write(iso8601DF.format((Date) val));
}
}
return gen;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/config-array-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"timestamp": "nowTimestamp()",
"array-that-worked-with-issue43": [{"one":"integer()"}, {"two":"double(-90.0, 90.0)"}],
"array-that-didnt-work-with-issue43": ["integer()", "double(-90.0, 90.0)", "boolean()","date(\"2015/03/01T00:00:00\")", "long()"],
"location": {
"lat": "double(-90.0, 90.0)",
"lon": "double(-180.0, 180.0)"
}
}
6 changes: 3 additions & 3 deletions src/main/resources/config1.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
},
"dates": {
"rand-date": "date()",
"min-date": "date(\"2015/03/01\")",
"range-date": "date(\"2015/03/01\",\"2015/03/30\")",
"min-date": "date(\"2015/03/01T00:00:00\")",
"range-date": "date(\"2015/03/01T00:00:00\",\"2015/03/30T00:00:00\")",
"now": "now()",
"nowTimestamp": "nowTimestamp()",
"5days-ago": "now(-5_d)",
"timestamp": "timestamp(\"2015/03/01\",\"2015/03/30\")"
"timestamp": "timestamp(\"2015/03/01T00:00:00\",\"2015/03/30T00:00:00\")"
},
"literals": {
"literal-bool": true,
Expand Down

0 comments on commit 152e04f

Please sign in to comment.