Skip to content

Commit 1860276

Browse files
committed
Revert JsonPath change
1 parent 93732cd commit 1860276

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ data class ColumnOptions(
99
val function: String?,
1010
val cast: String?,
1111
val innerColumns: String,
12-
val jsonKey: String?
1312
)

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ class SelectableSymbolProcessor(
101101
val isForeign = parameter.annotations.getAnnotationOrNull(Foreign::class.java.simpleName) != null
102102
val jsonPathArguments = parameter.annotations.getAnnotationOrNull(JsonPath::class.java.simpleName)?.arguments
103103
val jsonPath = jsonPathArguments?.getParameterValue<ArrayList<String>>(JsonPath.PATH_PARAMETER_NAME)
104-
val jsonKey = jsonPathArguments?.getParameterValue<String>(JsonPath.KEY_PARAMETER_NAME)
105104
val returnAsText = jsonPathArguments?.getParameterValue<Boolean>(JsonPath.RETURN_AS_TEXT_PARAMETER_NAME) == true
106105
val function = parameter.annotations.getAnnotationOrNull(ApplyFunction::class.java.simpleName)
107106
?.arguments?.getParameterValue<String>(ApplyFunction.FUNCTION_PARAMETER_NAME)
@@ -112,7 +111,6 @@ class SelectableSymbolProcessor(
112111
columnName = columnName,
113112
isForeign = isForeign,
114113
jsonPath = jsonPath,
115-
jsonKey = jsonKey,
116114
returnAsText = returnAsText,
117115
function = function,
118116
cast = cast,
@@ -145,9 +143,12 @@ class SelectableSymbolProcessor(
145143
if(options.jsonPath != null && options.function != null) {
146144
logger.error("Parameter $parameterName can't have both @JsonPath and @ApplyFunction annotation", symbol)
147145
}
148-
if(options.jsonKey != null && options.columnName == null) {
146+
if(options.jsonPath != null && options.columnName == null) {
149147
logger.error("Parameter $parameterName must have a @ColumnName annotation when using @JsonPath", symbol)
150148
}
149+
if(options.jsonPath != null && options.jsonPath.isEmpty()) {
150+
logger.error("Parameter $parameterName must have at least one path in @JsonPath annotation", symbol)
151+
}
151152
}
152153

153154
private fun buildColumns(
@@ -158,10 +159,10 @@ class SelectableSymbolProcessor(
158159
): String {
159160
return buildString {
160161
if(options.jsonPath != null) {
161-
if(options.alias != options.jsonKey) {
162+
if(options.alias != options.jsonPath.last()) {
162163
append("${options.alias}:")
163164
}
164-
append(buildJsonPath(options.columnName, options.jsonKey, options.jsonPath, options.returnAsText))
165+
append(buildJsonPath(options.columnName, options.jsonPath, options.returnAsText))
165166
return@buildString
166167
}
167168

@@ -195,20 +196,19 @@ class SelectableSymbolProcessor(
195196

196197
private fun buildJsonPath(
197198
columnName: String?,
198-
jsonKey: String?,
199199
jsonPath: List<String>,
200200
returnAsText: Boolean
201201
): String {
202202
val operator = if(returnAsText) "->>" else "->"
203203
return buildString {
204204
append(columnName)
205-
if(jsonPath.isNotEmpty()) {
206-
jsonPath.forEach {
205+
if(jsonPath.size > 1) {
206+
jsonPath.dropLast(1).forEach {
207207
append("->$it")
208208
}
209209
}
210210
append(operator)
211-
append(jsonKey)
211+
append(jsonPath.last())
212212
}
213213
}
214214

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,32 @@ import io.github.jan.supabase.annotations.SupabaseInternal
99
* Table with a JSON column `data`:
1010
* ```json
1111
* {
12-
* "id": 1
12+
* "id": 1,
13+
* "nested": {
14+
* "name": "John"
15+
* }
1316
* }
1417
* ```
1518
* ```kotlin
1619
* @Selectable
1720
* data class Example(
1821
* @ColumnName("data")
1922
* @JsonPath("id")
20-
* val id: Int
23+
* val id: Int,
24+
*
25+
* @ColumnName("data")
26+
* @JsonPath("nested", "name")
27+
* val name: String
2128
* )
2229
* ```
2330
*
24-
* @param key The key of the JSON property.
25-
* @param path Additional path to the JSON property. If the JSON property is nested, you can specify the path to the property.
31+
* @param path The path to the JSON property. At least one path must be specified.
2632
* @param returnAsText Whether to return the JSON property as text. If `true`, the JSON property will be returned as a string. If `false`, the JSON property will be returned as JSON.
2733
*
2834
*/
2935
@Target(AnnotationTarget.VALUE_PARAMETER)
3036
@Retention(AnnotationRetention.SOURCE)
31-
annotation class JsonPath(val key: String, vararg val path: String, val returnAsText: Boolean = false) {
37+
annotation class JsonPath(vararg val path: String, val returnAsText: Boolean = false) {
3238

3339
companion object {
3440
@SupabaseInternal const val PATH_PARAMETER_NAME = "path"

0 commit comments

Comments
 (0)