Skip to content

Commit 5d1ba95

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 463900a commit 5d1ba95

File tree

7 files changed

+809
-0
lines changed

7 files changed

+809
-0
lines changed

integration-tests/pom.xml

+31
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>
@@ -207,6 +215,29 @@
207215
</dependencies>
208216
</plugin>
209217

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

0 commit comments

Comments
 (0)