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

Patching complex struct with map[customType]customSlice fails: value of type string is not assignable to type customMap #111

Open
pikaro opened this issue Nov 9, 2024 · 0 comments

Comments

@pikaro
Copy link

pikaro commented Nov 9, 2024

github.com/r3labs/diff/v3 v3.0.1

I just ran into a bug / unexpected behavior when trying to diff and patch a complex struct with custom types. Not sure if this is out of scope or already covered by the documentation, but it would be great to get some feedback either way.

package main

import (
	"fmt"

	"github.com/r3labs/diff/v3"
)

type (
	A string
	B string
)

type Inner struct {
	Value string
	Other string
}

type Key string

type (
	Slice []Inner
	Map   map[Key]Slice
)

type Outer struct {
	Map Map
}

func main() {
	s := Outer{
		Map: Map{
			"key": Slice{
				{
					Value: "value",
					Other: "other",
				},
			},
		},
	}

	s2 := Outer{
		Map: Map{
			"key": Slice{
				{
					Value: "value",
					Other: "other",
				},
				{
					Value: "valu2",
					Other: "othe2",
				},
			},
		},
	}

	differ, _ := diff.NewDiffer()

	delta, _ := differ.Diff(s, s2)
	fmt.Println(s)
	fmt.Println(s2)
	fmt.Println(delta)

	patchlog := differ.Patch(delta, &s)
	fmt.Printf("Have %d errors\n", patchlog.ErrorCount())

	for _, e := range patchlog {
		if e.Errors != nil {
			fmt.Println(e.Errors)
		}
	}

	fmt.Println(s)
}

This produces:

{map[key:[{value other}]]}
{map[key:[{value other} {valu2 othe2}]]}
[{create [Map key 1 Value] <nil> valu2 <nil>} {create [Map key 1 Other] <nil> othe2 <nil>}]
Have 2 errors
 reflect.Set: value of type string is not assignable to type main.Map (cause count 0)

 reflect.Set: value of type string is not assignable to type main.Map (cause count 0)

{map[key:[{value other}]]}

With diff.DisableStructValues(), I instead get:

{map[key:[{value other}]]}
{map[key:[{value other} {valu2 othe2}]]}
[{create [Map key 1] <nil> {valu2 othe2} <nil>}]
Have 1 errors
 reflect.Set: value of type main.Inner is not assignable to type main.Map (cause count 0)

{map[key:[{value other}]]}

If I remove all the non-struct custom types, it works as it should.

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