Skip to content

Commit be8a919

Browse files
committed
Support for more auto casts
1 parent 2ca488f commit be8a919

File tree

9 files changed

+41
-13
lines changed

9 files changed

+41
-13
lines changed

KSP/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
2+
13
plugins {
24
id(libs.plugins.kotlin.jvm.get().pluginId)
35
}
@@ -11,4 +13,8 @@ dependencies {
1113
implementation(libs.kotlin.poet)
1214
implementation(libs.kotlin.poet.ksp)
1315
implementation(project(":postgrest-kt"))
16+
}
17+
18+
tasks.named<KotlinCompilationTask<*>>("compileKotlin").configure {
19+
compilerOptions.freeCompilerArgs.add("-opt-in=io.github.jan.supabase.annotations.SupabaseInternal")
1420
}

KSP/src/main/kotlin/io/github/jan/supabase/ksp/PrimitiveColumnType.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@ package io.github.jan.supabase.ksp
22

33
val primitiveColumnTypes = mapOf<String, String>(
44
"kotlin.String" to "text",
5-
"kotlin.Int" to "int",
5+
"kotlin.Int" to "int4",
66
"kotlin.Long" to "int8",
77
"kotlin.Float" to "float4",
88
"kotlin.Double" to "float8",
99
"kotlin.Boolean" to "bool",
1010
"kotlin.Byte" to "int2",
1111
"kotlin.Short" to "int2",
1212
"kotlin.Char" to "char",
13-
//TIMESTAMPS etc.
13+
"kotlinx.datetime.Instant" to "timestamptz",
14+
"kotlinx.datetime.LocalDateTime" to "timestamp",
15+
"kotlin.uuid.Uuid" to "uuid",
16+
"kotlinx.datetime.LocalTime" to "time",
17+
"kotlinx.datetime.LocalDate" to "date",
18+
"kotlinx.serialization.json.JsonElement" to "jsonb",
19+
"kotlinx.serialization.json.JsonObject" to "jsonb",
20+
"kotlinx.serialization.json.JsonArray" to "jsonb",
21+
"kotlinx.serialization.json.JsonPrimitive" to "jsonb",
1422
)
1523

1624
fun isPrimitive(qualifiedName: String) = primitiveColumnTypes.containsKey(qualifiedName)

Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/annotations/ApplyFunction.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.jan.supabase.postgrest.annotations
22

3+
import io.github.jan.supabase.annotations.SupabaseInternal
4+
35
/**
46
* Annotation to apply a function to a column in a PostgREST query.
57
* @param function The function to apply to the column.
@@ -9,7 +11,12 @@ package io.github.jan.supabase.postgrest.annotations
911
annotation class ApplyFunction(val function: String) {
1012

1113
companion object {
12-
const val FUNCTION_PARAMETER_NAME = "function"
14+
@SupabaseInternal const val FUNCTION_PARAMETER_NAME = "function"
15+
const val AVG = "avg"
16+
const val COUNT = "count"
17+
const val MAX = "max"
18+
const val MIN = "min"
19+
const val SUM = "sum"
1320
}
1421

1522
}

Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/annotations/Cast.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.jan.supabase.postgrest.annotations
22

3+
import io.github.jan.supabase.annotations.SupabaseInternal
4+
35
/**
46
* Annotation to cast a column in a PostgREST query.
57
* @param type The type to cast the column to. If empty, the type will be inferred from the parameter type. For example, if the parameter is of type `String`, the column will be cast to `text`.
@@ -9,7 +11,7 @@ package io.github.jan.supabase.postgrest.annotations
911
annotation class Cast(val type: String = "") {
1012

1113
companion object {
12-
const val TYPE_PARAMETER_NAME = "type"
14+
@SupabaseInternal const val TYPE_PARAMETER_NAME = "type"
1315
}
1416

1517
}

Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/annotations/ColumnName.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.jan.supabase.postgrest.annotations
22

3+
import io.github.jan.supabase.annotations.SupabaseInternal
4+
35
/**
46
* Annotation to specify the name of a column in a PostgREST query.
57
*
@@ -11,7 +13,7 @@ package io.github.jan.supabase.postgrest.annotations
1113
annotation class ColumnName(val name: String) {
1214

1315
companion object {
14-
const val NAME_PARAMETER_NAME = "name"
16+
@SupabaseInternal const val NAME_PARAMETER_NAME = "name"
1517
}
1618

1719
}

Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/annotations/Foreign.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package io.github.jan.supabase.postgrest.annotations
33
/**
44
* Annotation to specify that a column is a foreign key in a PostgREST query.
55
*
6-
* This annotation may be used in combination with [ColumnName] to specify the name of the foreign key column. The type of the parameter must be marked with [Selectable].
6+
* This annotation may be used in combination with [ColumnName] to specify the name of the foreign key column. The type of the parameter must be annotated with [Selectable].
77
*/
88
@Target(AnnotationTarget.VALUE_PARAMETER)
99
@Retention(AnnotationRetention.SOURCE)

Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/annotations/JsonPath.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.jan.supabase.postgrest.annotations
22

3+
import io.github.jan.supabase.annotations.SupabaseInternal
4+
35
/**
46
* Annotation to specify a JSON path in a PostgREST query.
57
*
@@ -24,8 +26,8 @@ package io.github.jan.supabase.postgrest.annotations
2426
annotation class JsonPath(vararg val path: String, val returnAsText: Boolean = false) {
2527

2628
companion object {
27-
const val PATH_PARAMETER_NAME = "path"
28-
const val RETURN_AS_TEXT_PARAMETER_NAME = "returnAsText"
29+
@SupabaseInternal const val PATH_PARAMETER_NAME = "path"
30+
@SupabaseInternal const val RETURN_AS_TEXT_PARAMETER_NAME = "returnAsText"
2931
}
3032

3133
}

Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/annotations/Selectable.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ package io.github.jan.supabase.postgrest.annotations
33
import io.github.jan.supabase.postgrest.query.PostgrestQueryBuilder
44

55
/**
6-
* Marks a class as selectable.
6+
* Annotates a class as selectable.
77
*
88
* When using the **ksp-compiler** this annotation will be processed and columns for [PostgrestQueryBuilder.select] will be generated.
99
*
1010
* The columns can be accessed via the generated extension property for the companion object of the class.
1111
*
1212
* **Note:**
13-
* - All classes marked with this annotation must have a companion object, which can be empty, and must be a data class
14-
* - All parameters in the primary constructor must a primitive type`*` or a type that is also marked with [Selectable]
15-
* - Parameters may be marked with [ColumnName], [ApplyFunction], [Cast], [JsonPath], [Foreign].
13+
* - All classes annotated with this annotation must have a companion object, which can be empty, and must be a data class
14+
* - All parameters in the primary constructor must a primitive type¹, a type that is also annotated with [Selectable] or a serializable type.
15+
* - Parameters may be annotated with [ColumnName], [ApplyFunction], [Cast], [JsonPath], [Foreign].
1616
*
17-
* `*` Available primitive types are: String, Int, Long, Float, Double, Boolean
17+
* ¹: Available primitive types are: String, Int, Long, Float, Double, Boolean, Byte, Short, Char, Instant, LocalDateTime, Uuid, LocalTime, LocalDate, JsonElement, JsonObject, JsonArray, JsonPrimitive
1818
*
1919
* Example usage:
2020
* ```kotlin

buildSrc/src/main/kotlin/Modules.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import org.gradle.kotlin.dsl.DependencyHandlerScope
12
import org.jetbrains.compose.desktop.DesktopExtension
23
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
34
import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler

0 commit comments

Comments
 (0)