Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export errBadJSONDoc and errBadJSONPatch errors #209

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading