follow suggestions.
Co-authored-by: Oleg Lobanov <oleg@lobanov.me>
This commit is contained in:
parent
36f1ac5059
commit
0752b4b5b1
@ -3,6 +3,7 @@ package settings
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -71,7 +72,7 @@ func GenerateKey() ([]byte, error) {
|
|||||||
type Duration time.Duration // support json Marshal/Unmarshal for time.Duration
|
type Duration time.Duration // support json Marshal/Unmarshal for time.Duration
|
||||||
|
|
||||||
func (dur Duration) MarshalJSON() ([]byte, error) {
|
func (dur Duration) MarshalJSON() ([]byte, error) {
|
||||||
return []byte("\"" + time.Duration(dur).String() + "\""), nil
|
return []byte(strconv.Quote(time.Duration(dur).String())), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dur *Duration) UnmarshalJSON(data []byte) error {
|
func (dur *Duration) UnmarshalJSON(data []byte) error {
|
||||||
|
|||||||
@ -29,20 +29,21 @@ func TestDuration(t *testing.T) {
|
|||||||
{"yaml", yaml.Marshal, yaml.Unmarshal},
|
{"yaml", yaml.Marshal, yaml.Unmarshal},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
for _, codec := range codecs {
|
t.Run(tc.str, func(t *testing.T) {
|
||||||
t.Run(codec.name, func(t *testing.T) {
|
for _, codec := range codecs {
|
||||||
// str --> dur --> mid_str(may different from str) --> dur
|
t.Run(codec.name, func(t *testing.T) {
|
||||||
var dur Duration
|
// str --> dur --> mid_str(may different from str) --> dur
|
||||||
err := codec.unmarshal([]byte(tc.str), &dur)
|
var dur Duration
|
||||||
require.NoError(t, err)
|
err := codec.unmarshal([]byte(tc.str), &dur)
|
||||||
require.Equal(t, tc.value, dur)
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, tc.value, dur)
|
||||||
midStr, err := codec.marshal(dur)
|
midStr, err := codec.marshal(dur)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
err = codec.unmarshal(midStr, &dur)
|
err = codec.unmarshal(midStr, &dur)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, tc.value, dur)
|
require.Equal(t, tc.value, dur)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user