Commit 74c6b841 authored by Rui Ueyama's avatar Rui Ueyama Committed by Brad Fitzpatrick

expvar: fix map key output

To create a valid JSON string, "%s" is not enough.
Fixes #7761.

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/86730043
parent 9d81ade2
...@@ -112,7 +112,7 @@ func (v *Map) String() string { ...@@ -112,7 +112,7 @@ func (v *Map) String() string {
if !first { if !first {
fmt.Fprintf(&b, ", ") fmt.Fprintf(&b, ", ")
} }
fmt.Fprintf(&b, "\"%s\": %v", kv.Key, kv.Value) fmt.Fprintf(&b, "%q: %v", kv.Key, kv.Value)
first = false first = false
}) })
fmt.Fprintf(&b, "}") fmt.Fprintf(&b, "}")
......
...@@ -97,15 +97,15 @@ func TestMapCounter(t *testing.T) { ...@@ -97,15 +97,15 @@ func TestMapCounter(t *testing.T) {
colors.Add("red", 1) colors.Add("red", 1)
colors.Add("red", 2) colors.Add("red", 2)
colors.Add("blue", 4) colors.Add("blue", 4)
colors.AddFloat("green", 4.125) colors.AddFloat(`green "midori"`, 4.125)
if x := colors.m["red"].(*Int).i; x != 3 { if x := colors.m["red"].(*Int).i; x != 3 {
t.Errorf("colors.m[\"red\"] = %v, want 3", x) t.Errorf("colors.m[\"red\"] = %v, want 3", x)
} }
if x := colors.m["blue"].(*Int).i; x != 4 { if x := colors.m["blue"].(*Int).i; x != 4 {
t.Errorf("colors.m[\"blue\"] = %v, want 4", x) t.Errorf("colors.m[\"blue\"] = %v, want 4", x)
} }
if x := colors.m["green"].(*Float).f; x != 4.125 { if x := colors.m[`green "midori"`].(*Float).f; x != 4.125 {
t.Errorf("colors.m[\"green\"] = %v, want 3.14", x) t.Errorf("colors.m[`green \"midori\"] = %v, want 3.14", x)
} }
// colors.String() should be '{"red":3, "blue":4}', // colors.String() should be '{"red":3, "blue":4}',
......
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