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

Patch failing for byte slices when patching onto struct with nil destination #90

Open
valantonini opened this issue Jul 20, 2022 · 0 comments

Comments

@valantonini
Copy link

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?

Go Playground

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)

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant