Skip to content

Commit

Permalink
fix: 修复获取迭代值时会默认将指针转换成内存值的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
ahl5esoft committed Apr 30, 2021
1 parent 76bd28f commit 330857b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
6 changes: 5 additions & 1 deletion field.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func FieldValue(name string) GetFieldValueFunc {
var getter GetFieldValueFunc
getter = func(item interface{}) reflect.Value {
itemRV := getRealValue(item)
if itemRV.Kind() == reflect.Ptr {
itemRV = itemRV.Elem()
}

itemRT := itemRV.Type()
for i := 0; i < itemRT.NumField(); i++ {
field := itemRT.Field(i)
Expand All @@ -33,7 +37,7 @@ func FieldValue(name string) GetFieldValueFunc {
}
}

if strings.ToLower(name) == strings.ToLower(field.Name) {
if strings.EqualFold(name, field.Name) {
return itemRV.Field(i)
}
}
Expand Down
37 changes: 26 additions & 11 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,32 @@ import (
)

func Test_Map(t *testing.T) {
src := []string{"11", "12", "13"}
var res []int
Chain(src).Map(func(s string, _ int) int {
n, _ := strconv.Atoi(s)
return n
}).Value(&res)
assert.EqualValues(
t,
res,
[]int{11, 12, 13},
)
t.Run("ok", func(t *testing.T) {
src := []string{"11", "12", "13"}
var res []int
Chain(src).Map(func(s string, _ int) int {
n, _ := strconv.Atoi(s)
return n
}).Value(&res)
assert.EqualValues(
t,
res,
[]int{11, 12, 13},
)
})

t.Run("ptr", func(t *testing.T) {
src := []string{"11", "12", "13"}
var res []*string
Chain(src).Map(func(r string, _ int) *string {
return &r
}).Value(&res)
assert.EqualValues(
t,
res,
[]*string{&src[0], &src[1], &src[2]},
)
})
}

func Test_MapBy(t *testing.T) {
Expand Down
6 changes: 0 additions & 6 deletions query.go

This file was deleted.

4 changes: 0 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ func getRealValue(v interface{}) reflect.Value {
value = v.(reflect.Value)
}

if value.Kind() == reflect.Ptr {
value = value.Elem()
}

if value.Type() == facadeType {
value = value.Interface().(facade).Real
}
Expand Down

0 comments on commit 330857b

Please sign in to comment.