Ensure diff-match-patch works on non string values

This commit is contained in:
Tobie Morgan Hitchcock 2020-12-09 21:20:01 +00:00
parent 410a0e1769
commit 41ad26811c

View file

@ -15,6 +15,8 @@
package diff package diff
import ( import (
"fmt"
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
@ -237,12 +239,21 @@ func (o *operations) patch(old map[string]interface{}) (now map[string]interface
case "replace": case "replace":
obj.Set(v.value, path...) obj.Set(v.value, path...)
case "change": case "change":
if txt, ok := obj.Get(path...).Data().(string); ok { switch val := obj.Get(path...).Data().(type) {
case fmt.Stringer:
txt := val.String()
dmp := diffmatchpatch.New() dmp := diffmatchpatch.New()
if pch, err := dmp.PatchFromText(v.value.(string)); err == nil { if pch, err := dmp.PatchFromText(v.value.(string)); err == nil {
txt, _ := dmp.PatchApply(pch, txt) txt, _ := dmp.PatchApply(pch, txt)
obj.Set(txt, path...) obj.Set(txt, path...)
} }
case string:
dmp := diffmatchpatch.New()
if pch, err := dmp.PatchFromText(v.value.(string)); err == nil {
txt, _ := dmp.PatchApply(pch, val)
obj.Set(txt, path...)
}
} }
} }