follow suggestions.

Co-authored-by: Oleg Lobanov <oleg@lobanov.me>
This commit is contained in:
qisfj 2022-10-07 20:23:49 +08:00
parent 36f1ac5059
commit 0752b4b5b1
2 changed files with 18 additions and 16 deletions

View File

@ -3,6 +3,7 @@ package settings
import (
"crypto/rand"
"encoding/json"
"strconv"
"strings"
"time"
@ -71,7 +72,7 @@ func GenerateKey() ([]byte, error) {
type Duration time.Duration // support json Marshal/Unmarshal for time.Duration
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 {

View File

@ -29,6 +29,7 @@ func TestDuration(t *testing.T) {
{"yaml", yaml.Marshal, yaml.Unmarshal},
}
for _, tc := range testCases {
t.Run(tc.str, func(t *testing.T) {
for _, codec := range codecs {
t.Run(codec.name, func(t *testing.T) {
// str --> dur --> mid_str(may different from str) --> dur
@ -36,7 +37,6 @@ func TestDuration(t *testing.T) {
err := codec.unmarshal([]byte(tc.str), &dur)
require.NoError(t, err)
require.Equal(t, tc.value, dur)
midStr, err := codec.marshal(dur)
require.NoError(t, err)
err = codec.unmarshal(midStr, &dur)
@ -44,5 +44,6 @@ func TestDuration(t *testing.T) {
require.Equal(t, tc.value, dur)
})
}
})
}
}