Skip to content

Commit 6e3b9ab

Browse files
committed
feat(kotlin): provide operators through extension functions
To use a more fluent syntax, we provide some extension functions in the kotlin context. All the code is just a bridge with real java implementation. Closes #11
1 parent 8ab68c5 commit 6e3b9ab

File tree

7 files changed

+843
-0
lines changed

7 files changed

+843
-0
lines changed

integration-tests/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<db.url>jdbc:postgresql://127.0.0.1:${pg.port}/${pg.dbname}</db.url>
1919

2020
<test.jooq-codegen.directory>${project.build.directory}/generated-sources/jooq</test.jooq-codegen.directory>
21+
22+
<kotlin.version>1.4.20</kotlin.version>
2123
</properties>
2224

2325
<dependencies>
@@ -37,6 +39,12 @@
3739
<scope>test</scope>
3840
</dependency>
3941

42+
<dependency>
43+
<groupId>org.jetbrains.kotlin</groupId>
44+
<artifactId>kotlin-stdlib</artifactId>
45+
<optional>true</optional>
46+
</dependency>
47+
4048
<dependency>
4149
<groupId>junit</groupId>
4250
<artifactId>junit</artifactId>
@@ -188,6 +196,29 @@
188196
</dependencies>
189197
</plugin>
190198

