Return empty array instead of null for empty list · ejholmes/kiterss@1ea8a93 · GitHub
Skip to content

Commit 1ea8a93

Browse files
ejholmesclaude
andcommitted
Return empty array instead of null for empty list
Initialize slice as []Item{} instead of nil so JSON output is "[]" rather than "null" when there are no unread items. Fixes #4 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d6fdf8b commit 1ea8a93

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (k *KiteRSS) List(configPath string) {
125125

126126
// ListItems fetches feeds from the given URLs and returns unread items.
127127
func (k *KiteRSS) ListItems(urls []string) []Item {
128-
var allItems []Item
128+
allItems := []Item{}
129129
var mu sync.Mutex
130130
var wg sync.WaitGroup
131131

main_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"database/sql"
6+
"encoding/json"
67
"testing"
78

89
"github.com/mmcdole/gofeed"
@@ -124,3 +125,28 @@ func TestMarkReadIdempotent(t *testing.T) {
124125
t.Fatalf("second MarkRead failed: %v", err)
125126
}
126127
}
128+
129+
func TestListItemsEmptyReturnsArray(t *testing.T) {
130+
db := setupTestDB(t)
131+
defer db.Close()
132+
133+
k := &KiteRSS{
134+
DB: db,
135+
Fetcher: &MockFeedFetcher{Feeds: map[string]*gofeed.Feed{}},
136+
Out: &bytes.Buffer{},
137+
ErrOut: &bytes.Buffer{},
138+
}
139+
140+
items := k.ListItems([]string{})
141+
142+
// Should return empty slice, not nil
143+
if items == nil {
144+
t.Fatal("expected empty slice, got nil")
145+
}
146+
147+
// JSON should be "[]" not "null"
148+
out, _ := json.Marshal(items)
149+
if string(out) != "[]" {
150+
t.Errorf("expected JSON '[]', got '%s'", string(out))
151+
}
152+
}

0 commit comments

Comments
 (0)