mirror of
https://gitee.com/BDWare/MockJava
synced 2025-06-28 04:29:07 +00:00
fix(mockjava): fix MockSchemaParser.visitPrimitive
refactor(mockjava): extract the same .visitObject to JsonVisitor from MockSchemaParser and MockGenerator style(mockjava)
This commit is contained in:
parent
14d5a35d25
commit
39b53a672a
@ -8,13 +8,14 @@ import java.util.Set;
|
|||||||
|
|
||||||
public class JsonUtil {
|
public class JsonUtil {
|
||||||
private static Set<Object> recorded = null;
|
private static Set<Object> recorded = null;
|
||||||
|
|
||||||
public static JsonElement toJson(jdk.nashorn.api.scripting.ScriptObjectMirror res) {
|
public static JsonElement toJson(jdk.nashorn.api.scripting.ScriptObjectMirror res) {
|
||||||
recorded = new HashSet<>();
|
recorded = new HashSet<>();
|
||||||
JsonElement jsonElement = copyInternal(res);
|
JsonElement jsonElement = copyInternal(res);
|
||||||
recorded.clear();
|
recorded.clear();
|
||||||
;
|
|
||||||
return jsonElement;
|
return jsonElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JsonElement copyInternal(Object obj) {
|
private static JsonElement copyInternal(Object obj) {
|
||||||
if (recorded.contains(obj)) return JsonNull.INSTANCE;
|
if (recorded.contains(obj)) return JsonNull.INSTANCE;
|
||||||
if (obj == null) return JsonNull.INSTANCE;
|
if (obj == null) return JsonNull.INSTANCE;
|
||||||
|
@ -6,8 +6,16 @@ import com.google.gson.JsonObject;
|
|||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
|
|
||||||
public abstract class JsonVisitor {
|
public abstract class JsonVisitor {
|
||||||
|
JsonElement result;
|
||||||
|
|
||||||
|
public JsonElement get() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public JsonVisitor visit(JsonElement ele) {
|
public JsonVisitor visit(JsonElement ele) {
|
||||||
if (ele == null) return null;
|
if (null == ele) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (ele.isJsonArray()) {
|
if (ele.isJsonArray()) {
|
||||||
return visitJsonArray((JsonArray) ele);
|
return visitJsonArray((JsonArray) ele);
|
||||||
} else if (ele.isJsonObject()) {
|
} else if (ele.isJsonObject()) {
|
||||||
@ -18,7 +26,20 @@ public abstract class JsonVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract JsonVisitor visitObject(JsonObject asJsonObject);
|
public JsonVisitor visitObject(JsonObject jsonObject) {
|
||||||
|
JsonObject jo = new JsonObject();
|
||||||
|
for (String key : jsonObject.keySet()) {
|
||||||
|
JsonElement origin = jsonObject.get(key);
|
||||||
|
visit(origin);
|
||||||
|
JsonElement je = get();
|
||||||
|
if (origin.isJsonArray()) {
|
||||||
|
key += "|" + Math.min(((JsonArray) origin).size(), 20);
|
||||||
|
}
|
||||||
|
jo.add(key, je);
|
||||||
|
}
|
||||||
|
result = jo;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public abstract JsonVisitor visitJsonArray(JsonArray ele);
|
public abstract JsonVisitor visitJsonArray(JsonArray ele);
|
||||||
|
|
||||||
|
@ -2,33 +2,11 @@ package org.bdware.mockjava;
|
|||||||
|
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
import org.bdware.mockjava.generator.IntegerGenerator;
|
import org.bdware.mockjava.generator.IntegerGenerator;
|
||||||
|
|
||||||
public class MockGenerator extends JsonVisitor {
|
public class MockGenerator extends JsonVisitor {
|
||||||
public static MockGenerator instance = new MockGenerator();
|
public static MockGenerator instance = new MockGenerator();
|
||||||
JsonElement result;
|
|
||||||
|
|
||||||
public JsonElement get() {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public JsonVisitor visitObject(JsonObject jsonObject) {
|
|
||||||
JsonObject jo = new JsonObject();
|
|
||||||
for (String key : jsonObject.keySet()) {
|
|
||||||
JsonElement origin = jsonObject.get(key);
|
|
||||||
visit(origin);
|
|
||||||
JsonElement je = get();
|
|
||||||
if (origin.isJsonArray()) {
|
|
||||||
key += "|" + Math.min(((JsonArray) origin).size(), 20);
|
|
||||||
}
|
|
||||||
jo.add(key, je);
|
|
||||||
}
|
|
||||||
result = jo;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonVisitor visitJsonArray(JsonArray array) {
|
public JsonVisitor visitJsonArray(JsonArray array) {
|
||||||
@ -43,8 +21,9 @@ public class MockGenerator extends JsonVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonVisitor visitPrimitive(JsonPrimitive primitive) {
|
public JsonVisitor visitPrimitive(JsonPrimitive primitive) {
|
||||||
if (!primitive.isString()) result = primitive.deepCopy();
|
if (!primitive.isString()) {
|
||||||
|
result = primitive.deepCopy();
|
||||||
|
}
|
||||||
ValueGenerator generator = lookupGenerator(primitive.getAsString());
|
ValueGenerator generator = lookupGenerator(primitive.getAsString());
|
||||||
result = generator.generate(null, primitive);
|
result = generator.generate(null, primitive);
|
||||||
return this;
|
return this;
|
||||||
|
@ -1,37 +1,14 @@
|
|||||||
package org.bdware.mockjava;
|
package org.bdware.mockjava;
|
||||||
|
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
|
|
||||||
public class MockSchemaParser extends JsonVisitor {
|
public class MockSchemaParser extends JsonVisitor {
|
||||||
public static MockSchemaParser instance = new MockSchemaParser();
|
public static MockSchemaParser instance = new MockSchemaParser();
|
||||||
JsonElement result;
|
|
||||||
|
|
||||||
public JsonElement get() {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public JsonVisitor visitObject(JsonObject jsonObject) {
|
|
||||||
JsonObject jo = new JsonObject();
|
|
||||||
for (String key : jsonObject.keySet()) {
|
|
||||||
JsonElement origin = jsonObject.get(key);
|
|
||||||
visit(origin);
|
|
||||||
JsonElement je = get();
|
|
||||||
if (origin.isJsonArray()) {
|
|
||||||
key += "|" + Math.min(((JsonArray) origin).size(), 20);
|
|
||||||
}
|
|
||||||
jo.add(key, je);
|
|
||||||
}
|
|
||||||
result = jo;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonVisitor visitJsonArray(JsonArray array) {
|
public JsonVisitor visitJsonArray(JsonArray array) {
|
||||||
JsonArray ret = new JsonArray();
|
JsonArray ret = new JsonArray();
|
||||||
if (array.size() > 0) {
|
if (array.size() > 0) {
|
||||||
visit(array.get(0));
|
visit(array.get(0));
|
||||||
ret.add(result);
|
ret.add(result);
|
||||||
@ -46,8 +23,9 @@ public class MockSchemaParser extends JsonVisitor {
|
|||||||
result = new JsonPrimitive("@boolean");
|
result = new JsonPrimitive("@boolean");
|
||||||
} else if (primitive.isNumber()) {
|
} else if (primitive.isNumber()) {
|
||||||
result = new JsonPrimitive("@integer");
|
result = new JsonPrimitive("@integer");
|
||||||
|
} else {
|
||||||
|
result = new JsonPrimitive("@string");
|
||||||
}
|
}
|
||||||
result = new JsonPrimitive("@string");
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -19,8 +19,8 @@ public class MockUtil {
|
|||||||
MOCK_JS_ENGINE = new ScriptEngineManager().getEngineByName("JavaScript");
|
MOCK_JS_ENGINE = new ScriptEngineManager().getEngineByName("JavaScript");
|
||||||
// System.out.println(MOCK_JS_ENGINE);
|
// System.out.println(MOCK_JS_ENGINE);
|
||||||
try (InputStream mockJs =
|
try (InputStream mockJs =
|
||||||
MockUtil.class.getClassLoader().getResourceAsStream(MOCK_JS_PATH);
|
MockUtil.class.getClassLoader().getResourceAsStream(MOCK_JS_PATH);
|
||||||
InputStreamReader reader = new InputStreamReader(mockJs); ) {
|
InputStreamReader reader = new InputStreamReader(mockJs)) {
|
||||||
MOCK_JS_ENGINE.eval(reader);
|
MOCK_JS_ENGINE.eval(reader);
|
||||||
} catch (ScriptException | IOException e) {
|
} catch (ScriptException | IOException e) {
|
||||||
// log.error("执行MockJs错误", e);
|
// log.error("执行MockJs错误", e);
|
||||||
@ -31,7 +31,7 @@ public class MockUtil {
|
|||||||
|
|
||||||
private static boolean isJson(String str) {
|
private static boolean isJson(String str) {
|
||||||
try {
|
try {
|
||||||
JsonObject jsonStr = JsonParser.parseString(str).getAsJsonObject();
|
JsonParser.parseString(str).getAsJsonObject();
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return false;
|
return false;
|
||||||
@ -39,7 +39,7 @@ public class MockUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Object mock(String template) {
|
public static Object mock(String template) {
|
||||||
Object result = (ScriptObjectMirror) ScriptObjectMirror.wrap(null, Context.getGlobal());
|
Object result = ScriptObjectMirror.wrap(null, Context.getGlobal());
|
||||||
if (template.length() > 0) {
|
if (template.length() > 0) {
|
||||||
try {
|
try {
|
||||||
//
|
//
|
||||||
|
@ -3,5 +3,5 @@ package org.bdware.mockjava;
|
|||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
|
|
||||||
public abstract class ValueGenerator {
|
public abstract class ValueGenerator {
|
||||||
public abstract JsonElement generate(String preField,JsonElement previous);
|
public abstract JsonElement generate(String preField, JsonElement previous);
|
||||||
}
|
}
|
||||||
|
@ -9,13 +9,13 @@ import java.util.Random;
|
|||||||
|
|
||||||
@Generator(tag = "@integer")
|
@Generator(tag = "@integer")
|
||||||
public class IntegerGenerator extends ValueGenerator {
|
public class IntegerGenerator extends ValueGenerator {
|
||||||
|
static Random secureRandom = new SecureRandom();
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
public static IntegerGenerator create(String[] args) {
|
public static IntegerGenerator create(String[] args) {
|
||||||
return new IntegerGenerator();
|
return new IntegerGenerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
static Random secureRandom = new SecureRandom();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonElement generate(String preField, JsonElement previous) {
|
public JsonElement generate(String preField, JsonElement previous) {
|
||||||
return new JsonPrimitive(secureRandom.nextInt());
|
return new JsonPrimitive(secureRandom.nextInt());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user