diff
diff copied to clipboard
Patch failing for byte slices when patching onto struct with nil destination
I'm getting some unexpected behaviour when diffing and applying the changelog for byte slices. In this example I am comparing 2 structs that have a byte slice and patching the result onto an empty struct that has a nil byte slice. Is this supported behaviour?
package main
import (
"fmt"
"github.com/r3labs/diff/v3"
)
type MyType struct {
MyField []byte
}
func main() {
left := &MyType{[]byte{208, 72, 51, 52, 175, 134, 76, 84, 143, 38, 99, 184, 128, 24, 107, 163}}
right := &MyType{[]byte{91, 102, 170, 173, 254, 105, 66, 81, 177, 175, 32, 173, 173, 165, 129, 192}}
changelog, err := diff.Diff(left, right)
if err != nil {
fmt.Println(err)
}
dest := &MyType{}
_ = diff.Patch(changelog, dest)
fmt.Println(left.MyField) // [208 72 51 52 175 134 76 84 143 38 99 184 128 24 107 163]
fmt.Println(right.MyField) // [91 102 170 173 254 105 66 81 177 175 32 173 173 165 129 192]
fmt.Println(dest.MyField) // [32 173 173 165 254 192] ?
// Patchlog errors:
// [MyField 0] 208 91 Value index 0 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 1] 72 102 Value index 1 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 2] 51 170 Value index 2 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 3] 52 173 Value index 3 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 5] 134 105 Value index 5 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 6] 76 66 Value index 6 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 7] 84 81 Value index 7 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 8] 143 177 Value index 8 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 9] 38 <nil> Value index 9 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 10] 99 32 Value index 10 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 11] 184 173 Value index 11 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 12] 128 173 Value index 12 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 13] 24 165 Value index 13 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 14] 107 129 Value index 14 is invalid (cause count 1)
// scanning for Value index (cause count 0)
// [MyField 15] 163 192 Value index 15 is invalid (cause count 1)
// scanning for Value index (cause count 0)
}