Skip to content

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

Open
@Benyamin-Tehrani

Description

@Benyamin-Tehrani

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions