Skip to content

Commit

Permalink
feat(kotlin): provide operators through extension functions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
davinkevin committed Dec 20, 2020
1 parent 463900a commit 5d1ba95
Show file tree
Hide file tree
Showing 7 changed files with 809 additions and 0 deletions.
31 changes: 31 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<db.url>jdbc:postgresql://127.0.0.1:${pg.port}/${pg.dbname}</db.url>

<test.jooq-codegen.directory>${project.build.directory}/generated-sources/jooq</test.jooq-codegen.directory>

<kotlin.version>1.4.20</kotlin.version>
</properties>

<dependencies>
Expand All @@ -37,6 +39,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -207,6 +215,29 @@
</dependencies>
</plugin>

<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>


<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package com.github.t9t.jooq.json

import org.jooq.Field
import org.jooq.JSON
import org.jooq.impl.DSL
import org.junit.Assert.assertEquals
import org.junit.Test

/**
* Created by kevin on 20/12/2020
*/
class JsonDSLTestIT {

private val jsonField: Field<JSON> = DSL.field("foo.bar", JSON::class.java)

@Test
fun `should provide extension function for arrayElement`() {
/* Given */
val index = 1
/* When */
val arrayElementField = JsonDSL.arrayElement(jsonField, index)
val arrayElementFieldExt = jsonField.arrayElement(index)
/* Then */
assertEquals(arrayElementField, arrayElementFieldExt)
}

@Test
fun `should provide extension function for arrayElementText`() {
/* Given */
val index = 1
/* When */
val arrayElementTextField = JsonDSL.arrayElementText(jsonField, index)
val arrayElementTextFieldExt = jsonField.arrayElementText(index)
/* Then */
assertEquals(arrayElementTextField, arrayElementTextFieldExt)
}

@Test
fun `should provide extension function for fieldByKey`() {
/* Given */
val key = "key"
/* When */
val fieldByKeyField = JsonDSL.fieldByKey(jsonField, key)
val fieldByKeyFieldExt = jsonField.fieldByKey(key)
/* Then */
assertEquals(fieldByKeyField, fieldByKeyFieldExt)
}

@Test
fun `should provide extension function for fieldByKeyText`() {
/* Given */
val key = "key"
/* When */
val fieldByKeyTextField = JsonDSL.fieldByKeyText(jsonField, key)
val fieldByKeyTextFieldExt = jsonField.fieldByKeyText(key)
/* Then */
assertEquals(fieldByKeyTextField, fieldByKeyTextFieldExt)
}

@Test
fun `should provide extension function for objectAtPath with varargs`() {
/* Given */
val path = arrayOf("path", "to", "key")
/* When */
val objectAtPathField = JsonDSL.objectAtPath(jsonField, *path)
val objectAtPathFieldExt = jsonField.objectAtPath(*path)
/* Then */
assertEquals(objectAtPathField, objectAtPathFieldExt)
}

@Test
fun `should provide extension function for objectAtPath with collection`() {
/* Given */
val path = arrayOf("path", "to", "key")
/* When */
val objectAtPathField = JsonDSL.objectAtPath(jsonField, path.toList())
val objectAtPathFieldExt = jsonField.objectAtPath(path.toList())
/* Then */
assertEquals(objectAtPathField, objectAtPathFieldExt)
}

@Test
fun `should provide extension function for objectAtPathText with varargs`() {
/* Given */
val path = arrayOf("path", "to", "key")
/* When */
val objectAtPathTextField = JsonDSL.objectAtPathText(jsonField, *path)
val objectAtPathTextFieldExt = jsonField.objectAtPathText(*path)
/* Then */
assertEquals(objectAtPathTextField, objectAtPathTextFieldExt)
}

@Test
fun `should provide extension function for objectAtPathText with collection`() {
/* Given */
val path = arrayOf("path", "to", "key")
/* When */
val objectAtPathTextField = JsonDSL.objectAtPathText(jsonField, path.toList())
val objectAtPathTextFieldExt = jsonField.objectAtPathText(path.toList())
/* Then */
assertEquals(objectAtPathTextField, objectAtPathTextFieldExt)
}

@Test
fun `should provide function for arrayLength`() {
/* Given */
/* When */
val arrayLengthField = JsonDSL.arrayLength(jsonField)
val arrayLengthFieldExt = arrayLength(jsonField)
/* Then */
assertEquals(arrayLengthField, arrayLengthFieldExt)
}

@Test
fun `should provide function for extractPath with varargs`() {
/* Given */
val path = arrayOf("path", "to", "key")
/* When */
val extractPathField = JsonDSL.extractPath(jsonField, *path)
val extractPathFieldExt = extractPath(jsonField, *path)
/* Then */
assertEquals(extractPathField, extractPathFieldExt)
}

@Test
fun `should provide function for extractPath with collection`() {
/* Given */
val path = arrayOf("path", "to", "key")
/* When */
val extractPathField = JsonDSL.extractPath(jsonField, path.toList())
val extractPathFieldExt = extractPath(jsonField, path.toList())
/* Then */
assertEquals(extractPathField, extractPathFieldExt)
}

@Test
fun `should provide function for extractPathText with varargs`() {
/* Given */
val path = arrayOf("path", "to", "key")
/* When */
val extractPathTextField = JsonDSL.extractPathText(jsonField, *path)
val extractPathTextFieldExt = extractPathText(jsonField, *path)
/* Then */
assertEquals(extractPathTextField, extractPathTextFieldExt)
}

@Test
fun `should provide function for extractPathText with collection`() {
/* Given */
val path = arrayOf("path", "to", "key")
/* When */
val extractPathTextField = JsonDSL.extractPathText(jsonField, path.toList())
val extractPathTextFieldExt = extractPathText(jsonField, path.toList())
/* Then */
assertEquals(extractPathTextField, extractPathTextFieldExt)
}

@Test
fun `should provide function for typeOf`() {
/* Given */
/* When */
val typeOfField = JsonDSL.typeOf(jsonField)
val typeOfFieldExt = typeOf(jsonField)
/* Then */
assertEquals(typeOfField, typeOfFieldExt)
}

@Test
fun `should provide function for stripNulls`() {
/* Given */
/* When */
val stripNullsField = JsonDSL.stripNulls(jsonField)
val stripNullsFieldExt = stripNulls(jsonField)
/* Then */
assertEquals(stripNullsField, stripNullsFieldExt)
}

}
Loading

0 comments on commit 5d1ba95

Please sign in to comment.