have output() return errors on null data

This commit is contained in:
filifa 2024-03-19 20:31:32 -05:00
parent 106af3222e
commit 66c1962999
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() {