doupuxuan5784
doupuxuan5784
2018-09-27 14:01

Reflect.Value问题

已采纳

In trying to test this business func:

//IsInSlice works like Array.prototype.find in JavaScript, except it
// returns -1 if `value` is not found. (Also, Array.prototype.find takes
// function, and IsInSlice takes `value` and `list`)
func IsInSlice(value interface{}, list interface{}) int {
    slice := reflect.ValueOf(list)

    for i := 0; i < slice.Len(); i++ {
        if slice.Index(i) == value {
            return i
        }
    }
    return -1
}

I find that it fails my sanity tests:

func TestIsInSlice(t *testing.T) {
    digits := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    slice := digits[3:8] // returns {3,4,5,6,7}

    type args struct {
        value interface{}
        list  interface{}
    }
    tests := []struct {
        name string
        args args
        want int
    }{
        {
            name: "SanityTest",
            args: args{value: 3,
                list: []int{3, 4, 5, 6, 7},
            },
            want: 0,
        },
        {
            name: "ElementAtEnd",
            args: args{
                value: 5,
                list:  slice,
            },
            want: 3,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := IsInSlice(tt.args.value, tt.args.list); got != tt.want {
                t.Errorf("IsInSlice() = %v, want %v", got, tt.want)
            }
        })
    }

}

The person responsible for fixing these bugs has no idea what is causing the bug, let alone how to fix it, and neither do me or the senior dev. So, I attempted to isolate the problem to try to identify it.

What I thought it was

When I logged the bugs, I thought they were because somehow, the value was being compared to the reflect.Value returned by slice.Index(i). I tried

reflect.DeepEqual(slice.Index(i), value)

but that fails. The only way I could get passing test is to use Int() to extract the value and use

var i int64 = 3

instead of the literal 3, which is flaky af.

What is the issue and how do we fix it?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dqy92287 dqy92287 3年前

    The Index() method returns a reflect.Value. Use that value's Interface() method to get its underlying value and compare to that:

    func IsInSlice(value interface{}, list interface{}) int {
        slice := reflect.ValueOf(list)
        for i := 0; i < slice.Len(); i++ {
            if slice.Index(i).Interface() == value {
                return i
            }
        }
        return -1
    }
    
    点赞 评论 复制链接分享
  • dongyingtang3803 dongyingtang3803 3年前

    Value.Index() returns a reflect.Value. Comparing this to the interface{} you receive will never return true. You'd need to compare against the "real" value by using slice.Index(i).Interface().

    However, you're messing with black magic here. You're missing a lot of safety checks - what if list isn't actually a slice or array? What if value isn't of the same type as list's elements? Take a look at the implementation of DeepEqual to see the kind of checks you might want to perform for a function like this: https://golang.org/src/reflect/deepequal.go

    点赞 评论 复制链接分享

相关推荐