jinzhu / copier

Copier for golang, copy value from struct to struct and more
MIT License
5.52k stars 485 forks source link

feat: Support custom file name mappings #190

Closed driventokill closed 1 year ago

driventokill commented 1 year ago

Custom field name mappings to copy values with different names in fromValue and toValue types. Examples can be found in copier_different_field_name_test.go.

resolve #139

type User1 struct {
    Id      int64
    Name    string
    Address []string
}

type User2 struct {
    Id2      int64
    Name2    string
    Address2 []string
}

u1 := User1{Id: 1, Name: "1", Address: []string{"1"}}
var u2 User2
err := copier.CopyWithOption(&u2, u1, copier.Option{FieldNameMapping: []copier.FieldNameMapping{
    {SrcType: u1, DstType: u2,
        Mapping: map[string]string{
            "Id":      "Id2",
            "Name":    "Name2",
            "Address": "Address2"}},
}})
driventokill commented 1 year ago

@jinzhu The failed cases seem like been broken since f036a4283eebfc1d291803f7255d2d08be52f5eb (Merge pull request #178 from QianChenglong/master)