]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: get rid of the stream_test.go TODOs
authorDaniel Martí <mvdan@mvdan.cc>
Sun, 26 Aug 2018 14:35:24 +0000 (08:35 -0600)
committerDaniel Martí <mvdan@mvdan.cc>
Sun, 26 Aug 2018 17:09:27 +0000 (17:09 +0000)
TestRawMessage now passes without the need for the RawMessage field to
be a pointer. The TODO dates all the way back to 2010, so I presume the
issue has since been fixed.

TestNullRawMessage tested the decoding of a JSON null into a
*RawMessage. The existing behavior was correct, but for the sake of
completeness a non-pointer RawMessage field has been added too. The
non-pointer field behaves differently, as one can read in the docs:

To unmarshal JSON into a value implementing the Unmarshaler
interface, Unmarshal calls that value's UnmarshalJSON method,
including when the input is a JSON null.

Change-Id: Iabaed75d4ed10ea427d135ee1b80c6e6b83b2e6e
Reviewed-on: https://go-review.googlesource.com/131377
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/encoding/json/stream_test.go

index 0ed1c9e97475410076bcb46939ac28ecadf2df02..aaf32e0a24ce516dd71bb83b548f47c07e6aa70e 100644 (file)
@@ -201,10 +201,9 @@ func nlines(s string, n int) string {
 }
 
 func TestRawMessage(t *testing.T) {
-       // TODO(rsc): Should not need the * in *RawMessage
        var data struct {
                X  float64
-               Id *RawMessage
+               Id RawMessage
                Y  float32
        }
        const raw = `["\u0056",null]`
@@ -213,8 +212,8 @@ func TestRawMessage(t *testing.T) {
        if err != nil {
                t.Fatalf("Unmarshal: %v", err)
        }
-       if string([]byte(*data.Id)) != raw {
-               t.Fatalf("Raw mismatch: have %#q want %#q", []byte(*data.Id), raw)
+       if string([]byte(data.Id)) != raw {
+               t.Fatalf("Raw mismatch: have %#q want %#q", []byte(data.Id), raw)
        }
        b, err := Marshal(&data)
        if err != nil {
@@ -226,20 +225,22 @@ func TestRawMessage(t *testing.T) {
 }
 
 func TestNullRawMessage(t *testing.T) {
-       // TODO(rsc): Should not need the * in *RawMessage
        var data struct {
-               X  float64
-               Id *RawMessage
-               Y  float32
+               X     float64
+               Id    RawMessage
+               IdPtr *RawMessage
+               Y     float32
        }
-       data.Id = new(RawMessage)
-       const msg = `{"X":0.1,"Id":null,"Y":0.2}`
+       const msg = `{"X":0.1,"Id":null,"IdPtr":null,"Y":0.2}`
        err := Unmarshal([]byte(msg), &data)
        if err != nil {
                t.Fatalf("Unmarshal: %v", err)
        }
-       if data.Id != nil {
-               t.Fatalf("Raw mismatch: have non-nil, want nil")
+       if want, got := "null", string(data.Id); want != got {
+               t.Fatalf("Raw mismatch: have %q, want %q", got, want)
+       }
+       if data.IdPtr != nil {
+               t.Fatalf("Raw pointer mismatch: have non-nil, want nil")
        }
        b, err := Marshal(&data)
        if err != nil {