Skip to content

Commit

Permalink
- Adding new API for elem ✨
Browse files Browse the repository at this point in the history
- Updating tests for the new API ✅
  • Loading branch information
fairyhunter13 committed Nov 17, 2020
1 parent d54d7a7 commit 67e9058
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
14 changes: 14 additions & 0 deletions elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ func GetChildElem(val reflect.Value) (res reflect.Value) {
return
}

// GetChildPtrElem is similar with GetChildElem but the function stops when the elem is ptr and the elem of that ptr is non ptr.
func GetChildPtrElem(val reflect.Value) (res reflect.Value) {
res = val
var tempRes reflect.Value
for IsKindValueElemableParentElem(res) {
tempRes = GetElem(res)
if res == tempRes {
return
}
res = tempRes
}
return
}

// GetChildNilElem is similar with GetChildElem but it uses GetNilElem function.
func GetChildNilElem(val reflect.Value) (res reflect.Value) {
res = val
Expand Down
8 changes: 1 addition & 7 deletions elem_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ func GetInitChildElem(val reflect.Value) (res reflect.Value) {
return
}

// GetInitChildPtrElem is similar with GetInitChildElem but the function stops when the elem is ptr and the elem of ptr is non ptr.
// GetInitChildPtrElem is similar with GetInitChildElem but the function stops when the elem is ptr and the elem of that ptr is non ptr.
func GetInitChildPtrElem(val reflect.Value) (res reflect.Value) {
res = val

var tempRes reflect.Value
for IsKindValueElemableParentElem(res) {
tempRes = GetInitElem(res)
Expand All @@ -51,8 +50,3 @@ func GetInitChildPtrElem(val reflect.Value) (res reflect.Value) {
}
return
}

// IsKindValueElemableParentElem checks whether the res have elemable kind for parent and elem.
func IsKindValueElemableParentElem(res reflect.Value) bool {
return IsKindValueElemable(GetKind(res)) && IsKindValueElemable(GetElemKind(res))
}
47 changes: 47 additions & 0 deletions elem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,50 @@ func TestGetChildNilElem(t *testing.T) {
})
}
}

func TestGetChildPtrElem(t *testing.T) {
type args struct {
val func() reflect.Value
}
tests := []struct {
name string
args args
wantRes func() reflect.Value
}{
{
name: "valid multi ptr string",
args: args{
val: func() reflect.Value {
var x **string
return reflect.ValueOf(x)
},
},
wantRes: func() reflect.Value {
var test **string
return reflect.ValueOf(test)
},
},
{
name: "valid multi ptr string",
args: args{
val: func() reflect.Value {
test := "hello"
hello := &test
hi := &hello
return reflect.ValueOf(&hi)
},
},
wantRes: func() reflect.Value {
var test *string
return reflect.ValueOf(test)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotRes := GetChildPtrElem(tt.args.val()); !(gotRes.Type() == tt.wantRes().Type()) {
t.Errorf("GetChildPtrElem() = %v, want %v", gotRes.Type(), tt.wantRes().Type())
}
})
}
}
5 changes: 5 additions & 0 deletions kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func IsKindValueElemable(kind reflect.Kind) bool {
return kind == reflect.Ptr || kind == reflect.Interface
}

// IsKindValueElemableParentElem checks whether the res have elemable kind for parent and elem.
func IsKindValueElemableParentElem(res reflect.Value) bool {
return IsKindValueElemable(GetKind(res)) && IsKindValueElemable(GetElemKind(res))
}

// IsKindTypeElemable checks the kind of reflect.Type that can call Elem method.
func IsKindTypeElemable(kind reflect.Kind) bool {
return kind == reflect.Array ||
Expand Down

0 comments on commit 67e9058

Please sign in to comment.