summaryrefslogtreecommitdiff
path: root/decode.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-10 19:07:57 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-10 21:49:02 -0700
commit7da5e8c664c7f4900ee74e8113e61a003ecbbe13 (patch)
tree6b3b127289100807e03ccf322cc53f769c69707e /decode.go
parentaec0feb925614b0f878afa566b912d102ed9ccca (diff)
decode: Have .readRune() and .expectRune() return *DecodeError
Diffstat (limited to 'decode.go')
-rw-r--r--decode.go147
1 files changed, 111 insertions, 36 deletions
diff --git a/decode.go b/decode.go
index 15ab766..9ec706f 100644
--- a/decode.go
+++ b/decode.go
@@ -268,17 +268,17 @@ func (dec *Decoder) newTypeError(jTyp string, gTyp reflect.Type, err error) *Dec
}
}
-func (dec *Decoder) readRune() (rune, jsonparse.RuneType) {
+func (dec *Decoder) readRune() (rune, jsonparse.RuneType, *DecodeError) {
c, _, t, e := dec.io.ReadRuneType()
if e != nil {
- panic(decodeError{
+ return 0, 0, &DecodeError{
Field: dec.structStackStr(),
FieldParent: dec.structStackParent(),
FieldName: dec.structStackName(),
Err: e,
- })
+ }
}
- return c, t
+ return c, t, nil
}
func (dec *Decoder) unreadRune() {
@@ -289,22 +289,31 @@ func (dec *Decoder) unreadRune() {
}
}
-//nolint:unparam // It will start returning errors soon.
func (dec *Decoder) peekRuneType() (jsonparse.RuneType, *DecodeError) {
- _, t := dec.readRune()
+ _, t, err := dec.readRune()
+ if err != nil {
+ return 0, err
+ }
dec.unreadRune()
return t, nil
}
-func (dec *Decoder) expectRune(ec rune, et jsonparse.RuneType) {
- ac, at := dec.readRune()
+func (dec *Decoder) expectRune(ec rune, et jsonparse.RuneType) *DecodeError {
+ ac, at, err := dec.readRune()
+ if err != nil {
+ return err
+ }
if ac != ec || at != et {
panic("should not happen")
}
+ return nil
}
func (dec *Decoder) expectRuneType(ec rune, et jsonparse.RuneType, gt reflect.Type) *DecodeError {
- ac, at := dec.readRune()
+ ac, at, err := dec.readRune()
+ if err != nil {
+ return err
+ }
if ac != ec || at != et {
return dec.newTypeError(at.JSONType(), gt, nil)
}
@@ -869,7 +878,10 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) (_err *DecodeError) {
func (dec *Decoder) scan(out fastio.RuneWriter) *DecodeError {
dec.io.PushReadBarrier()
for {
- c, t := dec.readRune()
+ c, t, err := dec.readRune()
+ if err != nil {
+ return err
+ }
if t == jsonparse.RuneTypeEOF {
break
}
@@ -891,7 +903,10 @@ func (dec *Decoder) scanNumber(gTyp reflect.Type, out fastio.RuneWriter) *Decode
}
func (dec *Decoder) decodeAny() (any, *DecodeError) {
- c, _ := dec.readRune()
+ c, _, err := dec.readRune()
+ if err != nil {
+ return nil, err
+ }
dec.unreadRune()
switch c {
case '{':
@@ -1037,7 +1052,10 @@ func (dec *Decoder) decodeObject(gTyp reflect.Type, decodeKey, decodeVal func()
if err := dec.expectRuneType('{', jsonparse.RuneTypeObjectBeg, gTyp); err != nil {
return err
}
- _, t := dec.readRune()
+ _, t, err := dec.readRune()
+ if err != nil {
+ return err
+ }
switch t {
case jsonparse.RuneTypeObjectEnd:
return nil
@@ -1047,14 +1065,21 @@ func (dec *Decoder) decodeObject(gTyp reflect.Type, decodeKey, decodeVal func()
if err := decodeKey(); err != nil {
return err
}
- dec.expectRune(':', jsonparse.RuneTypeObjectColon)
+ if err := dec.expectRune(':', jsonparse.RuneTypeObjectColon); err != nil {
+ return err
+ }
if err := decodeVal(); err != nil {
return err
}
- _, t := dec.readRune()
+ _, t, err := dec.readRune()
+ if err != nil {
+ return err
+ }
switch t {
case jsonparse.RuneTypeObjectComma:
- dec.expectRune('"', jsonparse.RuneTypeStringBeg)
+ if err := dec.expectRune('"', jsonparse.RuneTypeStringBeg); err != nil {
+ return err
+ }
goto decodeMember
case jsonparse.RuneTypeObjectEnd:
return nil
@@ -1121,7 +1146,10 @@ func (dec *Decoder) decodeArray(gTyp reflect.Type, decodeMember func() *DecodeEr
if err := dec.expectRuneType('[', jsonparse.RuneTypeArrayBeg, gTyp); err != nil {
return err
}
- _, t := dec.readRune()
+ _, t, err := dec.readRune()
+ if err != nil {
+ return err
+ }
switch t {
case jsonparse.RuneTypeArrayEnd:
return nil
@@ -1131,7 +1159,10 @@ func (dec *Decoder) decodeArray(gTyp reflect.Type, decodeMember func() *DecodeEr
if err := decodeMember(); err != nil {
return err
}
- _, t := dec.readRune()
+ _, t, err := dec.readRune()
+ if err != nil {
+ return err
+ }
switch t {
case jsonparse.RuneTypeArrayComma:
goto decodeNextMember
@@ -1149,7 +1180,10 @@ func (dec *Decoder) decodeString(gTyp reflect.Type, out fastio.RuneWriter) *Deco
}
var uhex [4]byte
for {
- c, t := dec.readRune()
+ c, t, err := dec.readRune()
+ if err != nil {
+ return err
+ }
switch t {
case jsonparse.RuneTypeStringChar:
_, _ = out.WriteRune(c)
@@ -1199,7 +1233,9 @@ func (dec *Decoder) decodeString(gTyp reflect.Type, out fastio.RuneWriter) *Deco
_, _ = out.WriteRune(utf8.RuneError)
break
}
- dec.expectRune('\\', jsonparse.RuneTypeStringEsc)
+ if err := dec.expectRune('\\', jsonparse.RuneTypeStringEsc); err != nil {
+ return err
+ }
t, err = dec.peekRuneType()
if err != nil {
return err
@@ -1208,15 +1244,29 @@ func (dec *Decoder) decodeString(gTyp reflect.Type, out fastio.RuneWriter) *Deco
_, _ = out.WriteRune(utf8.RuneError)
break
}
- dec.expectRune('u', jsonparse.RuneTypeStringEscU)
+ if err := dec.expectRune('u', jsonparse.RuneTypeStringEscU); err != nil {
+ return err
+ }
- b, _ := dec.readRune()
+ b, _, err := dec.readRune()
+ if err != nil {
+ return err
+ }
uhex[0], _ = jsonparse.HexToInt(b)
- b, _ = dec.readRune()
+ b, _, err = dec.readRune()
+ if err != nil {
+ return err
+ }
uhex[1], _ = jsonparse.HexToInt(b)
- b, _ = dec.readRune()
+ b, _, err = dec.readRune()
+ if err != nil {
+ return err
+ }
uhex[2], _ = jsonparse.HexToInt(b)
- b, _ = dec.readRune()
+ b, _, err = dec.readRune()
+ if err != nil {
+ return err
+ }
uhex[3], _ = jsonparse.HexToInt(b)
c2 := 0 |
rune(uhex[0])<<12 |
@@ -1242,18 +1292,35 @@ func (dec *Decoder) decodeString(gTyp reflect.Type, out fastio.RuneWriter) *Deco
}
func (dec *Decoder) decodeBool(gTyp reflect.Type) (bool, *DecodeError) {
- c, t := dec.readRune()
+ c, t, err := dec.readRune()
+ if err != nil {
+ return false, err
+ }
switch c {
case 't':
- dec.expectRune('r', jsonparse.RuneTypeTrueR)
- dec.expectRune('u', jsonparse.RuneTypeTrueU)
- dec.expectRune('e', jsonparse.RuneTypeTrueE)
+ if err := dec.expectRune('r', jsonparse.RuneTypeTrueR); err != nil {
+ return false, err
+ }
+ if err := dec.expectRune('u', jsonparse.RuneTypeTrueU); err != nil {
+ return false, err
+ }
+ if err := dec.expectRune('e', jsonparse.RuneTypeTrueE); err != nil {
+ return false, err
+ }
return true, nil
case 'f':
- dec.expectRune('a', jsonparse.RuneTypeFalseA)
- dec.expectRune('l', jsonparse.RuneTypeFalseL)
- dec.expectRune('s', jsonparse.RuneTypeFalseS)
- dec.expectRune('e', jsonparse.RuneTypeFalseE)
+ if err := dec.expectRune('a', jsonparse.RuneTypeFalseA); err != nil {
+ return false, err
+ }
+ if err := dec.expectRune('l', jsonparse.RuneTypeFalseL); err != nil {
+ return false, err
+ }
+ if err := dec.expectRune('s', jsonparse.RuneTypeFalseS); err != nil {
+ return false, err
+ }
+ if err := dec.expectRune('e', jsonparse.RuneTypeFalseE); err != nil {
+ return false, err
+ }
return false, nil
default:
return false, dec.newTypeError(t.JSONType(), gTyp, nil)
@@ -1261,10 +1328,18 @@ func (dec *Decoder) decodeBool(gTyp reflect.Type) (bool, *DecodeError) {
}
func (dec *Decoder) decodeNull() *DecodeError {
- dec.expectRune('n', jsonparse.RuneTypeNullN)
- dec.expectRune('u', jsonparse.RuneTypeNullU)
- dec.expectRune('l', jsonparse.RuneTypeNullL1)
- dec.expectRune('l', jsonparse.RuneTypeNullL2)
+ if err := dec.expectRune('n', jsonparse.RuneTypeNullN); err != nil {
+ return err
+ }
+ if err := dec.expectRune('u', jsonparse.RuneTypeNullU); err != nil {
+ return err
+ }
+ if err := dec.expectRune('l', jsonparse.RuneTypeNullL1); err != nil {
+ return err
+ }
+ if err := dec.expectRune('l', jsonparse.RuneTypeNullL2); err != nil {
+ return err
+ }
return nil
}