Skip to content

Conversation

@nerodesu017
Copy link
Collaborator

No description provided.

Comment on lines 152 to 168
@JvmStatic
suspend fun getRaw(name: String, params: String?): Result<Map<String, Any?>> {
return withContext(Dispatchers.IO) {
val res = getNative(name, params)
if (res.status != 0) {
return@withContext Result.failure(parseOpacityError(res.err))
}

val map: Map<String, Any?> =
Json.parseToJsonElement(res.data!!).jsonObject.mapValues {
parseJsonElementToAny(it.value)
}

return@withContext Result.success(map)
}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed a new function because, funny enough, when trying to Json.encodeToString(), it would crash the app because Any can not be serialized. (line 137 - val paramsString = params?.let { Json.encodeToString(it) })

Am I doing something wrong here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? what did you try to pass. But in any case Json.encodeToString() is kinda basic, you can see on the JsonToAnyConverter I had to write custom logic to create generic JSON from a string. You probably will need to write the inverse or just pass the raw string after doing your validation? Not sure what you tried to pass

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to pass {"previous_response": ""} to it

I even modified my function to use the JsonToAnyConverter.parseJsonElementToAny:

val paramsValue = if (params == null) null else {
    val parsed = kotlinx.serialization.json.Json.parseToJsonElement(
        params
    )
    parsed.jsonObject.entries.map { (key, value) ->
        key to JsonToAnyConverter.parseJsonElementToAny(
            value
        )
    }.toMap()
}
val res = OpacityCore.get(
    flowInput.value,
    paramsValue
)

And it is still crashing on what it was crashing before

I would probably need to write logic for doing the inverse Any to actual JSON.

I was thinking trying to parse it and validating and then giving the raw string directly would be better? The way I've done in the PR?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just took a look at it seems I made the mistake. A Map<String, Any>? is not serializable by kotlinx.serialization. It seems the library is really following the Java philosphy of only working with objects/classes, so it cannot convert Any... terrible

I'm taking a look to see if there is anyway it can take a Map<String, Any> before serializing it

@nerodesu017 nerodesu017 marked this pull request as ready for review July 29, 2025 13:36
@nerodesu017 nerodesu017 requested a review from ospfranco July 29, 2025 13:36
@ospfranco ospfranco merged commit cfa8aa4 into main Aug 4, 2025
@ospfranco ospfranco deleted the nero/ENG-101-dynamic-params-textbox branch August 4, 2025 12:52
Copy link
Collaborator Author

@nerodesu017 nerodesu017 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants