KV
10/28/2020, 4:25 AMval filter: QueryFilter? = null
and I have one class like this ->
class QueryFilter(val by: String, val operator: String, val value: String)
And I am using the variable with class like this ->
filter.let {
mapOf(Pair("q[${<http://it.by|it.by>}_${it.operator}]", it.value))
} ?: emptyMap()
But now I have a variable like this
val filter: Array<QueryFilter> = emptyArray()
then how to use the loop with filter function with the same class?
I want to know that is this the correct way to write a code ? or Is there any better way to write the below code?
filter.let { it ->
it.forEach {
mapOf(Pair("q[${<http://it.by|it.by>}_${it.operator}]", it.value))
}
}
}) as Map<String, String>
Tobias Berger
10/28/2020, 8:09 AM?.let
.
The .let
call in your last code snippet is obsolete, because you're only using it once to call forEach
. Also, your forEach
lambda shadows the it
which makes code more confusing. Also the cast in the end wouldn't work, because forEach just returns Unit.
what you probably mean to do is this:
filter.associate { Pair("q[${<http://it.by|it.by>}_${it.operator}]", it.value) }
or to make it even a bit more "kotliny" but still doing exactly the same thing
filter.associate { "q[${<http://it.by|it.by>}_${it.operator}]" to it.value }
KV
10/28/2020, 8:16 AMval filter: QueryFilter? = null
because I know I have only QueryFilter data but now the requirement is we have to use ArrayOfQueryFilter with assigning the empty array val filter: Array<QueryFilter> = emptyArray()
.
So now I have to use forloop to get all the query filter data so I modified like below code (More readable format)
filter.let { it: Array<QueryFilter> ->
it.forEach {
mapOf(Pair("q[${<http://it.by|it.by>}_${it.operator}]", it.value))
}
}
}) as Map<String, String>
KV
10/28/2020, 8:21 AMType mismatch.
Required:
Map<String, String>
Found:
() → Map<String, String>
Tobias Berger
10/28/2020, 8:34 AMfilter.let { it.forEach { ... } }
or just filter.forEach { ... }
and usually IntelliJ will actually tell you that.
The code I posted should produce a Map, not a lambda returning a map. can you show a more complete snippet of code that shows how you're using it?KV
10/28/2020, 9:12 AMsealed class Api : JsonApi() {
// API endpoints
data class Index(val accountId: String, val filter: Array<QueryFilters> = emptyArray()) : Api()
// JsonApi implementation
override val baseUrl: String
get() = "COMPANY URL"
override val subpath: String
get() = when (this) {
is Index -> "data/$accountId/index"
}
override val method: HttpMethod
get() = when (this) {
is Index -> HttpMethod.Get
}
override val headers: Map<String, String>
get() = emptyMap()
override val queryParameters: Map<String, String>
get() = when (this) {
is Index -> ({
filter.let { it ->
it.forEach {
mapOf(Pair("q[${<http://it.by|it.by>}_${it.operator}]", it.value))
}
}
}) as Map<String, String>
}
}
class QueryFilters(val by: String, val operator: String, val value: String)
This queryParameters is a override method so adding the below part of code where we are creating the function and using the queryParameters
with for loop line number 3.
1. private fun buildRequestUrl(): URL {
2. val uriBuilder = Uri.parse(baseUrl).buildUpon()
3. for ((key, value) in queryParameters) {
4. uriBuilder.appendQueryParameter(key, value)
5. }
6. return URL(uriBuilder.build().toString())
7. }
Tobias Berger
10/28/2020, 9:18 AMoverride val queryParameters: Map<String, String>
get() = when (this) {
is Index -> filter.associate { "q[${<http://it.by|it.by>}_${it.operator}]" to it.value }
}
you probably tried this:
override val queryParameters: Map<String, String>
get() = when (this) {
is Index -> ({ filter.associate { "q[${<http://it.by|it.by>}_${it.operator}]" to it.value } })
}
That wraps the code in a lambda body instead of calling itKV
10/28/2020, 9:29 AM