From 4b93a258a6ba1cfb8f9bbb8114935be028f21560 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 17 Jan 2025 07:30:01 -0800 Subject: [PATCH] Refactor item formatting into core/item.go --- cmd/feed.go | 31 +++++-------------------------- core/db.go | 32 ++------------------------------ core/item.go | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 56 deletions(-) create mode 100644 core/item.go diff --git a/cmd/feed.go b/cmd/feed.go index 71e2601..bccb865 100644 --- a/cmd/feed.go +++ b/cmd/feed.go @@ -2,7 +2,6 @@ package cmd import ( "database/sql" - "encoding/json" "fmt" "log" @@ -40,14 +39,14 @@ func init() { } func feed() { - var formatter func(core.Item) + var formatter func(core.Item) string switch feedFormat { case "headlines": - formatter = formatHeadline + formatter = core.FormatAsHeadline case "json": - formatter = formatJson + formatter = core.FormatAsJson case "short": - formatter = formatShort + formatter = core.FormatAsShort default: log.Fatalf("error: invalid format %s", feedFormat) } @@ -76,26 +75,6 @@ func feed() { } for _, item := range items { - formatter(item) + fmt.Println(formatter(item)) } } - -func formatHeadline(item core.Item) { - title := item.Title - if title == "" { - title = item.Id - } - fmt.Println(title) -} - -func formatJson(item core.Item) { - data, err := json.Marshal(item) - if err != nil { - log.Fatalf("error: failed to serialize %s/%s: %v", item.Source, item.Id, err) - } - fmt.Println(string(data)) -} - -func formatShort(item core.Item) { - fmt.Printf("%s/%s\n", item.Source, item.Id) -} diff --git a/core/db.go b/core/db.go index 2c87d67..d00221f 100644 --- a/core/db.go +++ b/core/db.go @@ -10,18 +10,6 @@ import ( _ "github.com/mattn/go-sqlite3" ) -type Item struct { - Source string `json:"source"` - Id string `json:"id"` - Created int `json:"created"` - Active bool `json:"active"` - Title string `json:"title"` - Author string `json:"author"` - Body string `json:"body"` - Link string `json:"link"` - Time int `json:"time"` -} - //go:embed sql/*.sql var migrations embed.FS @@ -172,15 +160,7 @@ func DeactivateItem(db *sql.DB, source string, id string) (bool, error) { func GetAllActiveItems(db *sql.DB) ([]Item, error) { rows, err := db.Query(` select - source, - id, - created, - active, - title, - author, - body, - link, - time + source, id, created, active, title, author, body, link, time from items where active <> 0 `) @@ -199,15 +179,7 @@ func GetAllActiveItems(db *sql.DB) ([]Item, error) { func GetActiveItemsForSource(db *sql.DB, source string) ([]Item, error) { rows, err := db.Query(` select - source, - id, - created, - active, - title, - author, - body, - link, - time + source, id, created, active, title, author, body, link, time from items where source = ? diff --git a/core/item.go b/core/item.go new file mode 100644 index 0000000..04c952c --- /dev/null +++ b/core/item.go @@ -0,0 +1,39 @@ +package core + +import ( + "encoding/json" + "fmt" + "log" +) + +type Item struct { + Source string `json:"source"` + Id string `json:"id"` + Created int `json:"created"` + Active bool `json:"active"` + Title string `json:"title"` + Author string `json:"author"` + Body string `json:"body"` + Link string `json:"link"` + Time int `json:"time"` +} + +func FormatAsHeadline(item Item) string { + title := item.Title + if title == "" { + title = item.Id + } + return title +} + +func FormatAsJson(item Item) string { + data, err := json.Marshal(item) + if err != nil { + log.Fatalf("error: failed to serialize %s/%s: %v", item.Source, item.Id, err) + } + return string(data) +} + +func FormatAsShort(item Item) string { + return fmt.Sprintf("%s/%s", item.Source, item.Id) +}