-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Reuven
committed
Apr 4, 2024
1 parent
c2ca1b1
commit 5a6578c
Showing
4 changed files
with
75 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package diff | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/wI2L/jsondiff" | ||
) | ||
|
||
// jsonPatch is a wrapper to jsondiff.jsonPatch with proper serialization for json and yaml | ||
type jsonPatch []*jsonOperation | ||
|
||
// jsonOperation is a wrapper to jsondiff.jsonOperation with proper serialization for json and yaml | ||
type jsonOperation struct { | ||
OldValue interface{} `json:"old_value" yaml:"old_value"` | ||
Value interface{} `json:"value" yaml:"value"` | ||
Type string `json:"op" yaml:"op"` | ||
From string `json:"from" yaml:"from"` | ||
Path string `json:"path" yaml:"path"` | ||
} | ||
|
||
func toJsonPatch(patch jsondiff.Patch) jsonPatch { | ||
result := make(jsonPatch, len(patch)) | ||
for i, op := range patch { | ||
result[i] = newJsonOperation(op) | ||
} | ||
return result | ||
} | ||
|
||
func newJsonOperation(op jsondiff.Operation) *jsonOperation { | ||
return &jsonOperation{ | ||
OldValue: op.OldValue, | ||
Value: op.Value, | ||
Type: op.Type, | ||
From: op.From, | ||
Path: op.Path, | ||
} | ||
} | ||
|
||
func compareJson(source, target interface{}, opts ...jsondiff.Option) (jsonPatch, error) { | ||
patch, err := jsondiff.Compare(source, target, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return toJsonPatch(patch), nil | ||
} | ||
|
||
// GetJsonOrigValue returns the original value of the diff, only if there is exactly one diff | ||
func GetJsonOrigValue(patch jsonPatch) (json.RawMessage, bool) { | ||
if len(patch) != 1 { | ||
return nil, false | ||
} | ||
result, ok := patch[0].OldValue.(json.RawMessage) | ||
if !ok { | ||
return nil, false | ||
} | ||
return result, true | ||
} | ||
|
||
// GetJsonOrigValue returns the new value of the diff, only if there is exactly one diff | ||
func GetJsonNewValue(patch jsonPatch) (json.RawMessage, bool) { | ||
if len(patch) != 1 { | ||
return nil, false | ||
} | ||
result, ok := patch[0].Value.(json.RawMessage) | ||
if !ok { | ||
return nil, false | ||
} | ||
return result, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,9 @@ | ||
package diff | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/wI2L/jsondiff" | ||
) | ||
|
||
// ModifiedInterfaces is map of interface names to their respective diffs | ||
type ModifiedInterfaces map[string]jsondiff.Patch | ||
type ModifiedInterfaces map[string]jsonPatch | ||
|
||
// Empty indicates whether a change was found in this element | ||
func (modifiedInterfaces ModifiedInterfaces) Empty() bool { | ||
return len(modifiedInterfaces) == 0 | ||
} | ||
|
||
// GetJsonOrigValue returns the original value of the diff, only if there is exactly one diff | ||
func GetJsonOrigValue(patch jsondiff.Patch) (json.RawMessage, bool) { | ||
if len(patch) != 1 { | ||
return nil, false | ||
} | ||
result, ok := patch[0].OldValue.(json.RawMessage) | ||
if !ok { | ||
return nil, false | ||
} | ||
return result, true | ||
} | ||
|
||
// GetJsonOrigValue returns the new value of the diff, only if there is exactly one diff | ||
func GetJsonNewValue(patch jsondiff.Patch) (json.RawMessage, bool) { | ||
if len(patch) != 1 { | ||
return nil, false | ||
} | ||
result, ok := patch[0].Value.(json.RawMessage) | ||
if !ok { | ||
return nil, false | ||
} | ||
return result, true | ||
} |