diff --git a/.gitignore b/.gitignore index 8f5229ceb..166a5c497 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Project exclude paths /.gradle/ **/build/** +/test/ .idea local.properties diff --git a/Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/query/PostgrestBuilder.kt b/Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/query/PostgrestBuilder.kt index 5be49680d..e60cff79b 100644 --- a/Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/query/PostgrestBuilder.kt +++ b/Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/query/PostgrestBuilder.kt @@ -130,7 +130,7 @@ class PostgrestBuilder( filter: PostgrestFilterBuilder.() -> Unit = {} ): PostgrestResult { val updateRequest = UpdateRequest( - body = buildPostgrestUpdate(postgrest.config.propertyConversionMethod, update), + body = buildPostgrestUpdate(postgrest.config.propertyConversionMethod, postgrest.serializer, update), returning = returning, count = count, filter = buildPostgrestFilter(postgrest.config.propertyConversionMethod, filter), diff --git a/Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/query/PostgrestUpdate.kt b/Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/query/PostgrestUpdate.kt index 6e2bdddcf..4cd1f2f8f 100644 --- a/Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/query/PostgrestUpdate.kt +++ b/Postgrest/src/commonMain/kotlin/io/github/jan/supabase/postgrest/query/PostgrestUpdate.kt @@ -1,18 +1,19 @@ package io.github.jan.supabase.postgrest.query +import io.github.jan.supabase.SupabaseSerializer import io.github.jan.supabase.annotations.SupabaseInternal +import io.github.jan.supabase.encodeToJsonElement import io.github.jan.supabase.postgrest.PropertyConversionMethod -import io.github.jan.supabase.supabaseJson import kotlinx.serialization.json.JsonElement +import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive -import kotlinx.serialization.json.encodeToJsonElement import kotlin.reflect.KProperty1 /** * Represents a postgrest update query */ -class PostgrestUpdate(@PublishedApi internal val propertyConversionMethod: PropertyConversionMethod) { +class PostgrestUpdate(@PublishedApi internal val propertyConversionMethod: PropertyConversionMethod, @PublishedApi internal val serializer: SupabaseSerializer) { @PublishedApi internal val map = mutableMapOf() @@ -20,45 +21,43 @@ class PostgrestUpdate(@PublishedApi internal val propertyConversionMethod: Prope /** * Sets the value of the column with the name of the [KProperty1] to [value] */ - inline infix fun KProperty1.setTo(value: V?) { - map[propertyConversionMethod(this)] = supabaseJson.encodeToJsonElement(value) + inline infix fun KProperty1>.setTo(value: T) { + if(value == null) { + set(propertyConversionMethod(this), JsonNull) + } else { + set(propertyConversionMethod(this), serializer.encodeToJsonElement(value)) + } } /** * Sets the value of the column with the name of the [KProperty1] to [value] */ - @Suppress("NOTHING_TO_INLINE") - inline infix fun KProperty1.setTo(value: String?) = set(propertyConversionMethod(this), value) + infix fun KProperty1.setTo(value: String?) = set(propertyConversionMethod(this), value) /** * Sets the value of the column with the name of the [KProperty1] to [value] */ - @Suppress("NOTHING_TO_INLINE") - inline infix fun KProperty1.setTo(value: Int?) = set(propertyConversionMethod(this), value) + infix fun KProperty1.setTo(value: Int?) = set(propertyConversionMethod(this), value) /** * Sets the value of the column with the name of the [KProperty1] to [value] */ - @Suppress("NOTHING_TO_INLINE") - inline infix fun KProperty1.setTo(value: Long?) = set(propertyConversionMethod(this), value) + infix fun KProperty1.setTo(value: Long?) = set(propertyConversionMethod(this), value) /** * Sets the value of the column with the name of the [KProperty1] to [value] */ - @Suppress("NOTHING_TO_INLINE") - inline infix fun KProperty1.setTo(value: Float?) = set(propertyConversionMethod(this), value) + infix fun KProperty1.setTo(value: Float?) = set(propertyConversionMethod(this), value) /** * Sets the value of the column with the name of the [KProperty1] to [value] */ - @Suppress("NOTHING_TO_INLINE") - inline infix fun KProperty1.setTo(value: Double?) = set(propertyConversionMethod(this), value) + infix fun KProperty1.setTo(value: Double?) = set(propertyConversionMethod(this), value) /** * Sets the value of the column with the name of the [KProperty1] to [value] */ - @Suppress("NOTHING_TO_INLINE") - inline infix fun KProperty1.setTo(value: Boolean?) = set(propertyConversionMethod(this), value) + infix fun KProperty1.setTo(value: Boolean?) = set(propertyConversionMethod(this), value) /** * Sets the value of the [column] to [value] @@ -102,11 +101,22 @@ class PostgrestUpdate(@PublishedApi internal val propertyConversionMethod: Prope map[column] = JsonPrimitive(value) } + /** + * Sets the value of the [column] to null + */ + fun setToNull(column: String) { + map[column] = JsonNull + } + /** * Sets the value of the [column] to [value] */ - operator fun set(column: String, value: JsonElement) { - map[column] = value + inline operator fun set(column: String, value: T) { + if(value == null) { + map[column] = JsonNull + } else { + map[column] = serializer.encodeToJsonElement(value) + } } @PublishedApi internal fun toJson() = JsonObject(map) @@ -114,8 +124,8 @@ class PostgrestUpdate(@PublishedApi internal val propertyConversionMethod: Prope } @SupabaseInternal -inline fun buildPostgrestUpdate(propertyConversionMethod: PropertyConversionMethod = PropertyConversionMethod.SERIAL_NAME, block: PostgrestUpdate.() -> Unit): JsonObject { - val update = PostgrestUpdate(propertyConversionMethod) +inline fun buildPostgrestUpdate(propertyConversionMethod: PropertyConversionMethod = PropertyConversionMethod.SERIAL_NAME, serializer: SupabaseSerializer, block: PostgrestUpdate.() -> Unit): JsonObject { + val update = PostgrestUpdate(propertyConversionMethod, serializer) update.block() return update.toJson() } \ No newline at end of file