Skip to content

Commit

Permalink
Adding better support for elements offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksander Mamla authored and Aleksander Mamla committed Feb 5, 2018
1 parent 5096fdd commit 19a1b85
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
vendor/github.com/buger/goterm/
prof.cpu
prof.mem

.idea
75 changes: 75 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package jsonparser

// Errors
var (
keyPathNotFoundError = 1
unknownValueTypeError = 2
malformedJsonError = 3
malformedStringError = 4
malformedArrayError = 5
malformedObjectError = 6
malformedValueError = 7
malformedStringEscapeError = 8
)

type ParserError struct {
offset int
s string
errorType int
}

func (err *ParserError) Error() string {
return err.s
}

func (err *ParserError) Offset() int {
return err.offset
}

func MalformedObjectError(offset int) error {
return &ParserError{s: "Value looks like object, but can't find closing '}' symbol", offset: offset, errorType: malformedObjectError}
}

func UnknownValueTypeError(offset int) error {
return &ParserError{s:"Unknown value type", offset: offset, errorType: unknownValueTypeError}
}

func KeyPathNotFoundError(offset int) error {
return &ParserError{s: "Key path not found", offset: offset, errorType: keyPathNotFoundError}
}

func MalformedJsonError(offset int) error {
return &ParserError{s: "Malformed JSON error", offset: offset, errorType: malformedJsonError}
}

func MalformedStringError(offset int) error {
return &ParserError{s: "Value is string, but can't find closing '\"' symbol", offset: offset, errorType: malformedStringError}
}

func MalformedArrayError(offset int) error {
return &ParserError{s: "Value is array, but can't find closing ']' symbol", offset: offset, errorType: malformedArrayError}
}

func MalformedValueError(offset int) error {
return &ParserError{s: "Value looks like Number/Boolean/None, but can't find its end: ',' or '}' symbol", offset: offset, errorType: malformedValueError}
}

func MalformedStringEscapeError(offset int) error {
return &ParserError{s: "Encountered an invalid escape sequence in a string", offset: offset, errorType: malformedStringEscapeError}
}

func addToErrorOffset(err error, offset int) error {
if parserError, ok := err.(*ParserError); ok {
parserError.offset = parserError.offset + offset
}
return err
}

func isKeyPathNotFoundError(err error) bool {
return isParserErrorOfType(err, keyPathNotFoundError)
}

func isParserErrorOfType(err error, errorType int) bool {
parserError, ok := err.(*ParserError)
return ok && parserError.errorType == errorType
}
2 changes: 1 addition & 1 deletion escape.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func Unescape(in, out []byte) ([]byte, error) {
// Unescape the next escaped character
inLen, bufLen := unescapeToUTF8(in, buf)
if inLen == -1 {
return nil, MalformedStringEscapeError
return nil, MalformedStringEscapeError(firstBackslash)
}

in = in[inLen:]
Expand Down
Loading

0 comments on commit 19a1b85

Please sign in to comment.