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 (
"database/sql"
"errors"
"flag"
"fmt"
_ "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) {
timestamp, err := event.timestamp.Value()
if err != nil {
return 0, "", "", err
if !event.timestamp.Valid {
return 0, "", "", errors.New("null timestamp")
} else if !event.description.Valid {
return 0, "", "", errors.New("null description")
}
desc, err := event.description.Value()
if err != nil {
return 0, "", "", err
}
yearknown, err := event.yearknown.Value()
if err != nil {
return 0, "", "", err
}
timestamp := event.timestamp.Int64
desc := event.description.String
yearknown := event.yearknown.Valid && event.yearknown.Bool
var ago string
date := time.Unix(timestamp.(int64), 0)
if yearknown == nil || !yearknown.(bool) {
date := time.Unix(timestamp, 0)
if !yearknown {
yeardiff := time.Now().Year() - date.Year()
ago = strconv.Itoa(yeardiff) + " years ago"
} else {
ago = date.String()
}
return timestamp.(int64), ago, desc.(string), nil
return timestamp, ago, desc, nil
}
func main() {