Skip to content

Commit 8f7b4c3

Browse files
NF: BackendUtils gets better type
I had a bug due to an erroneous type sent to `toJsonBytes`. I believe it's better to indicates explicitly which type we know it can accept, and if needed add more type.
1 parent 23b4783 commit 8f7b4c3

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

AnkiDroid/src/main/java/com/ichi2/libanki/TemplateManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class TemplateManager {
218218
backend.renderUncommittedCardLegacy(
219219
_note.toBackendNote(),
220220
_card.ord,
221-
BackendUtils.toJsonBytes(_template!!.deepClone()),
221+
BackendUtils.toJsonBytes(_template!!),
222222
fillEmpty,
223223
true,
224224
)

AnkiDroid/src/main/java/com/ichi2/libanki/backend/BackendUtils.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.ichi2.libanki.backend
2020

2121
import com.google.protobuf.ByteString
2222
import com.ichi2.libanki.utils.LibAnkiAlias
23+
import com.ichi2.utils.JSONObjectHolder
2324
import net.ankiweb.rsdroid.RustCleanup
2425
import org.json.JSONArray
2526
import org.json.JSONObject
@@ -31,12 +32,14 @@ object BackendUtils {
3132
fun jsonToArray(json: ByteString): JSONArray = JSONArray(json.toStringUtf8())
3233

3334
@RustCleanup("Confirm edge cases")
34-
fun toByteString(conf: Any?): ByteString {
35+
fun toByteString(conf: JSONObject): ByteString {
3536
val asString: String = conf.toString()
3637
return ByteString.copyFromUtf8(asString)
3738
}
3839

3940
@RustCleanup("Confirm edge cases")
4041
@LibAnkiAlias("to_json_bytes")
41-
fun toJsonBytes(json: Any?): ByteString = toByteString(json)
42+
fun toJsonBytes(json: JSONObject): ByteString = toByteString(json)
43+
44+
fun toJsonBytes(json: JSONObjectHolder): ByteString = toJsonBytes(json.jsonObject)
4245
}

0 commit comments

Comments
 (0)