Skip to content

Commit

Permalink
safely interface reflect values (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
purehyperbole authored Nov 23, 2021
1 parent 0773ab3 commit ad087c2
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions diff_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import "reflect"

func (d *Differ) diffBool(path []string, a, b reflect.Value, parent interface{}) error {
if a.Kind() == reflect.Invalid {
d.cl.Add(CREATE, path, nil, b.Interface())
d.cl.Add(CREATE, path, nil, exportInterface(b))
return nil
}

if b.Kind() == reflect.Invalid {
d.cl.Add(DELETE, path, a.Interface(), nil)
d.cl.Add(DELETE, path, exportInterface(a), nil)
return nil
}

Expand All @@ -22,7 +22,7 @@ func (d *Differ) diffBool(path []string, a, b reflect.Value, parent interface{})
}

if a.Bool() != b.Bool() {
d.cl.Add(UPDATE, path, a.Interface(), b.Interface(), parent)
d.cl.Add(UPDATE, path, exportInterface(a), exportInterface(b), parent)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions diff_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

func (d *Differ) diffFloat(path []string, a, b reflect.Value, parent interface{}) error {
if a.Kind() == reflect.Invalid {
d.cl.Add(CREATE, path, nil, b.Interface())
d.cl.Add(CREATE, path, nil, exportInterface(b))
return nil
}

if b.Kind() == reflect.Invalid {
d.cl.Add(DELETE, path, a.Interface(), nil)
d.cl.Add(DELETE, path, exportInterface(a), nil)
return nil
}

Expand All @@ -25,7 +25,7 @@ func (d *Differ) diffFloat(path []string, a, b reflect.Value, parent interface{}

if a.Float() != b.Float() {
if a.CanInterface() {
d.cl.Add(UPDATE, path, a.Interface(), b.Interface(), parent)
d.cl.Add(UPDATE, path, exportInterface(a), exportInterface(b), parent)
} else {
d.cl.Add(UPDATE, path, a.Float(), b.Float(), parent)
}
Expand Down
6 changes: 3 additions & 3 deletions diff_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

func (d *Differ) diffInt(path []string, a, b reflect.Value, parent interface{}) error {
if a.Kind() == reflect.Invalid {
d.cl.Add(CREATE, path, nil, b.Interface())
d.cl.Add(CREATE, path, nil, exportInterface(b))
return nil
}

if b.Kind() == reflect.Invalid {
d.cl.Add(DELETE, path, a.Interface(), nil)
d.cl.Add(DELETE, path, exportInterface(a), nil)
return nil
}

Expand All @@ -25,7 +25,7 @@ func (d *Differ) diffInt(path []string, a, b reflect.Value, parent interface{})

if a.Int() != b.Int() {
if a.CanInterface() {
d.cl.Add(UPDATE, path, a.Interface(), b.Interface(), parent)
d.cl.Add(UPDATE, path, exportInterface(a), exportInterface(b), parent)
} else {
d.cl.Add(UPDATE, path, a.Int(), b.Int(), parent)
}
Expand Down
12 changes: 6 additions & 6 deletions diff_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

func (d *Differ) diffSlice(path []string, a, b reflect.Value) error {
if a.Kind() == reflect.Invalid {
d.cl.Add(CREATE, path, nil, b.Interface())
d.cl.Add(CREATE, path, nil, exportInterface(b))
return nil
}

if b.Kind() == reflect.Invalid {
d.cl.Add(DELETE, path, a.Interface(), nil)
d.cl.Add(DELETE, path, exportInterface(a), nil)
return nil
}

Expand Down Expand Up @@ -56,7 +56,7 @@ func (d *Differ) diffSliceGeneric(path []string, a, b reflect.Value) error {
return nil
}

return d.diffComparative(path, missing, a.Interface())
return d.diffComparative(path, missing, exportInterface(a))
}

func (d *Differ) diffSliceComparative(path []string, a, b reflect.Value) error {
Expand All @@ -82,7 +82,7 @@ func (d *Differ) diffSliceComparative(path []string, a, b reflect.Value) error {
}
}

return d.diffComparative(path, c, a.Interface())
return d.diffComparative(path, c, exportInterface(a))
}

// keeps track of elements that have already been matched, to stop duplicate matches from occurring
Expand All @@ -100,7 +100,7 @@ func (st *sliceTracker) has(s, v reflect.Value) bool {
}

x := s.Index(i)
if reflect.DeepEqual(x.Interface(), v.Interface()) {
if reflect.DeepEqual(exportInterface(x), exportInterface(v)) {
(*st)[i] = true
return true
}
Expand All @@ -124,7 +124,7 @@ func hasAtSameIndex(s, v reflect.Value, atIndex int) bool {
// check the element in the slice at atIndex to see if it matches Value, if it is a valid index into the slice
if atIndex < s.Len() {
x := s.Index(atIndex)
return reflect.DeepEqual(x.Interface(), v.Interface())
return reflect.DeepEqual(exportInterface(x), exportInterface(v))
}

return false
Expand Down
6 changes: 3 additions & 3 deletions diff_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import "reflect"

func (d *Differ) diffString(path []string, a, b reflect.Value, parent interface{}) error {
if a.Kind() == reflect.Invalid {
d.cl.Add(CREATE, path, nil, b.Interface())
d.cl.Add(CREATE, path, nil, exportInterface(b))
return nil
}

if b.Kind() == reflect.Invalid {
d.cl.Add(DELETE, path, a.Interface(), nil)
d.cl.Add(DELETE, path, exportInterface(a), nil)
return nil
}

Expand All @@ -24,7 +24,7 @@ func (d *Differ) diffString(path []string, a, b reflect.Value, parent interface{
if a.String() != b.String() {
if a.CanInterface() {
// If a and/or b is of a type that is an alias for String, store that type in changelog
d.cl.Add(UPDATE, path, a.Interface(), b.Interface(), parent)
d.cl.Add(UPDATE, path, exportInterface(a), exportInterface(b), parent)
} else {
d.cl.Add(UPDATE, path, a.String(), b.String(), parent)
}
Expand Down
4 changes: 2 additions & 2 deletions diff_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (d *Differ) diffStruct(path []string, a, b reflect.Value) error {
continue
}

err := d.diff(fpath, af, bf, a.Interface())
err := d.diff(fpath, af, bf, exportInterface(a))
if err != nil {
return err
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (d *Differ) structValues(t string, path []string, a reflect.Value) error {
continue
}

err := nd.diff(fpath, xf, af, a.Interface())
err := nd.diff(fpath, xf, af, exportInterface(a))
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions diff_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (

func (d *Differ) diffTime(path []string, a, b reflect.Value) error {
if a.Kind() == reflect.Invalid {
d.cl.Add(CREATE, path, nil, b.Interface())
d.cl.Add(CREATE, path, nil, exportInterface(b))
return nil
}

if b.Kind() == reflect.Invalid {
d.cl.Add(DELETE, path, a.Interface(), nil)
d.cl.Add(DELETE, path, exportInterface(a), nil)
return nil
}

Expand All @@ -25,11 +25,11 @@ func (d *Differ) diffTime(path []string, a, b reflect.Value) error {
}

// Marshal and unmarshal time type will lose accuracy. Using unix nano to compare time type.
au := a.Interface().(time.Time).UnixNano()
bu := b.Interface().(time.Time).UnixNano()
au := exportInterface(a).(time.Time).UnixNano()
bu := exportInterface(b).(time.Time).UnixNano()

if au != bu {
d.cl.Add(UPDATE, path, a.Interface(), b.Interface())
d.cl.Add(UPDATE, path, exportInterface(a), exportInterface(b))
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions diff_uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

func (d *Differ) diffUint(path []string, a, b reflect.Value, parent interface{}) error {
if a.Kind() == reflect.Invalid {
d.cl.Add(CREATE, path, nil, b.Interface())
d.cl.Add(CREATE, path, nil, exportInterface(b))
return nil
}

if b.Kind() == reflect.Invalid {
d.cl.Add(DELETE, path, a.Interface(), nil)
d.cl.Add(DELETE, path, exportInterface(a), nil)
return nil
}

Expand All @@ -25,7 +25,7 @@ func (d *Differ) diffUint(path []string, a, b reflect.Value, parent interface{})

if a.Uint() != b.Uint() {
if a.CanInterface() {
d.cl.Add(UPDATE, path, a.Interface(), b.Interface(), parent)
d.cl.Add(UPDATE, path, exportInterface(a), exportInterface(b), parent)
} else {
d.cl.Add(UPDATE, path, a.Uint(), b.Uint(), parent)
}
Expand Down

0 comments on commit ad087c2

Please sign in to comment.