Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kotlin): provide operators through extension functions #12

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.21</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 @@ -188,6 +196,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,85 @@
package com.github.t9t.jooq.json

import com.github.t9t.jooq.generated.Tables
import org.jooq.JSON
import org.jooq.JSONB
import org.jooq.Record1
import org.jooq.SQLDialect
import org.jooq.impl.DSL
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test

/**
* Validates Kotlin, jOOQ and JSON(B) interoperability
*/
class JsonKotlinIT {
private val ds = TestDb.createDataSource()
private val dsl = DSL.using(ds, SQLDialect.POSTGRES)

@Before
fun setUp() {
dsl.deleteFrom(Tables.JSON_TEST).execute()
assertEquals(4, dsl.execute("insert into jooq.json_test (name, data, datab)" +
" values " +
"('both', '{\"json\": {\"int\": 100, \"str\": \"Hello, JSON world!\", \"object\": {\"v\": 200}, \"n\": null}}', '{\"jsonb\": {\"int\": 100, \"str\": \"Hello, JSONB world!\", \"object\": {\"v\": 200}, \"n\": null}}')," +
"('empty', '{}', '{}')," +
"('null-sql', null, null)," +
"('null-json', 'null'::json, 'null'::jsonb)").toLong())
}

@Test
fun `select JSON NULL SQL, returning non-null JSON type`() {
val r: Record1<JSON> = dsl.select(Tables.JSON_TEST.DATA)
.from(Tables.JSON_TEST)
.where(Tables.JSON_TEST.NAME.eq("null-sql"))
.fetchOne()!!
Assert.assertNull(r.value1())
}

@Test
fun `select JSON NULL SQL, returning nullable JSON type`() {
val r: Record1<JSON?> = dsl.select(Tables.JSON_TEST.DATA)
.from(Tables.JSON_TEST)
.where(Tables.JSON_TEST.NAME.eq("null-sql"))
.fetchOne()!!
Assert.assertNull(r.value1())
}

@Test
fun `select null JSON value`() {
val r: Record1<JSON> = dsl.select(Tables.JSON_TEST.DATA)
.from(Tables.JSON_TEST)
.where(Tables.JSON_TEST.NAME.eq("null-json"))
.fetchOne()!!
assertEquals("null", r.value1().toString())
}

@Test
fun `select JSON NULL SQL, returning non-null JSONB type`() {
val r: Record1<JSONB> = dsl.select(Tables.JSON_TEST.DATAB)
.from(Tables.JSON_TEST)
.where(Tables.JSON_TEST.NAME.eq("null-sql"))
.fetchOne()!!
Assert.assertNull(r.value1())
}

@Test
fun `select JSON NULL SQL, returning nullable JSONB type`() {
val r: Record1<JSONB?> = dsl.select(Tables.JSON_TEST.DATAB)
.from(Tables.JSON_TEST)
.where(Tables.JSON_TEST.NAME.eq("null-sql"))
.fetchOne()!!
Assert.assertNull(r.value1())
}

@Test
fun `select null JSONB value`() {
val r = dsl.select(Tables.JSON_TEST.DATAB)
.from(Tables.JSON_TEST)
.where(Tables.JSON_TEST.NAME.eq("null-json"))
.fetchOne()!!
assertEquals("null", r.value1().toString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package com.github.t9t.jooq.json.json

import com.github.t9t.jooq.json.JsonDSL
import org.jooq.Field
import org.jooq.JSON
import org.jooq.impl.DSL
import org.junit.Assert.assertEquals
import org.junit.Test

class JsonDSLTest {

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

@Test
fun `should provide extension function to create field from JSON`() {
/* Given */
val json = JSON.valueOf("{}")
/* When */
val jsonField = JsonDSL.field(json)
val jsonFieldExt = json.toField()
/* Then */
assertEquals(jsonField, jsonFieldExt)
}

@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