From 8b0a772eee803c490c03f8515114d76eb8d919d8 Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Wed, 26 Apr 2023 10:35:00 +0100 Subject: Update fillReadBuffer to use an empty structure as the root structure instead of a dedicated JSONInRoot structure --- walk/read.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'walk/read.go') diff --git a/walk/read.go b/walk/read.go index f123369..e46ae58 100644 --- a/walk/read.go +++ b/walk/read.go @@ -19,8 +19,7 @@ const ( type JSONInStructure int const ( - JSONInRoot JSONInStructure = iota - JSONInMap + JSONInMap JSONInStructure = iota JSONInArray ) @@ -49,7 +48,7 @@ func NewJSONIn(reader *bufio.Reader) JSONIn { return JSONIn { path: make([]Atom, 0, 256), reader: reader, - structure: []JSONInStructure{JSONInRoot}, + structure: []JSONInStructure{}, state: JSONInValueStart, readBuffer: make([]Atom, 0, 256), readIndex: 0, @@ -227,7 +226,7 @@ func (in *JSONIn) Read() (WalkItem, error) { } func (in *JSONIn) AssertDone() { - if len(in.structure) != 1 || in.structure[0] != JSONInRoot { + if len(in.structure) != 0 || in.state != JSONInValueEnd || in.readIndex < len(in.readBuffer) { panic("Input ended on incomplete JSON root") } } @@ -259,14 +258,15 @@ func (in *JSONIn) fillReadBuffer() error { panic("Invalid JSONInState") } valueStart: { + if len(in.structure) == 0 { + goto value + } innermost := in.structure[len(in.structure) - 1] switch innermost { case JSONInMap: goto mapValue case JSONInArray: goto arrayValue - case JSONInRoot: - goto value default: panic("Invalid JSONInStructure") } @@ -430,10 +430,11 @@ func (in *JSONIn) fillReadBuffer() error { in.state = JSONInValueEnd return err } - innermost := in.structure[len(in.structure) - 1] - if innermost == JSONInRoot { + if len(in.structure) == 0 { panic("More input after root JSON object ends") - } else if innermost == JSONInMap && r == '}' { + } + innermost := in.structure[len(in.structure) - 1] + if innermost == JSONInMap && r == '}' { in.structure = in.structure[:len(in.structure) - 1] in.pushActionBuffer(ActionPopPath) in.pushActionBuffer(ActionReadValue) -- cgit v1.2.3