Skip to content

Commit

Permalink
Merge pull request #209 from skitt/export-errs-v5
Browse files Browse the repository at this point in the history
Export errBadJSONDoc and errBadJSONPatch errors
  • Loading branch information
evanphx authored Jan 28, 2025
2 parents bd18525 + 7a7a88a commit 84a4bb1
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions v5/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func pruneAryNulls(ary *partialArray, options *ApplyOptions) *partialArray {
return ary
}

var errBadJSONDoc = fmt.Errorf("Invalid JSON Document")
var errBadJSONPatch = fmt.Errorf("Invalid JSON Patch")
var ErrBadJSONDoc = fmt.Errorf("Invalid JSON Document")
var ErrBadJSONPatch = fmt.Errorf("Invalid JSON Patch")
var errBadMergeTypes = fmt.Errorf("Mismatched JSON Documents")

// MergeMergePatches merges two merge patches together, such that
Expand All @@ -121,11 +121,11 @@ func MergePatch(docData, patchData []byte) ([]byte, error) {

func doMergePatch(docData, patchData []byte, mergeMerge bool) ([]byte, error) {
if !json.Valid(docData) {
return nil, errBadJSONDoc
return nil, ErrBadJSONDoc
}

if !json.Valid(patchData) {
return nil, errBadJSONPatch
return nil, ErrBadJSONPatch
}

options := NewApplyOptions()
Expand All @@ -143,15 +143,15 @@ func doMergePatch(docData, patchData []byte, mergeMerge bool) ([]byte, error) {
patchErr := patch.UnmarshalJSON(patchData)

if isSyntaxError(docErr) {
return nil, errBadJSONDoc
return nil, ErrBadJSONDoc
}

if isSyntaxError(patchErr) {
return patchData, nil
}

if docErr == nil && doc.obj == nil {
return nil, errBadJSONDoc
return nil, ErrBadJSONDoc
}

if patchErr == nil && patch.obj == nil {
Expand All @@ -175,15 +175,15 @@ func doMergePatch(docData, patchData []byte, mergeMerge bool) ([]byte, error) {
if json.Valid(patchData) {
return patchData, nil
}
return nil, errBadJSONPatch
return nil, ErrBadJSONPatch
}

pruneAryNulls(patchAry, options)

out, patchErr := json.Marshal(patchAry.nodes)

if patchErr != nil {
return nil, errBadJSONPatch
return nil, ErrBadJSONPatch
}

return out, nil
Expand Down Expand Up @@ -256,12 +256,12 @@ func createObjectMergePatch(originalJSON, modifiedJSON []byte) ([]byte, error) {

err := unmarshal(originalJSON, &originalDoc)
if err != nil {
return nil, errBadJSONDoc
return nil, ErrBadJSONDoc
}

err = unmarshal(modifiedJSON, &modifiedDoc)
if err != nil {
return nil, errBadJSONDoc
return nil, ErrBadJSONDoc
}

dest, err := getDiff(originalDoc, modifiedDoc)
Expand All @@ -286,17 +286,17 @@ func createArrayMergePatch(originalJSON, modifiedJSON []byte) ([]byte, error) {

err := unmarshal(originalJSON, &originalDocs)
if err != nil {
return nil, errBadJSONDoc
return nil, ErrBadJSONDoc
}

err = unmarshal(modifiedJSON, &modifiedDocs)
if err != nil {
return nil, errBadJSONDoc
return nil, ErrBadJSONDoc
}

total := len(originalDocs)
if len(modifiedDocs) != total {
return nil, errBadJSONDoc
return nil, ErrBadJSONDoc
}

result := []json.RawMessage{}
Expand Down

0 comments on commit 84a4bb1

Please sign in to comment.