Merge pull request #184 from unistack-org/duration

util/time: add Marshal/Unmarshal to own Duration
This commit is contained in:
Василий Толстов 2023-02-13 14:05:16 +03:00 committed by GitHub
commit 3ba8cb7f9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package time package time
import ( import (
"encoding/json"
"fmt" "fmt"
"time" "time"
) )
@ -46,3 +47,36 @@ func ParseDuration(s string) (time.Duration, error) {
return td, err return td, err
} }
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
*d = Duration(time.Duration(value))
return nil
case string:
dv, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(dv)
return nil
default:
return fmt.Errorf("invalid duration")
}
}
/*
func (d Duration) MarshalYAML() (interface{}, error) {
return nil, nil
}
func (d Duration) UnmarshalYAML(fn func(interface{}) error) error
*/

View File

@ -1,10 +1,37 @@
package time package time
import ( import (
"bytes"
"encoding/json"
"testing" "testing"
"time" "time"
) )
func TestMarshalJSON(t *testing.T) {
d := Duration(10000000)
buf, err := json.Marshal(d)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, []byte(`"10ms"`)) {
t.Fatalf("invalid duration: %s != %s", buf, `"10ms"`)
}
}
func TestUnmarshalJSON(t *testing.T) {
type str struct {
TTL Duration `json:"ttl"`
}
v := &str{}
err := json.Unmarshal([]byte(`{"ttl":"10ms"}`), v)
if err != nil {
t.Fatal(err)
} else if v.TTL != 10000000 {
t.Fatalf("invalid duration %v != 10000000", v.TTL)
}
}
func TestParseDuration(t *testing.T) { func TestParseDuration(t *testing.T) {
var td time.Duration var td time.Duration
var err error var err error