-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
improvement(libanki): sync Decks .ktwith upstream
#19200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
ce529ed to
025f690
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a note, but everything else LGTM.
| TODO() | ||
| } | ||
| @Suppress("unused") | ||
| fun nameIfExists(did: DeckId): String? = getLegacy(did)?.name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should implement nameIfExists and name as they are in the backend(get() is the same) to not have to update them once again when get() has the proper implementation.
This is why I'm not fond of implementing these methods ahead of actually using them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented get; fixed name and nameIfExists
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverted the get() changes due to test failure. Would you like these resolved in another manner?
git reset 28298a3265d29700c15e7cbeddddc6fd88dd35ec
DecksTest > test_remove FAILED
org.opentest4j.AssertionFailedError: expected: <[no deck]> but was: <Default>
at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
at app//org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1156)
at app//kotlin.test.junit5.JUnit5Asserter.assertEquals(JUnitSupport.kt:32)
at app//kotlin.test.AssertionsKt__AssertionsKt.assertEquals(Assertions.kt:63)
at app//kotlin.test.AssertionsKt.assertEquals(Unknown Source)
at app//kotlin.test.AssertionsKt__AssertionsKt.assertEquals$default(Assertions.kt:62)
at app//kotlin.test.AssertionsKt.assertEquals$default(Unknown Source)
at app//com.ichi2.anki.libanki.DecksTest.test_remove(DecksTest.kt:48)
Index: libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt b/libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt
--- a/libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt (revision 28298a3265d29700c15e7cbeddddc6fd88dd35ec)
+++ b/libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt (date 1760665574575)
@@ -38,6 +38,7 @@
import anki.decks.copy
import com.ichi2.anki.common.annotations.NeedsTest
import com.ichi2.anki.common.utils.ext.jsonObjectIterable
+import com.ichi2.anki.libanki.Consts.DEFAULT_DECK_ID
import com.ichi2.anki.libanki.backend.BackendUtils
import com.ichi2.anki.libanki.backend.BackendUtils.fromJsonBytes
import com.ichi2.anki.libanki.backend.BackendUtils.toJsonBytes
@@ -289,19 +290,29 @@
return col.db.queryScalar("select count() from cards where did in $strIds or odid in $strIds")
}
- @RustCleanup("implement and make public")
@LibAnkiAlias("get")
- @Suppress("unused", "unused_parameter")
+ @Suppress("unused", "unused_parameter", "LiftReturnOrAssignment", "RedundantElseInIf")
@CheckResult
- private fun get(
- did: DeckId,
+ fun get(
+ did: DeckId?,
default: Boolean = true,
- ): Deck? =
- try {
- Deck(BackendUtils.fromJsonBytes(col.backend.getDeckLegacy(did)))
- } catch (ex: BackendNotFoundException) {
- null
+ ): Deck? {
+ if (did == null) {
+ if (default) {
+ return getLegacy(DEFAULT_DECK_ID)
+ } else {
+ return null
+ }
}
+ val deck = getLegacy(did)
+ if (deck != null) {
+ return deck
+ } else if (default) {
+ return getLegacy(DEFAULT_DECK_ID)
+ } else {
+ return null
+ }
+ }
/** Get deck with NAME, ignoring case. */
@LibAnkiAlias("by_name")
@@ -494,13 +505,13 @@
/** Returns the [deck name][Deck.name], or 'no deck' if not found */
@LibAnkiAlias("name")
@CheckResult
- fun name(did: DeckId): String = getLegacy(did)?.name ?: col.backend.tr.decksNoDeck()
+ fun name(did: DeckId): String = get(did)?.name ?: col.backend.tr.decksNoDeck()
/** Returns the [deck name][Deck.name], or `null` if not found */
@LibAnkiAlias("name_if_exists")
@Suppress("unused")
@CheckResult
- fun nameIfExists(did: DeckId): String? = getLegacy(did)?.name
+ fun nameIfExists(did: DeckId): String? = get(did)?.name
@RustCleanup("implement and make public")
@Suppress("unused", "unused_parameter")There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decks.get() is ok
Decks.name() should include the boolean default parameter like on backend
Decks.nameIfExists() should call name() with false like on backend
Then we could make the correct call for that test to pass.
025f690 to
28298a3
Compare
28298a3 to
1a80024
Compare
The return value is now nullable, so callers were updated https://github.com/ankitects/anki/blob/a6d5c949970627f2b4dcea8a02fea3a497e0440f/pylib/anki/decks.py
1a80024 to
3ef4408
Compare
Purpose / Description
https://github.com/ankitects/anki/blob/a6d5c949970627f2b4dcea8a02fea3a497e0440f/pylib/anki/decks.py
Approach
Go through each method
RustCleanupon othersHow Has This Been Tested?
Unit tested
Checklist