Improve encoding tests

This commit is contained in:
andig 2023-02-25 13:09:37 +01:00
parent 4642a135db
commit db98a34e23
2 changed files with 16 additions and 16 deletions

View file

@ -95,11 +95,11 @@ func encode(v interface{}) (string, error) {
var s string
switch val := v.(type) {
case time.Time:
var b []byte
if !val.IsZero() {
b, _ = val.Round(time.Second).Local().MarshalText()
if val.IsZero() {
s = "null"
} else {
s = fmt.Sprintf(`"%s"`, val.Format(time.RFC3339))
}
s = fmt.Sprintf(`"%s"`, string(b))
case time.Duration:
// must be before stringer to convert to seconds instead of string
s = fmt.Sprintf("%d", int64(val.Seconds()))
@ -112,6 +112,8 @@ func encode(v interface{}) (string, error) {
default:
if b, err := json.Marshal(v); err == nil {
s = string(b)
} else {
return "", err
}
}
return s, nil