Skip to content

Commit 1ab0634

Browse files
committed
feat: safer Query value filter
Pairs with empty iterables value are filtered out, as well as "bad enums". Note that hourly/daily may now be safely called with empty arrays.
1 parent 46ea407 commit 1ab0634

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

  • lib/src/main/kotlin/com/openmeteo/api/common/query

lib/src/main/kotlin/com/openmeteo/api/common/query/Query.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,24 @@ interface Query {
2222
key(property, T::class)
2323

2424
@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class)
25-
internal fun value(any: Any?): String =
25+
internal fun value(any: Any?): String? =
2626
when (any) {
27-
is Iterable<*> -> any.joinToString(",") { value(it) }
28-
is Enum<*> -> any.javaClass.kotlin.serializer()
29-
.descriptor.getElementName(any.ordinal)
27+
is Iterable<*> -> any
28+
.mapNotNull { value(it) }
29+
.takeUnless { it.isEmpty() }
30+
?.joinToString(",")
31+
is Enum<*> -> runCatching {
32+
any.javaClass.kotlin.serializer()
33+
.descriptor.getElementName(any.ordinal)
34+
}.getOrNull()
35+
null -> null
3036
else -> any.toString()
3137
}
3238

3339
}
3440

3541
private fun value(property: KProperty1<Query, *>) =
36-
property.get(this)?.let { Companion.value(it) }
42+
Companion.value(property.get(this))
3743

3844
private val memberProperties
3945
get() =

0 commit comments

Comments
 (0)