Skip to content

Commit

Permalink
Remove panic hangover in Dereference from initial development.
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckpreslar committed Sep 5, 2014
1 parent 55a4cdc commit c049786
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
18 changes: 13 additions & 5 deletions tranq.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (i UninterfaceabledValueError) Error() string {

// Dereference attempts to dereference the provided paramter `i`
// from reflect.Kind's of reflect.Ptr and reflect.Interface.
func Dereference(i interface{}) (reflect.Value, reflect.Kind, reflect.Type) {
func Dereference(i interface{}) (reflect.Value, reflect.Kind, reflect.Type, error) {
var (
v reflect.Value
k reflect.Kind
Expand All @@ -50,12 +50,12 @@ func Dereference(i interface{}) (reflect.Value, reflect.Kind, reflect.Type) {
return Dereference(v.Interface())
}

panic(UninterfaceabledValueError{v})
return reflect.Value{}, reflect.Invalid, nil, UninterfaceabledValueError{v}
}

t = v.Type()

return v, k, t
return v, k, t, nil
}

// TypeName attempts to resolve parameter `i`'s type name,
Expand All @@ -66,10 +66,14 @@ func TypeName(i interface{}) (string, error) {
o bool
t reflect.Type
k reflect.Kind
e error
)

if t, o = i.(reflect.Type); !o {
_, k, t = Dereference(i)
_, k, t, e = Dereference(i)
if nil != e {
return "", e
}
} else {
k = t.Kind()
}
Expand Down Expand Up @@ -129,7 +133,11 @@ type compiler struct {
}

func (cr compiler) compile(i interface{}, c, m int) (interface{}, error) {
var v, k, t = Dereference(i)
var v, k, t, e = Dereference(i)

if nil != e {
return nil, e
}

if isBaseKind(k) {
if v.CanInterface() {
Expand Down
18 changes: 9 additions & 9 deletions tranq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,45 +76,45 @@ func TestUninterfaceabledValueError(t *testing.T) {
}

func TestSimpleStructDereference(t *testing.T) {
var _, kind, _ = tranq.Dereference(TStruct{})
var _, kind, _, _ = tranq.Dereference(TStruct{})

assert.Equal(t, kind, reflect.Struct, "failed to result in reflect.Kind of reflect.Struct")
}

func TestPointerStructDereference(t *testing.T) {
var _, kind, _ = tranq.Dereference(&TStruct{})
var _, kind, _, _ = tranq.Dereference(&TStruct{})

assert.Equal(t, kind, reflect.Struct, "failed to result in reflect.Kind of reflect.Struct")
}

func TestComplexPointerStructDereference(t *testing.T) {
var (
tstruct = &TStruct{}
_, kind, _ = tranq.Dereference(&tstruct)
tstruct = &TStruct{}
_, kind, _, _ = tranq.Dereference(&tstruct)
)

assert.Equal(t, kind, reflect.Struct, "failed to result in reflect.Kind of reflect.Struct")
}

func TestSimpleCollectionDereference(t *testing.T) {
var _, kind, _ = tranq.Dereference([]TStruct{})
var _, kind, _, _ = tranq.Dereference([]TStruct{})

assert.Equal(t, kind, reflect.Slice, "failed to result in reflect.Kind of reflect.Slice")
}

func TestPointerCollectionDereference(t *testing.T) {
var (
tstructs = []*TStruct{}
_, kind, _ = tranq.Dereference(tstructs)
tstructs = []*TStruct{}
_, kind, _, _ = tranq.Dereference(tstructs)
)

assert.Equal(t, kind, reflect.Slice, "failed to result in reflect.Kind of reflect.Slice")
}

func TestComplexPointerCollectionDereference(t *testing.T) {
var (
tstructs = []*TStruct{}
_, kind, _ = tranq.Dereference(&tstructs)
tstructs = []*TStruct{}
_, kind, _, _ = tranq.Dereference(&tstructs)
)

assert.Equal(t, kind, reflect.Slice, "failed to result in reflect.Kind of reflect.Slice")
Expand Down

0 comments on commit c049786

Please sign in to comment.