summaryrefslogtreecommitdiff
path: root/internal/anki
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-02 17:52:13 +0300
committerPaul Buetow <paul@buetow.org>2026-04-02 17:52:13 +0300
commit11a3e62f17433a7df0e6a77a688321d94a736778 (patch)
treeada0f94c074a3e290ea993179fbf645a40d7ab48 /internal/anki
parentf3d057fec20aeef584cb6340c6a002280a019f15 (diff)
task 003: handle home-dir and APKG marshal errors
Diffstat (limited to 'internal/anki')
-rw-r--r--internal/anki/apkg_generator.go31
-rw-r--r--internal/anki/apkg_generator_test.go8
2 files changed, 34 insertions, 5 deletions
diff --git a/internal/anki/apkg_generator.go b/internal/anki/apkg_generator.go
index 86b28df..c3f489a 100644
--- a/internal/anki/apkg_generator.go
+++ b/internal/anki/apkg_generator.go
@@ -244,14 +244,20 @@ func (g *APKGGenerator) insertCollection(db *sql.DB) error {
"extendRev": 50,
},
}
- decksJSON, _ := json.Marshal(decks)
+ decksJSON, err := marshalJSON("decks", decks)
+ if err != nil {
+ return err
+ }
// Create model (note type) configuration
models := map[string]interface{}{
fmt.Sprintf("%d", g.modelID): g.createNoteTypeConfig(),
fmt.Sprintf("%d", g.modelIDBgBg): g.createBgBgNoteTypeConfig(),
}
- modelsJSON, _ := json.Marshal(models)
+ modelsJSON, err := marshalJSON("models", models)
+ if err != nil {
+ return err
+ }
// Default configuration
conf := map[string]interface{}{
@@ -270,7 +276,10 @@ func (g *APKGGenerator) insertCollection(db *sql.DB) error {
"curModel": fmt.Sprintf("%d", g.modelID),
"dayLearnFirst": false,
}
- confJSON, _ := json.Marshal(conf)
+ confJSON, err := marshalJSON("conf", conf)
+ if err != nil {
+ return err
+ }
// Deck options
dconf := map[string]interface{}{
@@ -311,10 +320,13 @@ func (g *APKGGenerator) insertCollection(db *sql.DB) error {
"replayq": true,
},
}
- dconfJSON, _ := json.Marshal(dconf)
+ dconfJSON, err := marshalJSON("dconf", dconf)
+ if err != nil {
+ return err
+ }
query := `INSERT INTO col VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
- _, err := db.Exec(query,
+ _, err = db.Exec(query,
1, // id
now, // crt
now*1000, // mod
@@ -332,6 +344,15 @@ func (g *APKGGenerator) insertCollection(db *sql.DB) error {
return err
}
+func marshalJSON(name string, value any) ([]byte, error) {
+ data, err := json.Marshal(value)
+ if err != nil {
+ return nil, fmt.Errorf("marshal %s: %w", name, err)
+ }
+
+ return data, nil
+}
+
// createNoteTypeConfig creates the note type configuration
func (g *APKGGenerator) createNoteTypeConfig() map[string]interface{} {
return map[string]interface{}{
diff --git a/internal/anki/apkg_generator_test.go b/internal/anki/apkg_generator_test.go
index 4f2db18..68d828d 100644
--- a/internal/anki/apkg_generator_test.go
+++ b/internal/anki/apkg_generator_test.go
@@ -63,6 +63,7 @@ func TestAPKGAddCard(t *testing.T) {
t.Errorf("Expected Bulgarian 'ябълка', got '%s'", gen.cards[0].Bulgarian)
}
}
+
func TestMediaFiles(t *testing.T) {
gen := NewAPKGGenerator("Test Deck")
@@ -82,6 +83,7 @@ func TestMediaFiles(t *testing.T) {
t.Errorf("Expected mediaFiles['image.jpg'] = 1, got %d", gen.mediaFiles["image.jpg"])
}
}
+
func TestGenerateAPKG(t *testing.T) {
tempDir := t.TempDir()
@@ -213,3 +215,9 @@ func TestCreateDatabase(t *testing.T) {
t.Errorf("Expected 1 note, got %d", noteCount)
}
}
+
+func TestMarshalJSONReturnsErrorForUnsupportedValue(t *testing.T) {
+ if _, err := marshalJSON("bad", make(chan int)); err == nil {
+ t.Fatal("marshalJSON() error = nil, want error")
+ }
+}