-
-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from driventokill/feature/custom-field-names
feat: Support custom file name mappings
- Loading branch information
Showing
4 changed files
with
100 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package copier_test | ||
|
||
import ( | ||
"github.com/jinzhu/copier" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestCustomFieldName(t *testing.T) { | ||
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"}}, | ||
}}) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if u1.Id != u2.Id2 { | ||
t.Error("copy id failed.") | ||
} | ||
|
||
if u1.Name != u2.Name2 { | ||
t.Error("copy name failed.") | ||
} | ||
|
||
if !reflect.DeepEqual(u1.Address, u2.Address2) { | ||
t.Error("copy address failed.") | ||
} | ||
} |