have output() return errors on null data

This commit is contained in:
Nick Griffey 2024-02-24 00:47:45 -06:00
parent 35cd759205
commit fdd7a7bb98
1 changed files with 11 additions and 15 deletions

26
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"database/sql" "database/sql"
"errors"
"flag" "flag"
"fmt" "fmt"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@ -70,31 +71,26 @@ func (db *TimelineDB) closestEvents(t int64, n int) ([]EventsRow, error) {
} }
func (event *EventsRow) Output() (int64, string, string, error) { func (event *EventsRow) Output() (int64, string, string, error) {
timestamp, err := event.timestamp.Value() if !event.timestamp.Valid {
if err != nil { return 0, "", "", errors.New("null timestamp")
return 0, "", "", err } else if !event.description.Valid {
return 0, "", "", errors.New("null description")
} }
desc, err := event.description.Value() timestamp := event.timestamp.Int64
if err != nil { desc := event.description.String
return 0, "", "", err yearknown := event.yearknown.Valid && event.yearknown.Bool
}
yearknown, err := event.yearknown.Value()
if err != nil {
return 0, "", "", err
}
var ago string var ago string
date := time.Unix(timestamp.(int64), 0) date := time.Unix(timestamp, 0)
if yearknown == nil || !yearknown.(bool) { if !yearknown {
yeardiff := time.Now().Year() - date.Year() yeardiff := time.Now().Year() - date.Year()
ago = strconv.Itoa(yeardiff) + " years ago" ago = strconv.Itoa(yeardiff) + " years ago"
} else { } else {
ago = date.String() ago = date.String()
} }
return timestamp.(int64), ago, desc.(string), nil return timestamp, ago, desc, nil
} }
func main() { func main() {