199+
<plugin>
200+
<groupId>org.jetbrains.kotlin</groupId>
201+
<artifactId>kotlin-maven-plugin</artifactId>
202+
<version>${kotlin.version}</version>
203+
<configuration>
204+
<jvmTarget>1.8</jvmTarget>
205+
</configuration>
206+
<executions>
207+
<execution>
208+
<id>test-compile</id>
209+
<goals>
210+
<goal>test-compile</goal>
211+
</goals>
212+
<configuration>
213+
<sourceDirs>
214+
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
215+
</sourceDirs>
216+
</configuration>
217+
</execution>
218+
</executions>
219+
</plugin>
220+
221+
191222
<plugin>
192223
<groupId>org.codehaus.mojo</groupId>
193224
<artifactId>build-helper-maven-plugin</artifactId>
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package com.github.t9t.jooq.json.json
2+
3+
import com.github.t9t.jooq.json.JsonDSL
4+
import org.jooq.Field
5+
import org.jooq.JSON
6+
import org.jooq.impl.DSL
7+
import org.junit.Assert.assertEquals
8+
import org.junit.Test
9+
10+
/**
11+
* Created by kevin on 20/12/2020
12+
*/
13+
class JsonDSLTestIT {
14+
15+
private val jsonField: Field<JSON> = DSL.field("foo.bar", JSON::class.java)
16+
17+
@Test
18+
fun `should provide extension function to create field from JSON`() {
19+
/* Given */
20+
val json = JSON.valueOf("{}")
21+
/* When */
22+
val jsonField = JsonDSL.field(json)
23+
val jsonFieldExt = json.toField()
24+
/* Then */
25+
assertEquals(jsonField, jsonFieldExt)
26+
}
27+
28+
@Test
29+
fun `should provide extension function for arrayElement`() {
30+
/* Given */
31+
val index = 1
32+
/* When */
33+
val arrayElementField = JsonDSL.arrayElement(jsonField, index)
34+
val arrayElementFieldExt = jsonField.arrayElement(index)
35+
/* Then */
36+
assertEquals(arrayElementField, arrayElementFieldExt)
37+
}
38+
39+
@Test
40+
fun `should provide extension function for arrayElementText`() {
41+
/* Given */
42+
val index = 1
43+
/* When */
44+
val arrayElementTextField = JsonDSL.arrayElementText(jsonField, index)
45+
val arrayElementTextFieldExt = jsonField.arrayElementText(index)
46+
/* Then */
47+
assertEquals(arrayElementTextField, arrayElementTextFieldExt)
48+
}
49+
50+
@Test
51+
fun `should provide extension function for fieldByKey`() {
52+
/* Given */
53+
val key = "key"
54+
/* When */
55+
val fieldByKeyField = JsonDSL.fieldByKey(jsonField, key)
56+
val fieldByKeyFieldExt = jsonField.fieldByKey(key)
57+
/* Then */
58+
assertEquals(fieldByKeyField, fieldByKeyFieldExt)
59+
}
60+
61+
@Test
62+
fun `should provide extension function for fieldByKeyText`() {
63+
/* Given */
64+
val key = "key"
65+
/* When */
66+
val fieldByKeyTextField = JsonDSL.fieldByKeyText(jsonField, key)
67+
val fieldByKeyTextFieldExt = jsonField.fieldByKeyText(key)
68+
/* Then */
69+
assertEquals(fieldByKeyTextField, fieldByKeyTextFieldExt)
70+
}
71+
72+
@Test
73+
fun `should provide extension function for objectAtPath with varargs`() {
74+
/* Given */
75+
val path = arrayOf("path", "to", "key")
76+
/* When */
77+
val objectAtPathField = JsonDSL.objectAtPath(jsonField, *path)
78+
val objectAtPathFieldExt = jsonField.objectAtPath(*path)
79+
/* Then */
80+
assertEquals(objectAtPathField, objectAtPathFieldExt)
81+
}
82+
83+
@Test
84+
fun `should provide extension function for objectAtPath with collection`() {
85+
/* Given */
86+
val path = arrayOf("path", "to", "key")
87+
/* When */
88+
val objectAtPathField = JsonDSL.objectAtPath(jsonField, path.toList())
89+
val objectAtPathFieldExt = jsonField.objectAtPath(path.toList())
90+
/* Then */
91+
assertEquals(objectAtPathField, objectAtPathFieldExt)
92+
}
93+
94+
@Test
95+
fun `should provide extension function for objectAtPathText with varargs`() {
96+
/* Given */
97+
val path = arrayOf("path", "to", "key")
98+
/* When */
99+
val objectAtPathTextField = JsonDSL.objectAtPathText(jsonField, *path)
100+
val objectAtPathTextFieldExt = jsonField.objectAtPathText(*path)
101+
/* Then */
102+
assertEquals(objectAtPathTextField, objectAtPathTextFieldExt)
103+
}
104+
105+
@Test
106+
fun `should provide extension function for objectAtPathText with collection`() {
107+
/* Given */
108+
val path = arrayOf("path", "to", "key")
109+
/* When */
110+
val objectAtPathTextField = JsonDSL.objectAtPathText(jsonField, path.toList())
111+
val objectAtPathTextFieldExt = jsonField.objectAtPathText(path.toList())
112+
/* Then */
113+
assertEquals(objectAtPathTextField, objectAtPathTextFieldExt)
114+
}
115+
116+
@Test
117+
fun `should provide function for arrayLength`() {
118+
/* Given */
119+
/* When */
120+
val arrayLengthField = JsonDSL.arrayLength(jsonField)
121+
val arrayLengthFieldExt = arrayLength(jsonField)
122+
/* Then */
123+
assertEquals(arrayLengthField, arrayLengthFieldExt)
124+
}
125+
126+
@Test
127+
fun `should provide function for extractPath with varargs`() {
128+
/* Given */
129+
val path = arrayOf("path", "to", "key")
130+
/* When */
131+
val extractPathField = JsonDSL.extractPath(jsonField, *path)
132+
val extractPathFieldExt = extractPath(jsonField, *path)
133+
/* Then */
134+
assertEquals(extractPathField, extractPathFieldExt)
135+
}
136+
137+
@Test
138+
fun `should provide function for extractPath with collection`() {
139+
/* Given */
140+
val path = arrayOf("path", "to", "key")
141+
/* When */
142+
val extractPathField = JsonDSL.extractPath(jsonField, path.toList())
143+
val extractPathFieldExt = extractPath(jsonField, path.toList())
144+
/* Then */
145+
assertEquals(extractPathField, extractPathFieldExt)
146+
}
147+
148+
@Test
149+
fun `should provide function for extractPathText with varargs`() {
150+
/* Given */
151+
val path = arrayOf("path", "to", "key")
152+
/* When */
153+
val extractPathTextField = JsonDSL.extractPathText(jsonField, *path)
154+
val extractPathTextFieldExt = extractPathText(jsonField, *path)
155+
/* Then */
156+
assertEquals(extractPathTextField, extractPathTextFieldExt)
157+
}
158+
159+
@Test
160+
fun `should provide function for extractPathText with collection`() {
161+
/* Given */
162+
val path = arrayOf("path", "to", "key")
163+
/* When */
164+
val extractPathTextField = JsonDSL.extractPathText(jsonField, path.toList())
165+
val extractPathTextFieldExt = extractPathText(jsonField, path.toList())
166+
/* Then */
167+
assertEquals(extractPathTextField, extractPathTextFieldExt)
168+
}
169+
170+
@Test
171+
fun `should provide function for typeOf`() {
172+
/* Given */
173+
/* When */
174+
val typeOfField = JsonDSL.typeOf(jsonField)
175+
val typeOfFieldExt = typeOf(jsonField)
176+
/* Then */
177+
assertEquals(typeOfField, typeOfFieldExt)
178+
}
179+
180+
@Test
181+
fun `should provide function for stripNulls`() {
182+
/* Given */
183+
/* When */
184+
val stripNullsField = JsonDSL.stripNulls(jsonField)
185+
val stripNullsFieldExt = stripNulls(jsonField)
186+
/* Then */
187+
assertEquals(stripNullsField, stripNullsFieldExt)
188+
}
189+
190+
}

0 commit comments

Comments
 (0)