You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nmapper/mapper_object_test.go

90 lines
2.0 KiB
Go

package nmapper_test
import (
"fmt"
"git.noahlan.cn/noahlan/ntool/nmapper"
"git.noahlan.cn/noahlan/ntool/nmapper/wrapper"
"testing"
"time"
)
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:"-"`
}
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,
}
_ = mapper.Mapper(dest, src)
dest2 := make(map[string]string)
mapper.SetTypeWrapper(wrapper.StrWrapper()...)
_ = mapper.Mapper(dest2, src)
mapper.SetTypeWrapper()
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.ParseMap(&dest, src)
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 10:07:40"
src["time_no_layout"] = "2023-08-19 10:07:40"
src["ignore"] = "ignore"
var dest testType
mapper := nmapper.NewMapper()
mapper.SetTypeWrapper(wrapper.StrWrapper()...)
_ = mapper.ParseStrMap(&dest, src)
fmt.Printf("%+v\n", dest)
}