github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/ztype/map_test.go (about)

     1  package ztype
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/sohaha/zlsgo"
     9  	"github.com/sohaha/zlsgo/zreflect"
    10  )
    11  
    12  func TestMap(t *testing.T) {
    13  	T := zlsgo.NewTest(t)
    14  	m := make(map[interface{}]interface{})
    15  	m["T"] = "test"
    16  	tMapKeyExists := MapKeyExists("T", m)
    17  	T.Equal(true, tMapKeyExists)
    18  }
    19  
    20  func TestMapCopy(t *testing.T) {
    21  	tt := zlsgo.NewTest(t)
    22  
    23  	z := Map{"a": 1}
    24  	m := Map{"1": 1, "z": z}
    25  	m2 := m.DeepCopy()
    26  	m3 := m
    27  
    28  	tt.Equal(m, m2)
    29  	tt.Equal(m, m3)
    30  
    31  	m["1"] = 2
    32  	z["a"] = 2
    33  
    34  	tt.EqualTrue(m.Get("z.a").String() != m2.Get("z.a").String())
    35  	t.Log(m, m2, m3)
    36  	tt.EqualTrue(m.Get("z.a").String() == m3.Get("z.a").String())
    37  }
    38  
    39  func TestMapNil(t *testing.T) {
    40  	tt := zlsgo.NewTest(t)
    41  
    42  	var m Map
    43  	tt.Equal(true, m.IsEmpty())
    44  	err := m.Delete("no")
    45  	t.Log(err)
    46  
    47  	err = m.Set("val", "99")
    48  	t.Log(err)
    49  	tt.EqualTrue(err != nil)
    50  
    51  	var m2 = &Map{}
    52  	tt.Equal(true, m2.IsEmpty())
    53  	err = m.Delete("no")
    54  	t.Log(err)
    55  
    56  	err = m2.Set("val", "99")
    57  	tt.NoError(err)
    58  	tt.Equal("99", m2.Get("val").String())
    59  }
    60  
    61  type other struct {
    62  	Sex int
    63  }
    64  
    65  type (
    66  	Str string
    67  	Obj struct {
    68  		Name Str `json:"name"`
    69  	}
    70  	u struct {
    71  		Other  *other
    72  		Name   Str                      `json:"name"`
    73  		Region struct{ Country string } `json:"reg"`
    74  		Objs   []Obj
    75  		Key    int
    76  		Status bool
    77  	}
    78  )
    79  
    80  var user = &u{
    81  	Name: "n666",
    82  	Key:  9,
    83  	Objs: []Obj{
    84  		{"n1"},
    85  		{"n2"},
    86  		{"n3"},
    87  		{"n4"},
    88  		{"n5"},
    89  	},
    90  	Status: true,
    91  	Region: struct {
    92  		Country string
    93  	}{"中国"},
    94  }
    95  
    96  func TestToMap(T *testing.T) {
    97  	t := zlsgo.NewTest(T)
    98  	type u struct {
    99  		Name   string
   100  		Key    int
   101  		Status bool
   102  	}
   103  	user := &u{
   104  		Name:   "666",
   105  		Key:    9,
   106  		Status: true,
   107  	}
   108  	userMap := Map{
   109  		"Name":   user.Name,
   110  		"Status": user.Status,
   111  		"Key":    user.Key,
   112  	}
   113  	toUserMap := ToMap(user)
   114  	t.Log(user)
   115  	t.Log(userMap)
   116  	t.Log(toUserMap)
   117  	t.Equal(userMap, toUserMap)
   118  
   119  	t.Equal(1, ToMap(map[interface{}]interface{}{"name": 1}).Get("name").Int())
   120  	t.Equal(1, ToMap(map[interface{}]string{"name": "1"}).Get("name").Int())
   121  	t.Equal(1, ToMap(map[interface{}]int{"name": 1}).Get("name").Int())
   122  	t.Equal(1, ToMap(map[interface{}]uint{"name": 1}).Get("name").Int())
   123  	t.Equal(1, ToMap(map[interface{}]float64{"name": 1}).Get("name").Int())
   124  	t.Equal(1, ToMap(map[interface{}]bool{"name": true}).Get("name").Int())
   125  	t.Equal(1, ToMap(map[string]interface{}{"name": 1}).Get("name").Int())
   126  	t.Equal(1, ToMap(map[string]string{"name": "1"}).Get("name").Int())
   127  	t.Equal(1, ToMap(map[string]int{"name": 1}).Get("name").Int())
   128  	t.Equal(1, ToMap(map[string]uint{"name": 1}).Get("name").Int())
   129  	t.Equal(1, ToMap(map[string]float64{"name": 1}).Get("name").Int())
   130  	t.Equal(1, ToMap(map[string]bool{"name": true}).Get("name").Int())
   131  }
   132  
   133  func TestToMaps(T *testing.T) {
   134  	t := zlsgo.NewTest(T)
   135  	type u struct {
   136  		Other  *other
   137  		Name   string
   138  		Key    int
   139  		Status bool
   140  	}
   141  	var rawData = make([]u, 2)
   142  	rawData[0] = u{
   143  		Name:   "666",
   144  		Key:    9,
   145  		Status: true,
   146  		Other: &other{
   147  			Sex: 18,
   148  		},
   149  	}
   150  	rawData[1] = u{
   151  		Name:   "666",
   152  		Key:    9,
   153  		Status: true,
   154  	}
   155  	toSliceMapString := ToMaps(rawData)
   156  	t.Log(toSliceMapString)
   157  	t.Equal(18, toSliceMapString[0].Get("Other").Get("Sex").Int())
   158  
   159  	var data = make([]map[string]interface{}, 2)
   160  	data[0] = map[string]interface{}{"name": "hi"}
   161  	data[1] = map[string]interface{}{"name": "golang"}
   162  	toSliceMapString = ToMaps(data)
   163  	t.Equal("hi", toSliceMapString.Index(0).Get("name").String())
   164  
   165  	data2 := Maps{{"name": "hi"}, {"name": "golang"}, {"name": "!"}}
   166  	toSliceMapString = ToMaps(data2)
   167  	t.Equal("hi", toSliceMapString.Index(0).Get("name").String())
   168  	t.EqualTrue(!data2.IsEmpty())
   169  
   170  	t.Equal("hi", data2.First().Get("name").String())
   171  	t.Equal("!", data2.Last().Get("name").String())
   172  }
   173  
   174  func TestConvContainTime(t *testing.T) {
   175  	tt := zlsgo.NewTest(t)
   176  
   177  	type JsonTime time.Time
   178  
   179  	type S struct {
   180  		Date1 JsonTime `z:"date"`
   181  		Date2 time.Time
   182  		Name  string
   183  	}
   184  
   185  	now := time.Now()
   186  	v := map[string]interface{}{
   187  		"date":  now,
   188  		"Date2": now,
   189  		"Name":  "123",
   190  	}
   191  
   192  	var s S
   193  	isTime := zreflect.TypeOf(time.Time{})
   194  	err := To(v, &s, func(conver *Conver) {
   195  		conver.ConvHook = func(i reflect.Value, o reflect.Type) (reflect.Value, error) {
   196  			t := i.Type()
   197  			if t == isTime && t.ConvertibleTo(o) {
   198  				return i.Convert(o), nil
   199  			}
   200  			return i, nil
   201  		}
   202  	})
   203  	tt.NoError(err)
   204  
   205  	tt.Equal(now.Unix(), time.Time(s.Date1).Unix())
   206  	tt.Equal(now.Unix(), s.Date2.Unix())
   207  }
   208  
   209  func BenchmarkName(b *testing.B) {
   210  
   211  	b.Run("toMapString", func(b *testing.B) {
   212  		b.ReportAllocs()
   213  		b.ResetTimer()
   214  		for i := 0; i < b.N; i++ {
   215  			_ = toMapString(user)
   216  		}
   217  	})
   218  
   219  	b.Run("ToMap", func(b *testing.B) {
   220  		b.ReportAllocs()
   221  		b.ResetTimer()
   222  		for i := 0; i < b.N; i++ {
   223  			_ = ToMap(user)
   224  		}
   225  	})
   226  
   227  	b.Run("toMapString", func(b *testing.B) {
   228  		b.ReportAllocs()
   229  		b.ResetTimer()
   230  		b.RunParallel(func(pb *testing.PB) {
   231  			for pb.Next() {
   232  				_ = toMapString(user)
   233  			}
   234  		})
   235  	})
   236  
   237  	b.Run("ToMap", func(b *testing.B) {
   238  		b.ReportAllocs()
   239  		b.ResetTimer()
   240  		b.RunParallel(func(pb *testing.PB) {
   241  			for pb.Next() {
   242  				_ = ToMap(user)
   243  			}
   244  		})
   245  	})
   246  }