Skip to content

Commit 67f832a

Browse files
committed
Introduce Json object
1 parent bd5a42b commit 67f832a

File tree

14 files changed

+316
-103
lines changed

14 files changed

+316
-103
lines changed

integration-tests/pom.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,22 @@
190190

191191
<forcedTypes>
192192
<forcedType>
193-
<userType>java.lang.String</userType>
194-
<binding>com.github.t9t.jooq.json.JsonStringBinding</binding>
193+
<userType>com.github.t9t.jooq.json.Json</userType>
194+
<binding>com.github.t9t.jooq.json.JsonBinding</binding>
195+
<expression>.*\.json_test\..*</expression>
195196
<types>json</types>
196197
</forcedType>
198+
<forcedType>
199+
<userType>com.github.t9t.jooq.json.Json</userType>
200+
<binding>com.github.t9t.jooq.json.JsonBinding</binding>
201+
<expression>.*\.json_test\..*</expression>
202+
<types>jsonb</types>
203+
</forcedType>
197204
<forcedType>
198205
<userType>java.lang.String</userType>
199206
<binding>com.github.t9t.jooq.json.JsonbStringBinding</binding>
200-
<types>jsonb</types>
207+
<expression>.*\.json_str_test\..*</expression>
208+
<types>json|jsonb</types>
201209
</forcedType>
202210
</forcedTypes>
203211
</database>

