package tool import ( "fmt" "testing" "time" "github.com/devfeel/mapper" "github.com/stretchr/testify/assert" ) func ExampleDecode() { type Person struct { Name string Age int Emails []string Extra map[string]string } // This input can come from anywhere, but typically comes from // something like decoding JSON where we're not quite sure of the // struct initially. input := map[string]interface{}{ "name": "Mitchell", "age": 91, "emails": []string{"one", "two", "three"}, "extra": map[string]string{ "twitter": "mitchellh", }, } var result Person err := Decode(input, &result) if err != nil { panic(err) } fmt.Printf("%#v", result) // Output: // tool.Person{Name:"Mitchell", Age:91, Emails:[]string{"one", "two", "three"}, Extra:map[string]string{"twitter":"mitchellh"}} } func TestMapper(t *testing.T) { type ( User struct { Name string Age int Id string `mapper:"_id"` AA string `json:"Score"` Time time.Time } Student struct { Name string Age int Id string `mapper:"_id"` Score string } Teacher struct { Name string Age int Id string `mapper:"_id"` Level string } ) mapper.Register(&User{}) mapper.Register(&Student{}) user := &User{} userMap := &User{} teacher := &Teacher{} student := &Student{Name: "test", Age: 10, Id: "testId", Score: "100"} valMap := make(map[string]interface{}) valMap["Name"] = "map" valMap["Age"] = 10 valMap["_id"] = "x1asd" valMap["Score"] = 100 valMap["Time"] = time.Now() assert.Nil(t, mapper.Mapper(student, user)) assert.Nil(t, mapper.AutoMapper(student, teacher)) assert.Nil(t, mapper.MapperMap(valMap, userMap)) fmt.Println("student:", student) fmt.Println("user:", user) fmt.Println("teacher", teacher) fmt.Println("userMap:", userMap) }