github.com/gogf/gf@v1.16.9/util/gconv/gconv_z_unit_map_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gconv_test
     8  
     9  import (
    10  	"github.com/gogf/gf/util/gutil"
    11  	"testing"
    12  
    13  	"github.com/gogf/gf/frame/g"
    14  	"github.com/gogf/gf/test/gtest"
    15  	"github.com/gogf/gf/util/gconv"
    16  )
    17  
    18  func Test_Map_Basic(t *testing.T) {
    19  	gtest.C(t, func(t *gtest.T) {
    20  		m1 := map[string]string{
    21  			"k": "v",
    22  		}
    23  		m2 := map[int]string{
    24  			3: "v",
    25  		}
    26  		m3 := map[float64]float32{
    27  			1.22: 3.1,
    28  		}
    29  		t.Assert(gconv.Map(m1), g.Map{
    30  			"k": "v",
    31  		})
    32  		t.Assert(gconv.Map(m2), g.Map{
    33  			"3": "v",
    34  		})
    35  		t.Assert(gconv.Map(m3), g.Map{
    36  			"1.22": "3.1",
    37  		})
    38  	})
    39  }
    40  
    41  func Test_Map_Slice(t *testing.T) {
    42  	gtest.C(t, func(t *gtest.T) {
    43  		slice1 := g.Slice{"1", "2", "3", "4"}
    44  		slice2 := g.Slice{"1", "2", "3"}
    45  		slice3 := g.Slice{}
    46  		t.Assert(gconv.Map(slice1), g.Map{
    47  			"1": "2",
    48  			"3": "4",
    49  		})
    50  		t.Assert(gconv.Map(slice2), g.Map{
    51  			"1": "2",
    52  			"3": nil,
    53  		})
    54  		t.Assert(gconv.Map(slice3), g.Map{})
    55  	})
    56  }
    57  
    58  func Test_Maps_Basic(t *testing.T) {
    59  	params := g.Slice{
    60  		g.Map{"id": 100, "name": "john"},
    61  		g.Map{"id": 200, "name": "smith"},
    62  	}
    63  	gtest.C(t, func(t *gtest.T) {
    64  		list := gconv.Maps(params)
    65  		t.Assert(len(list), 2)
    66  		t.Assert(list[0]["id"], 100)
    67  		t.Assert(list[1]["id"], 200)
    68  	})
    69  }
    70  
    71  func Test_Maps_JsonStr(t *testing.T) {
    72  	jsonStr := `[{"id":100, "name":"john"},{"id":200, "name":"smith"}]`
    73  	gtest.C(t, func(t *gtest.T) {
    74  		list := gconv.Maps(jsonStr)
    75  		t.Assert(len(list), 2)
    76  		t.Assert(list[0]["id"], 100)
    77  		t.Assert(list[1]["id"], 200)
    78  	})
    79  }
    80  
    81  func Test_Map_StructWithGConvTag(t *testing.T) {
    82  	gtest.C(t, func(t *gtest.T) {
    83  		type User struct {
    84  			Uid      int
    85  			Name     string
    86  			SiteUrl  string `gconv:"-"`
    87  			NickName string `gconv:"nickname, omitempty"`
    88  			Pass1    string `gconv:"password1"`
    89  			Pass2    string `gconv:"password2"`
    90  		}
    91  		user1 := User{
    92  			Uid:     100,
    93  			Name:    "john",
    94  			SiteUrl: "https://goframe.org",
    95  			Pass1:   "123",
    96  			Pass2:   "456",
    97  		}
    98  		user2 := &user1
    99  		map1 := gconv.Map(user1)
   100  		map2 := gconv.Map(user2)
   101  		t.Assert(map1["Uid"], 100)
   102  		t.Assert(map1["Name"], "john")
   103  		t.Assert(map1["SiteUrl"], nil)
   104  		t.Assert(map1["NickName"], nil)
   105  		t.Assert(map1["nickname"], nil)
   106  		t.Assert(map1["password1"], "123")
   107  		t.Assert(map1["password2"], "456")
   108  
   109  		t.Assert(map2["Uid"], 100)
   110  		t.Assert(map2["Name"], "john")
   111  		t.Assert(map2["SiteUrl"], nil)
   112  		t.Assert(map2["NickName"], nil)
   113  		t.Assert(map2["nickname"], nil)
   114  		t.Assert(map2["password1"], "123")
   115  		t.Assert(map2["password2"], "456")
   116  	})
   117  }
   118  
   119  func Test_Map_StructWithJsonTag(t *testing.T) {
   120  	gtest.C(t, func(t *gtest.T) {
   121  		type User struct {
   122  			Uid      int
   123  			Name     string
   124  			SiteUrl  string `json:"-"`
   125  			NickName string `json:"nickname, omitempty"`
   126  			Pass1    string `json:"password1"`
   127  			Pass2    string `json:"password2"`
   128  		}
   129  		user1 := User{
   130  			Uid:     100,
   131  			Name:    "john",
   132  			SiteUrl: "https://goframe.org",
   133  			Pass1:   "123",
   134  			Pass2:   "456",
   135  		}
   136  		user2 := &user1
   137  		map1 := gconv.Map(user1)
   138  		map2 := gconv.Map(user2)
   139  		t.Assert(map1["Uid"], 100)
   140  		t.Assert(map1["Name"], "john")
   141  		t.Assert(map1["SiteUrl"], nil)
   142  		t.Assert(map1["NickName"], nil)
   143  		t.Assert(map1["nickname"], nil)
   144  		t.Assert(map1["password1"], "123")
   145  		t.Assert(map1["password2"], "456")
   146  
   147  		t.Assert(map2["Uid"], 100)
   148  		t.Assert(map2["Name"], "john")
   149  		t.Assert(map2["SiteUrl"], nil)
   150  		t.Assert(map2["NickName"], nil)
   151  		t.Assert(map2["nickname"], nil)
   152  		t.Assert(map2["password1"], "123")
   153  		t.Assert(map2["password2"], "456")
   154  	})
   155  }
   156  
   157  func Test_Map_StructWithCTag(t *testing.T) {
   158  	gtest.C(t, func(t *gtest.T) {
   159  		type User struct {
   160  			Uid      int
   161  			Name     string
   162  			SiteUrl  string `c:"-"`
   163  			NickName string `c:"nickname, omitempty"`
   164  			Pass1    string `c:"password1"`
   165  			Pass2    string `c:"password2"`
   166  		}
   167  		user1 := User{
   168  			Uid:     100,
   169  			Name:    "john",
   170  			SiteUrl: "https://goframe.org",
   171  			Pass1:   "123",
   172  			Pass2:   "456",
   173  		}
   174  		user2 := &user1
   175  		map1 := gconv.Map(user1)
   176  		map2 := gconv.Map(user2)
   177  		t.Assert(map1["Uid"], 100)
   178  		t.Assert(map1["Name"], "john")
   179  		t.Assert(map1["SiteUrl"], nil)
   180  		t.Assert(map1["NickName"], nil)
   181  		t.Assert(map1["nickname"], nil)
   182  		t.Assert(map1["password1"], "123")
   183  		t.Assert(map1["password2"], "456")
   184  
   185  		t.Assert(map2["Uid"], 100)
   186  		t.Assert(map2["Name"], "john")
   187  		t.Assert(map2["SiteUrl"], nil)
   188  		t.Assert(map2["NickName"], nil)
   189  		t.Assert(map2["nickname"], nil)
   190  		t.Assert(map2["password1"], "123")
   191  		t.Assert(map2["password2"], "456")
   192  	})
   193  }
   194  
   195  func Test_Map_PrivateAttribute(t *testing.T) {
   196  	type User struct {
   197  		Id   int
   198  		name string
   199  	}
   200  	gtest.C(t, func(t *gtest.T) {
   201  		user := &User{1, "john"}
   202  		t.Assert(gconv.Map(user), g.Map{"Id": 1})
   203  	})
   204  }
   205  
   206  func Test_Map_Embedded(t *testing.T) {
   207  	type Base struct {
   208  		Id int
   209  	}
   210  	type User struct {
   211  		Base
   212  		Name string
   213  	}
   214  	type UserDetail struct {
   215  		User
   216  		Brief string
   217  	}
   218  	gtest.C(t, func(t *gtest.T) {
   219  		user := &User{}
   220  		user.Id = 1
   221  		user.Name = "john"
   222  
   223  		m := gconv.Map(user)
   224  		t.Assert(len(m), 2)
   225  		t.Assert(m["Id"], user.Id)
   226  		t.Assert(m["Name"], user.Name)
   227  	})
   228  	gtest.C(t, func(t *gtest.T) {
   229  		user := &UserDetail{}
   230  		user.Id = 1
   231  		user.Name = "john"
   232  		user.Brief = "john guo"
   233  
   234  		m := gconv.Map(user)
   235  		t.Assert(len(m), 3)
   236  		t.Assert(m["Id"], user.Id)
   237  		t.Assert(m["Name"], user.Name)
   238  		t.Assert(m["Brief"], user.Brief)
   239  	})
   240  }
   241  
   242  func Test_Map_Embedded2(t *testing.T) {
   243  	type Ids struct {
   244  		Id  int `c:"id"`
   245  		Uid int `c:"uid"`
   246  	}
   247  	type Base struct {
   248  		Ids
   249  		CreateTime string `c:"create_time"`
   250  	}
   251  	type User struct {
   252  		Base
   253  		Passport string `c:"passport"`
   254  		Password string `c:"password"`
   255  		Nickname string `c:"nickname"`
   256  	}
   257  	gtest.C(t, func(t *gtest.T) {
   258  		user := new(User)
   259  		user.Id = 100
   260  		user.Nickname = "john"
   261  		user.CreateTime = "2019"
   262  		m := gconv.Map(user)
   263  		t.Assert(m["id"], "100")
   264  		t.Assert(m["nickname"], user.Nickname)
   265  		t.Assert(m["create_time"], "2019")
   266  	})
   267  	gtest.C(t, func(t *gtest.T) {
   268  		user := new(User)
   269  		user.Id = 100
   270  		user.Nickname = "john"
   271  		user.CreateTime = "2019"
   272  		m := gconv.MapDeep(user)
   273  		t.Assert(m["id"], user.Id)
   274  		t.Assert(m["nickname"], user.Nickname)
   275  		t.Assert(m["create_time"], user.CreateTime)
   276  	})
   277  }
   278  
   279  func Test_MapDeep2(t *testing.T) {
   280  	type A struct {
   281  		F string
   282  		G string
   283  	}
   284  
   285  	type B struct {
   286  		A
   287  		H string
   288  	}
   289  
   290  	type C struct {
   291  		A A
   292  		F string
   293  	}
   294  
   295  	type D struct {
   296  		I A
   297  		F string
   298  	}
   299  
   300  	gtest.C(t, func(t *gtest.T) {
   301  		b := new(B)
   302  		c := new(C)
   303  		d := new(D)
   304  		mb := gconv.MapDeep(b)
   305  		mc := gconv.MapDeep(c)
   306  		md := gconv.MapDeep(d)
   307  		t.Assert(gutil.MapContains(mb, "F"), true)
   308  		t.Assert(gutil.MapContains(mb, "G"), true)
   309  		t.Assert(gutil.MapContains(mb, "H"), true)
   310  		t.Assert(gutil.MapContains(mc, "A"), true)
   311  		t.Assert(gutil.MapContains(mc, "F"), true)
   312  		t.Assert(gutil.MapContains(mc, "G"), false)
   313  		t.Assert(gutil.MapContains(md, "F"), true)
   314  		t.Assert(gutil.MapContains(md, "I"), true)
   315  		t.Assert(gutil.MapContains(md, "H"), false)
   316  		t.Assert(gutil.MapContains(md, "G"), false)
   317  	})
   318  }
   319  
   320  func Test_MapDeep3(t *testing.T) {
   321  	type Base struct {
   322  		Id   int    `c:"id"`
   323  		Date string `c:"date"`
   324  	}
   325  	type User struct {
   326  		UserBase Base   `c:"base"`
   327  		Passport string `c:"passport"`
   328  		Password string `c:"password"`
   329  		Nickname string `c:"nickname"`
   330  	}
   331  
   332  	gtest.C(t, func(t *gtest.T) {
   333  		user := &User{
   334  			UserBase: Base{
   335  				Id:   1,
   336  				Date: "2019-10-01",
   337  			},
   338  			Passport: "john",
   339  			Password: "123456",
   340  			Nickname: "JohnGuo",
   341  		}
   342  		m := gconv.MapDeep(user)
   343  		t.Assert(m, g.Map{
   344  			"base": g.Map{
   345  				"id":   user.UserBase.Id,
   346  				"date": user.UserBase.Date,
   347  			},
   348  			"passport": user.Passport,
   349  			"password": user.Password,
   350  			"nickname": user.Nickname,
   351  		})
   352  	})
   353  
   354  	gtest.C(t, func(t *gtest.T) {
   355  		user := &User{
   356  			UserBase: Base{
   357  				Id:   1,
   358  				Date: "2019-10-01",
   359  			},
   360  			Passport: "john",
   361  			Password: "123456",
   362  			Nickname: "JohnGuo",
   363  		}
   364  		m := gconv.Map(user)
   365  		t.Assert(m, g.Map{
   366  			"base":     user.UserBase,
   367  			"passport": user.Passport,
   368  			"password": user.Password,
   369  			"nickname": user.Nickname,
   370  		})
   371  	})
   372  }
   373  
   374  func Test_MapDeepWithAttributeTag(t *testing.T) {
   375  	type Ids struct {
   376  		Id  int `c:"id"`
   377  		Uid int `c:"uid"`
   378  	}
   379  	type Base struct {
   380  		Ids        `json:"ids"`
   381  		CreateTime string `c:"create_time"`
   382  	}
   383  	type User struct {
   384  		Base     `json:"base"`
   385  		Passport string `c:"passport"`
   386  		Password string `c:"password"`
   387  		Nickname string `c:"nickname"`
   388  	}
   389  	gtest.C(t, func(t *gtest.T) {
   390  		user := new(User)
   391  		user.Id = 100
   392  		user.Nickname = "john"
   393  		user.CreateTime = "2019"
   394  		m := gconv.Map(user)
   395  		t.Assert(m["id"], "")
   396  		t.Assert(m["nickname"], user.Nickname)
   397  		t.Assert(m["create_time"], "")
   398  	})
   399  	gtest.C(t, func(t *gtest.T) {
   400  		user := new(User)
   401  		user.Id = 100
   402  		user.Nickname = "john"
   403  		user.CreateTime = "2019"
   404  		m := gconv.MapDeep(user)
   405  		t.Assert(m["base"].(map[string]interface{})["ids"].(map[string]interface{})["id"], user.Id)
   406  		t.Assert(m["nickname"], user.Nickname)
   407  		t.Assert(m["base"].(map[string]interface{})["create_time"], user.CreateTime)
   408  	})
   409  }