]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: simplify encodeState.{string, stringBytes}
authorNodir Turakulov <nodir@google.com>
Wed, 14 Oct 2015 22:18:10 +0000 (15:18 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 14 Oct 2015 23:29:14 +0000 (23:29 +0000)
As correctly mentioned in #11883, encodeState.string and
encodeState.stringBytes never return an error.
This CL removes the error from the function signatures and somewhat
simplifies call sites.

Fixes #11883

Change-Id: I1d1853d09631c545b68b5eea86ff7daa2e0ca10b
Reviewed-on: https://go-review.googlesource.com/15836
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/encoding/json/encode.go
src/encoding/json/encode_test.go

index 21f403e78861200fc3455570a1640fcd18fae6fe..60d1c9011b84759a2e2e2ba8c52ce878a55581d9 100644 (file)
@@ -448,12 +448,10 @@ func textMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
        }
        m := v.Interface().(encoding.TextMarshaler)
        b, err := m.MarshalText()
-       if err == nil {
-               _, err = e.stringBytes(b)
-       }
        if err != nil {
                e.error(&MarshalerError{v.Type(), err})
        }
+       e.stringBytes(b)
 }
 
 func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
@@ -464,12 +462,10 @@ func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
        }
        m := va.Interface().(encoding.TextMarshaler)
        b, err := m.MarshalText()
-       if err == nil {
-               _, err = e.stringBytes(b)
-       }
        if err != nil {
                e.error(&MarshalerError{v.Type(), err})
        }
+       e.stringBytes(b)
 }
 
 func boolEncoder(e *encodeState, v reflect.Value, quoted bool) {
@@ -783,7 +779,7 @@ func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) }
 func (sv stringValues) get(i int) string   { return sv[i].String() }
 
 // NOTE: keep in sync with stringBytes below.
-func (e *encodeState) string(s string) (int, error) {
+func (e *encodeState) string(s string) int {
        len0 := e.Len()
        e.WriteByte('"')
        start := 0
@@ -855,11 +851,11 @@ func (e *encodeState) string(s string) (int, error) {
                e.WriteString(s[start:])
        }
        e.WriteByte('"')
-       return e.Len() - len0, nil
+       return e.Len() - len0
 }
 
 // NOTE: keep in sync with string above.
-func (e *encodeState) stringBytes(s []byte) (int, error) {
+func (e *encodeState) stringBytes(s []byte) int {
        len0 := e.Len()
        e.WriteByte('"')
        start := 0
@@ -931,7 +927,7 @@ func (e *encodeState) stringBytes(s []byte) (int, error) {
                e.Write(s[start:])
        }
        e.WriteByte('"')
-       return e.Len() - len0, nil
+       return e.Len() - len0
 }
 
 // A field represents a single field found in a struct.
index 7abfa85db7bc243c6c057a476297dce62cc9c5f7..2206b2ee2e711823717babf780c24bb55ac8fa15 100644 (file)
@@ -381,16 +381,10 @@ func TestStringBytes(t *testing.T) {
                r = append(r, i)
        }
        s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
-       _, err := es.string(s)
-       if err != nil {
-               t.Fatal(err)
-       }
+       es.string(s)
 
        esBytes := &encodeState{}
-       _, err = esBytes.stringBytes([]byte(s))
-       if err != nil {
-               t.Fatal(err)
-       }
+       esBytes.stringBytes([]byte(s))
 
        enc := es.Buffer.String()
        encBytes := esBytes.Buffer.String()