]> Cypherpunks repositories - gostls13.git/commitdiff
expvar: fix map key output
authorRui Ueyama <ruiu@google.com>
Fri, 11 Apr 2014 04:14:04 +0000 (21:14 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 11 Apr 2014 04:14:04 +0000 (21:14 -0700)
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

src/pkg/expvar/expvar.go
src/pkg/expvar/expvar_test.go

index 3abc65c407b998b2e672931212e55e2dd0653fe1..6639ecc2944c7170e824601ef4d59fc24f8a17f1 100644 (file)
@@ -112,7 +112,7 @@ func (v *Map) String() string {
                if !first {
                        fmt.Fprintf(&b, ", ")
                }
-               fmt.Fprintf(&b, "\"%s\": %v", kv.Key, kv.Value)
+               fmt.Fprintf(&b, "%q: %v", kv.Key, kv.Value)
                first = false
        })
        fmt.Fprintf(&b, "}")
index d2ea484935e9c5fe08cb52e4c27c01a26b7cab79..765e3b757e9a48a95ea216a34efc83baec8e836e 100644 (file)
@@ -97,15 +97,15 @@ func TestMapCounter(t *testing.T) {
        colors.Add("red", 1)
        colors.Add("red", 2)
        colors.Add("blue", 4)
-       colors.AddFloat("green", 4.125)
+       colors.AddFloat(`green "midori"`, 4.125)
        if x := colors.m["red"].(*Int).i; x != 3 {
                t.Errorf("colors.m[\"red\"] = %v, want 3", x)
        }
        if x := colors.m["blue"].(*Int).i; x != 4 {
                t.Errorf("colors.m[\"blue\"] = %v, want 4", x)
        }
-       if x := colors.m["green"].(*Float).f; x != 4.125 {
-               t.Errorf("colors.m[\"green\"] = %v, want 3.14", x)
+       if x := colors.m[`green "midori"`].(*Float).f; x != 4.125 {
+               t.Errorf("colors.m[`green \"midori\"] = %v, want 3.14", x)
        }
 
        // colors.String() should be '{"red":3, "blue":4}',