Skip to content

Commit

Permalink
fix: use stdlib IsZero()
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Oct 12, 2022
1 parent cbd3392 commit dd6ede4
Showing 1 changed file with 1 addition and 19 deletions.
20 changes: 1 addition & 19 deletions repr.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (p *Printer) reprValue(seen map[reflect.Value]bool, v reflect.Value, indent
for i := 0; i < v.NumField(); i++ {
t := v.Type().Field(i)
f := v.Field(i)
if p.omitEmpty && isZero(f) {
if p.omitEmpty && f.IsZero() {
continue
}
fmt.Fprintf(p.w, "%s%s: ", ni, t.Name)
Expand Down Expand Up @@ -330,24 +330,6 @@ func Print(vs ...interface{}) {
New(os.Stdout, options...).Print(args...)
}

func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return v.IsNil()
}
return false
}

func timeToGo(w io.Writer, t time.Time) {
if t.IsZero() {
fmt.Fprint(w, "time.Time{}")
Expand Down

0 comments on commit dd6ede4

Please sign in to comment.