Skip to content

Commit 209d921

Browse files
committed
fix String() method
1 parent f1bbf57 commit 209d921

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const (
5454
// your application. Use the config.Duration type to support automatic unmarshal
5555
// from all sources. If you do not use a config file, do not use this type because
5656
// the environment unmarshaler supports time.Duration natively.
57-
type Duration struct{ time.Duration }
57+
type Duration struct{ time.Duration } //nolint:recvcheck // pointer receiver and non-pointer receiver is on purpose.
5858

5959
// UnmarshalText parses a duration type from a config file. This method works
6060
// with the Duration type to allow unmarshaling of durations from files and
@@ -71,17 +71,17 @@ func (d *Duration) UnmarshalText(b []byte) error {
7171
}
7272

7373
// MarshalText returns the string representation of a Duration. ie. 1m32s.
74-
func (d *Duration) MarshalText() ([]byte, error) {
75-
return []byte(d.Duration.String()), nil
74+
func (d Duration) MarshalText() ([]byte, error) {
75+
return []byte(d.String()), nil
7676
}
7777

7878
// MarshalJSON returns the string representation of a Duration for JSON. ie. "1m32s".
79-
func (d *Duration) MarshalJSON() ([]byte, error) {
80-
return []byte(`"` + d.Duration.String() + `"`), nil
79+
func (d Duration) MarshalJSON() ([]byte, error) {
80+
return []byte(`"` + d.String() + `"`), nil
8181
}
8282

8383
// String returns a Duration as string without trailing zero units.
84-
func (d *Duration) String() string {
84+
func (d Duration) String() string {
8585
dur := d.Duration.String()
8686
if len(dur) > 3 && dur[len(dur)-3:] == "m0s" {
8787
dur = dur[:len(dur)-2]

config_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cnfg_test
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"os"
67
"strconv"
@@ -79,19 +80,25 @@ func TestUnmarshalText(t *testing.T) {
7980
func TestUnmarshalJSON(t *testing.T) {
8081
t.Parallel()
8182

82-
d := cnfg.Duration{Duration: time.Minute + time.Hour}
83-
b, err := d.MarshalJSON()
83+
type testStruct struct {
84+
D cnfg.Duration `json:"d"`
85+
}
86+
87+
d := testStruct{D: cnfg.Duration{Duration: time.Hour + time.Minute}}
88+
b, err := json.Marshal(d)
8489

8590
require.NoError(t, err, "this method must not return an error")
86-
assert.Equal(t, []byte(`"1h1m0s"`), b)
91+
assert.JSONEq(t, `{"d":"1h1m"}`, string(b))
8792
}
8893

8994
func TestString(t *testing.T) {
9095
t.Parallel()
9196

9297
testDur := cnfg.Duration{Duration: time.Hour + time.Minute}
9398
assert.Equal(t, "1h1m", testDur.String())
99+
assert.Equal(t, "1h1m", fmt.Sprint(testDur))
94100

95101
testDur = cnfg.Duration{Duration: time.Hour}
96102
assert.Equal(t, "1h", testDur.String())
103+
assert.Equal(t, "1h", fmt.Sprint(testDur))
97104
}

0 commit comments

Comments
 (0)