fix bug with node length of conflicting map entries

This commit is contained in:
Arpad Ryszka 2020-12-06 22:30:19 +01:00
parent 197f72bb5b
commit a0113b016b
3 changed files with 32 additions and 2 deletions

24
bugfix_test.go Normal file
View File

@ -0,0 +1,24 @@
package notation
import "testing"
func TestMapWrapping(t *testing.T) {
const expect = `map[*interface{}]interface{}{
*interface{}(string("foo")): interface{}(bool(true)),
*interface{}(string("foo")): interface{}(bool(true)),
}`
ifpointer := func(v interface{}) *interface{} {
return &v
}
m := map[*interface{}]interface{}{
ifpointer("foo"): true,
ifpointer("foo"): true,
}
s := Sprintwv(m)
if s != expect {
t.Fatalf("expected: %s, got: %s", expect, s)
}
}

View File

@ -24,6 +24,7 @@ const (
skipTypes skipTypes
allTypes allTypes
randomMaps randomMaps
pointerValues
) )
type wrapLen struct { type wrapLen struct {

View File

@ -144,9 +144,10 @@ func reflectMap(o opts, p *pending, r reflect.Value) node {
sn := make(map[string]node) sn := make(map[string]node)
for _, key := range r.MapKeys() { for _, key := range r.MapKeys() {
kn := reflectValue(itemOpts, p, key) kn := reflectValue(itemOpts, p, key)
knExt := reflectValue(allTypes|pointerValues, p, key)
var b bytes.Buffer var b bytes.Buffer
wr := writer{w: &b} wr := writer{w: &b}
fprint(&wr, 0, kn) fprint(&wr, 0, knExt)
skey := b.String() skey := b.String()
skeys = append(skeys, skey) skeys = append(skeys, skey)
sv[skey] = key sv[skey] = key
@ -183,7 +184,11 @@ func reflectPointer(o opts, p *pending, r reflect.Value) node {
return e return e
} }
return nodeOf("*", e) if o&pointerValues == 0 {
return nodeOf("*", e)
}
return nodeOf("*(", r.Pointer(), ")", e)
} }
func reflectList(o opts, p *pending, r reflect.Value) node { func reflectList(o opts, p *pending, r reflect.Value) node {