Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package internal

import (
"time"
)

type StructA struct {
String1 string
string2 string
Time1 time.Time
time2 time.Time
}

func MakeStructA(s string, t time.Time) StructA {
return StructA{
String1: s,
string2: s + "2",
Time1: t,
time2: t.Add(2 * time.Hour),
}
}
52 changes: 29 additions & 23 deletions repr.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,33 +233,39 @@ func (p *Printer) reprValue(seen map[reflect.Value]bool, v reflect.Value, indent
fmt.Fprintf(p.w, "%s}", in)

case reflect.Struct:
if td, ok := v.Interface().(time.Time); ok {
timeToGo(p.w, td)
} else {
if showType {
fmt.Fprintf(p.w, "%s{", v.Type())
} else {
fmt.Fprint(p.w, "{")

if v.CanInterface() {
switch tv := v.Interface().(type) {
case time.Time:
timeToGo(p.w, tv)
return
}
if p.indent != "" && v.NumField() != 0 {
fmt.Fprintf(p.w, "\n")
}

if showType {
fmt.Fprintf(p.w, "%s{", v.Type())
} else {
fmt.Fprint(p.w, "{")
}
if p.indent != "" && v.NumField() != 0 {
fmt.Fprintf(p.w, "\n")
}
for i := 0; i < v.NumField(); i++ {
t := v.Type().Field(i)
f := v.Field(i)
if p.omitEmpty && isZero(f) {
continue
}
for i := 0; i < v.NumField(); i++ {
t := v.Type().Field(i)
f := v.Field(i)
if p.omitEmpty && isZero(f) {
continue
}
fmt.Fprintf(p.w, "%s%s: ", ni, t.Name)
p.reprValue(seen, f, ni, true)
if p.indent != "" {
fmt.Fprintf(p.w, ",\n")
} else if i < v.NumField()-1 {
fmt.Fprintf(p.w, ", ")
}
fmt.Fprintf(p.w, "%s%s: ", ni, t.Name)
p.reprValue(seen, f, ni, true)
if p.indent != "" {
fmt.Fprintf(p.w, ",\n")
} else if i < v.NumField()-2 {
fmt.Fprintf(p.w, ", ")
}
fmt.Fprintf(p.w, "%s}", indent)
}
fmt.Fprintf(p.w, "%s}", indent)

case reflect.Ptr:
if v.IsNil() {
fmt.Fprintf(p.w, "nil")
Expand Down
11 changes: 10 additions & 1 deletion repr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"testing"
"time"

"github.com/alecthomas/repr/internal"
)

func equal(t *testing.T, want, have string) {
Expand Down Expand Up @@ -185,7 +187,7 @@ func TestRecursiveIssue3(t *testing.T) {
child := &data{}
root := &data{children: []*data{child}}
child.parent = root
want := "&repr.data{children: []*repr.data{{parent: &..., }}}"
want := "&repr.data{children: []*repr.data{{parent: &...}}}"
have := String(root)
equal(t, want, have)
}
Expand All @@ -209,3 +211,10 @@ func TestReprPrivateBytes(t *testing.T) {
equal(t, "repr.MyBuffer{buf: &bytes.Buffer{buf: []byte(\"Hi th3re!\"), }}", s)
}
}

func TestReprInternalTime(t *testing.T) {
obj := internal.MakeStructA("s", time.Date(2020, 12, 1, 10, 13, 45, 0, time.UTC))
s := String(obj)

equal(t, "internal.StructA{String1: \"s\", string2: \"s2\", Time1: time.Date(2020, 12, 1, 10, 13, 45, 0, time.UTC)time2: time.Time{ext: 63742421625}}", s)
}