integration-tests/src/test/java/com/github/t9t/jooq/json/AbstractJsonDSLTest.java

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public abstract class AbstractJsonDSLTest {
3636
@Parameterized.Parameter(2)
3737
public Object expected;
3838
@Parameterized.Parameter(3)
39-
public Field<String> fieldToSelect;
39+
public Field<?> fieldToSelect;
4040

41-
static List<Object[]> generateParams(String baseName, BiFunction<String, Field<String>, List<Params>> testParamFunc) {
41+
static List<Object[]> generateParams(String baseName, BiFunction<String, Field<Json>, List<Params>> testParamFunc) {
4242
List<Object[]> params = new ArrayList<>();
4343
for (String type : Arrays.asList("json", "jsonb")) {
44-
Field<String> f = "json".equals(type) ? JSON_TEST.DATA : JSON_TEST.DATAB;
44+
Field<Json> f = "json".equals(type) ? JSON_TEST.DATA : JSON_TEST.DATAB;
4545
List<Params> paramList = testParamFunc.apply(type, f);
4646
for (Params p : paramList) {
4747
String name = String.format("%s_%s_%s", baseName, p.name, type);
@@ -51,42 +51,62 @@ static List<Object[]> generateParams(String baseName, BiFunction<String, Field<S
5151
return params;
5252
}
5353

54-
static Params test(String name, Object expected, Field<String> field) {
55-
return params(name, genericRow, expected, field);
54+
static Params test(String name, String expected, Field<Json> fieldToSelect) {
55+
return params(name, genericRow, Json.ofNullable(expected), fieldToSelect);
5656
}
5757

58-
static Params arrayTest(String name, String expected, Field<String> field) {
59-
return params(name, arrayRow, expected, field);
58+
static Params stringTest(String name, String expected, Field<String> fieldToSelect) {
59+
return params(name, genericRow, expected, fieldToSelect);
6060
}
6161

62-
private static Params params(String name, String rowName, Object expected, Field<String> field) {
63-
return new Params(name, rowName, expected, field);
62+
static Params test(String name, JsonNode expected, Field<?> fieldToSelect) {
63+
return params(name, genericRow, expected, fieldToSelect);
64+
}
65+
66+
static Params testNull(String name, Field<?> fieldToSelect) {
67+
return params(name, genericRow, null, fieldToSelect);
68+
}
69+
70+
static Params arrayTest(String name, String expected, Field<Json> fieldToSelect) {
71+
return params(name, arrayRow, Json.ofNullable(expected), fieldToSelect);
72+
}
73+
74+
static Params arrayStringTest(String name, String expected, Field<String> fieldToSelect) {
75+
return params(name, arrayRow, expected, fieldToSelect);
76+
}
77+
78+
private static Params params(String name, String rowName, Object expected, Field<?> fieldToSelect) {
79+
return new Params(name, rowName, expected, fieldToSelect);
6480
}
6581

6682
@Before
6783
public void setUp() {
6884
dsl.deleteFrom(JSON_TEST).execute();
6985

7086
String template = "{\"obj\": {\"i\": 5521, \"b\": true}, \"arr\": [{\"d\": 4408}, 10, true, \"s\"], \"num\": 1337, \"str\": \"Hello, %s world!\", \"n\": null}";
71-
String artmpl = "[{\"d\": 4408}, 10, true, \"%s array\"]";
87+
String arrayTemplate = "[{\"d\": 4408}, 10, true, \"%s array\"]";
7288
assertEquals(2, dsl.insertInto(JSON_TEST)
7389
.columns(JSON_TEST.NAME, JSON_TEST.DATA, JSON_TEST.DATAB)
74-
.values(genericRow, String.format(template, "json"), String.format(template, "jsonb"))
75-
.values(arrayRow, String.format(artmpl, "json"), String.format(artmpl, "jsonb"))
90+
.values(genericRow, Json.of(String.format(template, "json")), Json.of(String.format(template, "jsonb")))
91+
.values(arrayRow, Json.of(String.format(arrayTemplate, "json")), Json.of(String.format(arrayTemplate, "jsonb")))
7692
.execute());
7793
assertEquals(2, dsl.fetchCount(JSON_TEST));
7894
}
7995

8096
@Test
8197
public void test() {
82-
String data = select(rowName, fieldToSelect);
98+
Object data = select(rowName, fieldToSelect);
8399

84100
if (expected == null) {
85101
assertNull(data);
86-
} else if (expected instanceof String) {
102+
} else if (expected instanceof String || expected instanceof Json) {
87103
assertEquals(expected, data);
88104
} else if (expected instanceof JsonNode) {
89-
assertEquals(expected, toNode(data));
105+
if (data instanceof Json) {
106+
assertEquals(expected, toNode(((Json) data).getValue()));
107+
} else {
108+
assertEquals(expected, toNode((String) data));
109+
}
90110
} else {
91111
throw new IllegalArgumentException("Cannot assert object: " + expected.getClass());
92112
}
@@ -100,7 +120,7 @@ static JsonNode toNode(String s) {
100120
}
101121
}
102122

103-
private static String select(String rowName, Field<String> field) {
123+
private Object select(String rowName, Field<?> field) {
104124
return dsl.select(field)
105125
.from(JSON_TEST)
106126
.where(JSON_TEST.NAME.eq(rowName))
@@ -111,9 +131,9 @@ static class Params {
111131
final String name;
112132
final String dataSet;
113133
final Object expected;
114-
final Field<String> fieldToSelect;
134+
final Field<?> fieldToSelect;
115135

116-
public Params(String name, String dataSet, Object expected, Field<String> fieldToSelect) {
136+
public Params(String name, String dataSet, Object expected, Field<?> fieldToSelect) {
117137
this.name = name;
118138
this.dataSet = dataSet;
119139
this.expected = expected;

integration-tests/src/test/java/com/github/t9t/jooq/json/JsonDSLArrayElementTextIT.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ public class JsonDSLArrayElementTextIT extends AbstractJsonDSLTest {
99
@Parameterized.Parameters(name = "{1}_{0}")
1010
public static List<Object[]> params() {
1111
return generateParams("arrayElementText", (type, f) -> Arrays.asList(
12-
arrayTest("getFirstObject", "{\"d\": 4408}", JsonDSL.arrayElementText(f, 0)),
13-
arrayTest("getString", type + " array", JsonDSL.arrayElementText(f, 3)),
14-
arrayTest("outOfBounds", null, JsonDSL.arrayElementText(f, 100)),
15-
arrayTest("negativeIndex", "true", JsonDSL.arrayElementText(f, -2)),
16-
test("onObject", null, JsonDSL.arrayElementText(f, 100))
12+
arrayStringTest("getFirstObject", "{\"d\": 4408}", JsonDSL.arrayElementText(f, 0)),
13+
arrayStringTest("getString", type + " array", JsonDSL.arrayElementText(f, 3)),
14+
arrayStringTest("outOfBounds", null, JsonDSL.arrayElementText(f, 100)),
15+
arrayStringTest("negativeIndex", "true", JsonDSL.arrayElementText(f, -2)),
16+
testNull("onObject", JsonDSL.arrayElementText(f, 100))
1717
));
1818
}
1919

integration-tests/src/test/java/com/github/t9t/jooq/json/JsonDSLFieldByKeyIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static List<Object[]> params() {
1212
test("string", "\"Hello, " + type + " world!\"", JsonDSL.fieldByKey(f, "str")),
1313
test("twoLevels", "5521", JsonDSL.fieldByKey(JsonDSL.fieldByKey(f, "obj"), "i")),
1414
test("nullField", "null", JsonDSL.fieldByKey(f, "n")),
15-
test("notExistingField", null, JsonDSL.fieldByKey(f, "notExisting"))
15+
testNull("notExistingField", JsonDSL.fieldByKey(f, "notExisting"))
1616
));
1717
}
1818
}

integration-tests/src/test/java/com/github/t9t/jooq/json/JsonDSLFieldByKeyTextIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class JsonDSLFieldByKeyTextIT extends AbstractJsonDSLTest {
99
@Parameterized.Parameters(name = "{1}_{0}")
1010
public static List<Object[]> params() {
1111
return generateParams("fieldByKeyText", (type, f) -> Arrays.asList(
12-
test("string", "Hello, " + type + " world!", JsonDSL.fieldByKeyText(f, "str")),
13-
test("twoLevels", "5521", JsonDSL.fieldByKeyText(JsonDSL.fieldByKey(f, "obj"), "i")),
14-
test("nullField", null, JsonDSL.fieldByKeyText(f, "n")),
15-
test("notExistingField", null, JsonDSL.fieldByKeyText(f, "notExisting"))
12+
stringTest("string", "Hello, " + type + " world!", JsonDSL.fieldByKeyText(f, "str")),
13+
stringTest("twoLevels", "5521", JsonDSL.fieldByKeyText(JsonDSL.fieldByKey(f, "obj"), "i")),
14+
testNull("nullField", JsonDSL.fieldByKeyText(f, "n")),
15+
testNull("notExistingField", JsonDSL.fieldByKeyText(f, "notExisting"))
1616
));
1717
}
1818

integration-tests/src/test/java/com/github/t9t/jooq/json/JsonDSLObjectAtPathIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static List<Object[]> params() {
1313
test("obj", toNode("{\"i\": 5521, \"b\": true}"), JsonDSL.objectAtPath(f, "obj")),
1414
test("deepVarargs", "4408", JsonDSL.objectAtPath(f, "arr", "0", "d")),
1515
test("deepCollection", "4408", JsonDSL.objectAtPath(f, Arrays.asList("arr", "0", "d"))),
16-
test("notExistingPath", null, JsonDSL.objectAtPath(f, "not", "existing", "path"))
16+
testNull("notExistingPath", JsonDSL.objectAtPath(f, "not", "existing", "path"))
1717
));
1818
}
1919
}

integration-tests/src/test/java/com/github/t9t/jooq/json/JsonDSLObjectAtPathTextIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ public class JsonDSLObjectAtPathTextIT extends AbstractJsonDSLTest {
99
@Parameterized.Parameters(name = "{1}_{0}")
1010
public static List<Object[]> params() {
1111
return generateParams("objectAtPath", (type, f) -> Arrays.asList(
12-
test("oneLevel", "Hello, " + type + " world!", JsonDSL.objectAtPathText(f, "str")),
12+
stringTest("oneLevel", "Hello, " + type + " world!", JsonDSL.objectAtPathText(f, "str")),
1313
test("obj", toNode("{\"i\": 5521, \"b\": true}"), JsonDSL.objectAtPathText(f, "obj")),
14-
test("deepVarargs", "4408", JsonDSL.objectAtPathText(f, "arr", "0", "d")),
15-
test("deepCollection", "4408", JsonDSL.objectAtPathText(f, Arrays.asList("arr", "0", "d"))),
16-
test("notExistingPath", null, JsonDSL.objectAtPathText(f, "not", "existing", "path"))
14+
stringTest("deepVarargs", "4408", JsonDSL.objectAtPathText(f, "arr", "0", "d")),
15+
stringTest("deepCollection", "4408", JsonDSL.objectAtPathText(f, Arrays.asList("arr", "0", "d"))),
16+
testNull("notExistingPath", JsonDSL.objectAtPathText(f, "not", "existing", "path"))
1717
));
1818
}
1919
}

0 commit comments

Comments
 (0)