43 lines
827 B
Go
43 lines
827 B
Go
package json
|
|
|
|
import (
|
|
"forge.cadoles.com/wpetit/guesstimate/internal/diffsync"
|
|
"github.com/pkg/errors"
|
|
jsonpatch "gopkg.in/evanphx/json-patch.v4"
|
|
)
|
|
|
|
type Patcher struct{}
|
|
|
|
func (p *Patcher) Patch(content []byte, ops diffsync.Ops) ([]byte, error) {
|
|
patch, ok := ops.([]byte)
|
|
if !ok {
|
|
return nil, diffsync.ErrUnexpectedOpsFormat
|
|
}
|
|
|
|
modified, err := jsonpatch.MergePatch(content, patch)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not apply patch")
|
|
}
|
|
|
|
return modified, nil
|
|
}
|
|
|
|
func NewPatcher() *Patcher {
|
|
return &Patcher{}
|
|
}
|
|
|
|
type Differ struct{}
|
|
|
|
func (d *Differ) Diff(new, old []byte) (diffsync.Ops, error) {
|
|
ops, err := jsonpatch.CreateMergePatch(old, new)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute diff")
|
|
}
|
|
|
|
return ops, nil
|
|
}
|
|
|
|
func NewDiffer() *Differ {
|
|
return &Differ{}
|
|
}
|