Commit 86e6a441 authored by Russ Cox's avatar Russ Cox

reflect: allow unexported key in Value.MapIndex

Fixes #1748.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/4444087
parent 8ef56f7e
...@@ -726,6 +726,24 @@ func TestDeepEqualComplexStructInequality(t *testing.T) { ...@@ -726,6 +726,24 @@ func TestDeepEqualComplexStructInequality(t *testing.T) {
} }
} }
type UnexpT struct {
m map[int]int
}
func TestDeepEqualUnexportedMap(t *testing.T) {
// Check that DeepEqual can look at unexported fields.
x1 := UnexpT{map[int]int{1: 2}}
x2 := UnexpT{map[int]int{1: 2}}
if !DeepEqual(&x1, &x2) {
t.Error("DeepEqual(x1, x2) = false, want true")
}
y1 := UnexpT{map[int]int{2: 3}}
if DeepEqual(&x1, &y1) {
t.Error("DeepEqual(x1, y1) = true, want false")
}
}
func check2ndField(x interface{}, offs uintptr, t *testing.T) { func check2ndField(x interface{}, offs uintptr, t *testing.T) {
s := ValueOf(x) s := ValueOf(x)
......
...@@ -958,14 +958,19 @@ func (v Value) MapIndex(key Value) Value { ...@@ -958,14 +958,19 @@ func (v Value) MapIndex(key Value) Value {
iv.mustBe(Map) iv.mustBe(Map)
typ := iv.typ.toType() typ := iv.typ.toType()
// Do not require ikey to be exported, so that DeepEqual
// and other programs can use all the keys returned by
// MapKeys as arguments to MapIndex. If either the map
// or the key is unexported, though, the result will be
// considered unexported.
ikey := key.internal() ikey := key.internal()
ikey.mustBeExported()
ikey = convertForAssignment("reflect.Value.MapIndex", nil, typ.Key(), ikey) ikey = convertForAssignment("reflect.Value.MapIndex", nil, typ.Key(), ikey)
if iv.word == 0 { if iv.word == 0 {
return Value{} return Value{}
} }
flag := iv.flag & flagRO flag := (iv.flag | ikey.flag) & flagRO
elemType := typ.Elem() elemType := typ.Elem()
elemWord, ok := mapaccess(iv.word, ikey.word) elemWord, ok := mapaccess(iv.word, ikey.word)
if !ok { if !ok {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment