diff options
| author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-26 10:35:00 +0100 | 
|---|---|---|
| committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-26 10:35:00 +0100 | 
| commit | 8b0a772eee803c490c03f8515114d76eb8d919d8 (patch) | |
| tree | c92afc91cb3d8fb509d2fc82f7bd4b21403db9a8 | |
| parent | 88b60b7e1e43f88701c2ad0dafdcd2aa4f5fc454 (diff) | |
| download | stred-go-8b0a772eee803c490c03f8515114d76eb8d919d8.tar | |
Update fillReadBuffer to use an empty structure as the root structure instead of a dedicated JSONInRoot structure
| -rw-r--r-- | walk/read.go | 19 | 
1 files changed, 10 insertions, 9 deletions
| 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) | 
