fix unmarshal big time durations

master
bel 2023-04-04 19:15:28 -06:00
parent 0ffe5f797a
commit 5b2d89a73a
2 changed files with 52 additions and 48 deletions

57
db.go
View File

@ -1,6 +1,10 @@
package main package main
import ( import (
"errors"
"fmt"
"log"
"strconv"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@ -8,17 +12,18 @@ import (
type ( type (
db struct { db struct {
Knowledge Knowledge Knowledge knowledge
Users map[string]User Users map[string]user
Cadence []Duration Cadence []duration
} }
Knowledge struct { knowledge struct {
Questions map[string]Question Questions map[string]Question
Answers map[string]Answer Answers map[string]Answer
} }
User struct { user struct {
History map[string][]History History map[string][]History
} }
duration time.Duration
) )
func (db db) HistoryOf(user string) map[string][]History { func (db db) HistoryOf(user string) map[string][]History {
@ -84,3 +89,45 @@ func (db db) lastTS(user, q string) time.Time {
} }
return time.Unix(0, max) return time.Unix(0, max)
} }
func (d duration) MarshalYAML() (interface{}, error) {
log.Println("marshalling duration", time.Duration(d))
return time.Duration(d).String(), nil
}
func (d *duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
count := ""
var ttl time.Duration
for i := range s {
if s[i] < '0' || s[i] > '9' {
n, err := strconv.Atoi(count)
if err != nil {
return err
}
count += s[i : i+1]
switch s[i] {
case 'w':
count = fmt.Sprintf("%dh", n*24*7)
case 'd':
count = fmt.Sprintf("%dh", n*24)
}
d, err := time.ParseDuration(count)
if err != nil {
return err
}
ttl += d
count = ""
} else {
count += s[i : i+1]
}
}
if count != "" {
return errors.New(count)
}
*d = duration(ttl)
return nil
}

43
main.go
View File

@ -2,11 +2,9 @@ package main
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"log" "log"
"os" "os"
"strconv"
"time" "time"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -36,7 +34,6 @@ type (
TS int64 TS int64
Pass bool Pass bool
} }
Duration time.Duration
) )
func main() { func main() {
@ -91,43 +88,3 @@ func NewDB() (DB, error) {
} }
return db, nil return db, nil
} }
func (d Duration) MarshalYAML() (interface{}, error) {
return time.Duration(d).String(), nil
}
func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
count := ""
var ttl time.Duration
for i := range s {
if s[i] < '0' || s[i] > '9' {
n, err := strconv.Atoi(count)
if err != nil {
return err
}
count += s[i : i+1]
switch s[i] {
case 'w':
count = fmt.Sprintf("%dh", n*24*7)
case 'd':
count = fmt.Sprintf("%dh", n*24)
}
d, err := time.ParseDuration(count)
if err != nil {
return err
}
ttl += d
count = ""
} else {
count += s[i : i+1]
}
}
if count != "" {
return errors.New(count)
}
return nil
}