Skip to content

Commit

Permalink
zero checks + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Jun 21, 2024
1 parent abadaa0 commit a79298a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 29 deletions.
22 changes: 22 additions & 0 deletions explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package structexplorer
import (
"fmt"
"log/slog"
"reflect"
)

type objectAccess struct {
Expand Down Expand Up @@ -128,3 +129,24 @@ func (e *explorer) buildIndexData() indexData {
}
return b.data
}

func canExplore(v any) bool {
rt := reflect.TypeOf(v)
if rt.Kind() == reflect.Interface || rt.Kind() == reflect.Pointer {
rv := reflect.ValueOf(v)
if rv.IsZero() {
return false
}
rt = rt.Elem()
}
if rt.Kind() == reflect.Struct {
return true
}
if rt.Kind() == reflect.Slice {
return true
}
if rt.Kind() == reflect.Map {
return true
}
return false
}
7 changes: 7 additions & 0 deletions explorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ func TestExplorerTable(t *testing.T) {
t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want)
}
}

func TestCanExplorePointerNil(t *testing.T) {
var w *time.Time
if got, want := canExplore(w), false; got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
}
33 changes: 12 additions & 21 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (f fieldAccess) withPaddingTo(size int) fieldAccess {
return f
}

// pre: canExplore(v)
func newFields(v any) []fieldAccess {
list := []fieldAccess{}
if v == nil {
Expand All @@ -96,7 +97,8 @@ func newFields(v any) []fieldAccess {
var rt reflect.Type
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Interface || rv.Kind() == reflect.Pointer {
rt = rv.Elem().Type()
elm := rv.Elem()
rt = elm.Type()
} else {
rt = reflect.TypeOf(v)
}
Expand Down Expand Up @@ -202,6 +204,9 @@ func printString(v any) string {
}
return fmt.Sprintf("*%f", rv.Float())
case reflect.Value:
if !tv.IsValid() || tv.IsZero() {
return "~nil"
}
return "~" + printString(tv.Interface())
}
// can return string?
Expand All @@ -218,6 +223,12 @@ func printString(v any) string {
rv := reflect.ValueOf(v)
return fmt.Sprintf("%T (%d)", v, rv.Len())
}
if rt.Kind() == reflect.Pointer {
rv := reflect.ValueOf(v).Elem()
if !rv.IsValid() || rv.IsZero() {
return "nil"
}
}
return ellipsis(fmt.Sprintf("%[1]T", v))
}

Expand All @@ -228,26 +239,6 @@ func ellipsis(s string) string {
return s
}

func canExplore(v any) bool {
if v == nil {
return false
}
rt := reflect.TypeOf(v)
if rt.Kind() == reflect.Interface || rt.Kind() == reflect.Pointer {
rt = rt.Elem()
}
if rt.Kind() == reflect.Struct {
return true
}
if rt.Kind() == reflect.Slice {
return true
}
if rt.Kind() == reflect.Map {
return true
}
return false
}

func reflectMapKeyToString(key reflect.Value) string {
if key.Kind() == reflect.String {
s := key.String()
Expand Down
43 changes: 35 additions & 8 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ func TestFieldMapWithReflects(t *testing.T) {
m[reflect.ValueOf(1)] = reflect.ValueOf(2)
ks := reflectMapKeyToString(reflect.ValueOf(reflect.ValueOf(1)))
f := fieldAccess{owner: m, key: ks}
t.Log(f.value())
// TODO
val := valueAtAccessPath(m, []string{f.key})
t.Log(val)
if got, want := f.value().(reflect.Value).Int(), int64(2); got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
if got, want := valueAtAccessPath(m, []string{f.key}).(reflect.Value).Int(), int64(2); got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
}

func TestMapKeyAndBack(t *testing.T) {
Expand Down Expand Up @@ -227,10 +229,7 @@ func TestFieldsForMap(t *testing.T) {
1: 2, 3: 4,
}
l := newFields(m)
if got, want := len(l), 2; got != want && got != 4 { // sometimes it returns 4 TODO
t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want)
}
if got, want := l[0].value(), 2; got != want {
if got, want := len(l), 2; got != want {
t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want)
}
}
Expand Down Expand Up @@ -317,3 +316,31 @@ func TestPrintStringRequest(t *testing.T) {
t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want)
}
}
func TestPrintStringReflectValue1(t *testing.T) {
rv := reflect.ValueOf(1)
s := printString(rv)
if got, want := s, "~1"; got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
}
func TestPrintStringReflectValueNil(t *testing.T) {
rv := reflect.ValueOf(nil)
s := printString(rv)
if got, want := s, "~nil"; got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
}
func TestPrintStringReflectValueReflectValueNil(t *testing.T) {
rv := reflect.ValueOf(reflect.ValueOf(nil))
s := printString(rv)
if got, want := s, "~nil"; got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
}
func TestPrintStringReflectValueReflectValue2(t *testing.T) {
rv := reflect.ValueOf(reflect.ValueOf(2))
s := printString(rv)
if got, want := s, "~~2"; got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
}

0 comments on commit a79298a

Please sign in to comment.