Skip to content

Commit

Permalink
Merge pull request #50 from clagraff/master
Browse files Browse the repository at this point in the history
add nil-pointer checks to avoid runtime panics
  • Loading branch information
evanphx authored Mar 16, 2018
2 parents 944e072 + 975445d commit ed7cfba
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (n *lazyNode) intoDoc() (*partialDoc, error) {
return &n.doc, nil
}

if n.raw == nil {
return nil, fmt.Errorf("Unable to unmarshal nil pointer as partial document")
}

err := json.Unmarshal(*n.raw, &n.doc)

if err != nil {
Expand All @@ -81,6 +85,10 @@ func (n *lazyNode) intoAry() (*partialArray, error) {
return &n.ary, nil
}

if n.raw == nil {
return nil, fmt.Errorf("Unable to unmarshal nil pointer as partial array")
}

err := json.Unmarshal(*n.raw, &n.ary)

if err != nil {
Expand All @@ -94,6 +102,10 @@ func (n *lazyNode) intoAry() (*partialArray, error) {
func (n *lazyNode) compact() []byte {
buf := &bytes.Buffer{}

if n.raw == nil {
return nil
}

err := json.Compact(buf, *n.raw)

if err != nil {
Expand All @@ -104,6 +116,10 @@ func (n *lazyNode) compact() []byte {
}

func (n *lazyNode) tryDoc() bool {
if n.raw == nil {
return false
}

err := json.Unmarshal(*n.raw, &n.doc)

if err != nil {
Expand All @@ -115,6 +131,10 @@ func (n *lazyNode) tryDoc() bool {
}

func (n *lazyNode) tryAry() bool {
if n.raw == nil {
return false
}

err := json.Unmarshal(*n.raw, &n.ary)

if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,24 @@ var TestCases = []TestCase{
true,
"",
},
{
`{ "foo": null }`,
`[ { "op": "test", "path": "/foo", "value": null } ]`,
true,
"",
},
{
`{ "foo": {} }`,
`[ { "op": "test", "path": "/foo", "value": null } ]`,
false,
"/foo",
},
{
`{ "foo": [] }`,
`[ { "op": "test", "path": "/foo", "value": null } ]`,
false,
"/foo",
},
{
`{ "baz/foo": "qux" }`,
`[ { "op": "test", "path": "/baz~1foo", "value": "qux"} ]`,
Expand Down

0 comments on commit ed7cfba

Please sign in to comment.