Skip to content

Commit 61ca8aa

Browse files
committed
feat: support FormatTime.
1 parent 3d1485f commit 61ca8aa

11 files changed

+75
-3
lines changed

dump.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"sort"
1212
"strconv"
1313
"strings"
14+
"time"
1415
)
1516

1617
var (
@@ -40,6 +41,9 @@ type Options struct {
4041
// when it's safe. This is useful for diffing two structures, where pointer variables would cause
4142
// false changes. However, circular graphs are still detected and elided to avoid infinite output.
4243
DisablePointerReplacement bool
44+
45+
// FormatTime, if true, will format [time.Time] values.
46+
FormatTime bool
4347
}
4448

4549
// Config is the default config used when calling Dump
@@ -59,6 +63,7 @@ type dumpState struct {
5963
parentPointers ptrmap
6064
currentPointer *ptrinfo
6165
homePackageRegexp *regexp.Regexp
66+
timeFormatter func(t time.Time) string
6267
}
6368

6469
func (s *dumpState) write(b []byte) {
@@ -129,6 +134,12 @@ func (s *dumpState) dumpSlice(v reflect.Value) {
129134
}
130135

131136
func (s *dumpState) dumpStruct(v reflect.Value) {
137+
val := v.Interface()
138+
if t, ok := val.(time.Time); ok && s.timeFormatter != nil {
139+
s.writeString(s.timeFormatter(t))
140+
return
141+
}
142+
132143
dumpPreamble := func() {
133144
s.dumpType(v)
134145
s.write([]byte("{"))
@@ -246,7 +257,6 @@ func (s *dumpState) dumpChan(v reflect.Value) {
246257
}
247258

248259
func (s *dumpState) dumpCustom(v reflect.Value, buf *bytes.Buffer) {
249-
250260
// Dump the type
251261
s.dumpType(v)
252262

@@ -293,6 +303,8 @@ func (s *dumpState) dump(value interface{}) {
293303
s.dumpVal(v)
294304
}
295305

306+
var dumperType = reflect.TypeOf((*Dumper)(nil)).Elem()
307+
296308
func (s *dumpState) descendIntoPossiblePointer(value reflect.Value, f func()) {
297309
canonicalize := true
298310
if isPointerValue(value) {
@@ -345,7 +357,6 @@ func (s *dumpState) dumpVal(value reflect.Value) {
345357
}
346358

347359
// Handle custom dumpers
348-
dumperType := reflect.TypeOf((*Dumper)(nil)).Elem()
349360
if v.Type().Implements(dumperType) {
350361
s.descendIntoPossiblePointer(v, func() {
351362
// Run the custom dumper buffering the output
@@ -464,6 +475,16 @@ func newDumpState(value reflect.Value, options *Options, writer io.Writer) *dump
464475
w: writer,
465476
}
466477

478+
if options.FormatTime {
479+
result.timeFormatter = func(t time.Time) string {
480+
t = t.In(time.UTC)
481+
return fmt.Sprintf(
482+
`time.Date(%d, %d, %d, %d, %d, %d, %d, time.UTC)`,
483+
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(),
484+
)
485+
}
486+
}
487+
467488
if options.HomePackage != "" {
468489
result.homePackageRegexp = regexp.MustCompile(fmt.Sprintf("\\b%s\\.", options.HomePackage))
469490
}

dump_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/exec"
99
"reflect"
1010
"testing"
11+
"time"
1112

1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
@@ -163,6 +164,7 @@ func TestSdump_config(t *testing.T) {
163164
(func(v IntAlias) *IntAlias { return &v })(20),
164165
litter.Dump,
165166
func(s string, i int) (bool, error) { return false, nil },
167+
time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC),
166168
}
167169

168170
runTestWithCfg(t, "config_Compact", &litter.Options{
@@ -204,6 +206,9 @@ func TestSdump_config(t *testing.T) {
204206
return false
205207
},
206208
}, data)
209+
runTestWithCfg(t, "config_FormatTime", &litter.Options{
210+
FormatTime: true,
211+
}, data)
207212

208213
basic := &BasicStruct{1, 2}
209214
runTestWithCfg(t, "config_DisablePointerReplacement_simpleReusedStruct", &litter.Options{

testdata/config_Compact.dump

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[]interface{}{litter_test.options{Compact:false,StripPackageNames:false,HidePrivateFields:true,HomePackage:"",Separator:" ",StrictGo:false},&litter_test.BasicStruct{Public:1,private:2},litter_test.Function,&20,&20,litter.Dump,func(string,int)(bool,error)}
1+
[]interface{}{litter_test.options{Compact:false,StripPackageNames:false,HidePrivateFields:true,HomePackage:"",Separator:" ",StrictGo:false},&litter_test.BasicStruct{Public:1,private:2},litter_test.Function,&20,&20,litter.Dump,func(string,int)(bool,error),time.Time{wall:0,ext:63650361600,loc:nil}}

testdata/config_DumpFunc.dump

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@
1616
&20,
1717
litter.Dump,
1818
func(string, int) (bool, error),
19+
time.Time{
20+
wall: 0,
21+
ext: 63650361600,
22+
loc: nil,
23+
},
1924
}

testdata/config_FieldFilter.dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
&20,
1010
litter.Dump,
1111
func(string, int) (bool, error),
12+
time.Time{},
1213
}

testdata/config_FormatTime.dump

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[]interface {}{
2+
litter_test.options{
3+
Compact: false,
4+
StripPackageNames: false,
5+
HidePrivateFields: true,
6+
HomePackage: "",
7+
Separator: " ",
8+
StrictGo: false,
9+
},
10+
&litter_test.BasicStruct{
11+
Public: 1,
12+
private: 2,
13+
},
14+
litter_test.Function,
15+
&20,
16+
&20,
17+
litter.Dump,
18+
func(string, int) (bool, error),
19+
time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC),
20+
}

testdata/config_HidePrivateFields.dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
&20,
1616
litter.Dump,
1717
func(string, int) (bool, error),
18+
time.Time{},
1819
}

testdata/config_HideZeroValues.dump

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
&20,
1313
litter.Dump,
1414
func(string, int) (bool, error),
15+
time.Time{
16+
wall: 0,
17+
ext: 63650361600,
18+
},
1519
}

testdata/config_HomePackage.dump

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@
1616
&20,
1717
litter.Dump,
1818
func(string, int) (bool, error),
19+
time.Time{
20+
wall: 0,
21+
ext: 63650361600,
22+
loc: nil,
23+
},
1924
}

testdata/config_StrictGo.dump

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@
1616
(func(v litter_test.IntAlias) *litter_test.IntAlias { return &v })(20),
1717
litter.Dump,
1818
func(string, int) (bool, error),
19+
time.Time{
20+
wall: 0,
21+
ext: 63650361600,
22+
loc: nil,
23+
},
1924
}

0 commit comments

Comments
 (0)