package nmapper_test import ( "fmt" "git.noahlan.cn/noahlan/ntool/nmapper" "git.noahlan.cn/noahlan/ntool/nmapper/wrapper" "git.noahlan.cn/noahlan/ntool/nreflect" "reflect" "testing" "time" ) type testInnerType struct { InInt int `json:"in_int"` } type testType struct { Int int `json:"int"` Int8 int8 `json:"int8"` UInt8 uint8 `json:"uint8"` Float32 float32 `json:"float32"` Float64 float64 `json:"float64"` String string `json:"string"` Time time.Time `json:"time,layout=2006-01-02"` TimeNoLayout time.Time `json:"time_no_layout"` Ignore int `json:"-"` *testInnerType TT *testInnerType `json:"tt"` t *testInnerType `json:"t"` } func TestT(t *testing.T) { src := &testType{ Int: 1, Int8: 2, UInt8: 3, Float32: 4.2, Float64: 5.2222222222, String: "test", Time: time.Now(), TimeNoLayout: time.Now().Add(time.Hour * 24), Ignore: 99, testInnerType: &testInnerType{InInt: 111}, TT: &testInnerType{InInt: 222}, t: &testInnerType{InInt: 333}, } valElem := reflect.ValueOf(src) valElem = nreflect.Indirect(valElem) typElem := reflect.TypeOf(src) typElem = typElem.Elem() for i := 0; i < valElem.NumField(); i++ { fieldValElem := valElem.Field(i) fieldTypElem := typElem.Field(i) fieldElem := fieldValElem.Type() fmt.Println(fieldElem.Name(), fieldTypElem.Anonymous, fieldTypElem.Name) fmt.Println(fieldElem.Kind(), fieldTypElem.Type) fmt.Println(fieldElem.String(), fieldTypElem.IsExported()) fmt.Println() //if fieldElem.Kind() == reflect.Ptr { // fff := fieldElem.Elem() // fmt.Println(fff.Name()) // fmt.Println(fff.Kind()) // fmt.Println(fff.String()) //} } } func TestMapper_Mapper(t *testing.T) { mapper := nmapper.NewMapper() dest := make(map[string]any) src := &testType{ Int: 1, Int8: 2, UInt8: 3, Float32: 4.2, Float64: 5.2222222222, String: "test", Time: time.Now(), TimeNoLayout: time.Now().Add(time.Hour * 24), Ignore: 99, testInnerType: &testInnerType{InInt: 111}, TT: &testInnerType{InInt: 222}, } _ = mapper.Mapper(dest, src) dest2 := make(map[string]string) _ = mapper.Mapper(dest2, src) fmt.Printf("dest1:\n%+v\n", dest) fmt.Printf("dest2:\n%+v\n", dest2) } func TestMapper_Parse(t *testing.T) { src := make(map[string]any) src["int"] = 1 src["int8"] = 2 src["uint8"] = 3 src["float32"] = 4.2 src["float64"] = 5.2222222222 src["string"] = "test" src["time"] = time.Now() src["time_no_layout"] = time.Now().Add(time.Hour * 24) src["ignore"] = "ignore" var dest testType mapper := nmapper.NewMapper() _ = mapper.Parse(&dest, src, false) fmt.Printf("%+v\n", dest) } func TestMapperObject_ParseStrMap(t *testing.T) { src := make(map[string]string) src["int"] = "1" src["int8"] = "2" src["uint8"] = "3" src["float32"] = "4.2" src["float64"] = "5.2222222222" src["string"] = "test" src["time"] = "2023-08-18" src["time_no_layout"] = "2023-08-19 10:07:40" src["ignore"] = "ignore" var dest testType mapper := nmapper.NewMapper() _ = mapper.Parse(&dest, src, false) fmt.Printf("%+v\n", dest) } type testFlattenInnerType struct { A int `json:"a"` C int `json:"c"` } type testFlattenInnerType2 struct { B int `json:"b"` AT testFlattenInnerType `json:"t"` } type testFlattenInnerType3 struct { M3 map[string]string `json:"m3"` } type testFlattenType struct { testFlattenInnerType T *testFlattenInnerType `json:"t"` TT *testFlattenInnerType `json:"tt"` T2 testFlattenInnerType2 `json:"t2"` T3 *testFlattenInnerType3 `json:"t3"` M map[string]string `json:"m"` M2 map[string]any `json:"m2"` Int int `json:"int"` Time time.Time `json:"time,layout=2006-01-02"` } func TestParseFlatten(t *testing.T) { src := make(map[string]string) src["a"] = "1" src["c"] = "2" src["t.a"] = "1" src["tt.a"] = "11" src["tt.c"] = "33" src["t2.b"] = "2" src["t2.t.a"] = "3" src["t2.at.a"] = "4" src["t3.m3.a"] = "a" src["t3.m3.b"] = "b" src["m.dd"] = "x.xx" src["m2.a1.b1"] = "xx" src["m2.a2.b2.c2"] = "xx" src["int"] = "99" src["time"] = "2010-09-18" var dest testFlattenType mapper := nmapper.NewMapper() mapper.SetTypeWrapper(wrapper.StrWrapper()...) _ = mapper.Parse(&dest, src, true) fmt.Printf("%+v\n", dest) } func TestParse(t *testing.T) { src := make(map[string]any) src["t"] = map[string]any{"a": "1"} src["tt"] = map[string]any{"a": "11", "c": "33"} src["t2"] = map[string]any{"b": "2", "t": map[string]any{"a": "3"}, "at": map[string]any{"a": "4"}} src["m"] = map[string]string{"dd": "x.xx"} src["m2"] = map[string]any{"a1": map[string]any{"b1": "xx"}, "a2": map[string]any{"b2": map[string]any{"c2": "xx"}}} src["int"] = "99" src["time"] = "2010-09-18" src["a"] = "1" var dest testFlattenType mapper := nmapper.NewMapper() mapper.SetTypeWrapper(wrapper.StrWrapper()...) _ = mapper.Parse(&dest, src, false) fmt.Printf("%+v\n", dest) }