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

特定场景无法匹配到TypeConverter #170

Closed
smzgl opened this issue Nov 10, 2022 · 0 comments · Fixed by #180
Closed

特定场景无法匹配到TypeConverter #170

smzgl opened this issue Nov 10, 2022 · 0 comments · Fixed by #180
Assignees
Labels

Comments

@smzgl
Copy link

smzgl commented Nov 10, 2022

copier 在特定场景无法匹配到TypeConverter, 如下面四种场景, 实际上仅有一个能匹配到.

type A struct {
	A int
}

type B struct {
	A int
	b int
}

func Test_A(t *testing.T) {

	opt := copier.Option{
		Converters: []copier.TypeConverter{
			{
				SrcType: A{},
				DstType: B{},
				Fn: func(from interface{}) (interface{}, error) {
					fmt.Print("match TypeConverter ")
					return nil, nil
				},
			},
		},
	}

	{
		aa := A{A: 10}
		bb := B{A: 10, b: 100}
		t.Logf("err: %v", copier.CopyWithOption(&bb, &aa, opt))
	}

	{
		aa := map[string]*A{
			"a": &A{A: 10},
		}

		bb := map[string]*B{
			"a": &B{A: 10, b: 100},
		}

		t.Logf("err: %v", copier.CopyWithOption(&bb, &aa, opt))
	}
	{
		aa := []*A{
			&A{A: 10},
		}

		bb := []*B{
			&B{A: 10, b: 100},
		}

		t.Logf("err: %v", copier.CopyWithOption(&bb, &aa, opt))

		// b := B{A: 10, b: 100}
		// dst2 := []*B{&b}
		// t.Logf("copy a->*b err: %v, b: %+v", copier.CopyWithOption(&dst2, []*A{&a}, opt), b)
	}

	{
		aa := struct {
			A []*A
		}{
			A: []*A{&A{A: 10}},
		}

		bb := struct {
			A []*B
		}{
			A: []*B{&B{A: 10, b: 100}},
		}

		t.Logf("err: %v", copier.CopyWithOption(&bb, &aa, opt))

		// b := B{A: 10, b: 100}
		// dst2 := []*B{&b}
		// t.Logf("copy a->*b err: %v, b: %+v", copier.CopyWithOption(&dst2, []*A{&a}, opt), b)
	}

}
@uded uded added the bug label Jan 7, 2023
driventokill pushed a commit to driventokill/copier that referenced this issue Apr 24, 2023
As mentioned in issue#170 there're several issues in using custom converter:

1. from struct to struct
2. from map to map
3. from slice to slice
4. from embedded slice to embedded slice

All these cases are tested in `copier_issue170_test.go`. There is
a FIXME leaved behind becaused I'm not very familier with types in golang.
It'll be great if someone could fix it for me.

Resolves: jinzhu#170
driventokill added a commit to driventokill/copier that referenced this issue Apr 24, 2023
As mentioned in issue#170 there're several issues in using custom converter:

1. from struct to struct
2. from map to map
3. from slice to slice
4. from embedded slice to embedded slice

All these cases are tested in `copier_issue170_test.go`. There is
a FIXME leaved behind becaused I'm not very familier with types in golang.
It'll be great if someone could fix it for me.

Resolves: jinzhu#170
driventokill added a commit to driventokill/copier that referenced this issue Apr 25, 2023
As mentioned in issue#170 there're several issues in using custom converter:

1. from struct to struct
2. from map to map
3. from slice to slice
4. from embedded slice to embedded slice

All these cases are tested in `copier_issue170_test.go`. There is
a FIXME leaved behind becaused I'm not very familier with types in golang.
It'll be great if someone could fix it for me.

Resolves: jinzhu#170
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants