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

mergo.Map doesn't deal with interface{} when merging slices? #257

Open
Benyamin-Tehrani opened this issue Oct 5, 2024 · 0 comments
Open

Comments

@Benyamin-Tehrani
Copy link

Benyamin-Tehrani commented Oct 5, 2024

It seems that mergo.Map doesn't process with interface{} when merging slices.

Example:

package main

import (
	"dario.cat/mergo"
	"fmt"
)

type SliceStruct struct {
	Values []string
}

func main() {
	dst := SliceStruct{
		Values: []string{"value1", "value2", "value3"},
	}

	src := map[string]interface{}{
		"Values": []interface{}{"newValue1", "newValue2", "newValue3"},
	}

	err := mergo.Map(&dst, src, mergo.WithOverride)
	if err != nil {
		fmt.Println("mergo error:", err)
	}

	fmt.Printf("Updated struct: %+v\n", dst)
}

Error:

panic: reflect.Set: value of type []interface {} is not assignable to type []string
However, the type of Values in map is actually []string, which is the same as that in struct, not only the []interface{}.

Additional:

mergo.WithAppendSlice has the similar problem too:
mergo error: cannot append two slice with different type ([]interface {}, []string)

@Benyamin-Tehrani Benyamin-Tehrani changed the title mergo.Map doesn't check the real Kind() of element when merging slices? mergo.Map doesn't deal with interface{} when merging slices? Oct 5, 2024
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