github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_z_unit_issue_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/wangyougui/gf.
     6  
     7  package gconv_test
     8  
     9  import (
    10  	"math/big"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/wangyougui/gf/v2/container/gtype"
    15  	"github.com/wangyougui/gf/v2/encoding/gjson"
    16  	"github.com/wangyougui/gf/v2/frame/g"
    17  	"github.com/wangyougui/gf/v2/os/gtime"
    18  	"github.com/wangyougui/gf/v2/test/gtest"
    19  	"github.com/wangyougui/gf/v2/util/gconv"
    20  )
    21  
    22  // https://github.com/wangyougui/gf/issues/1227
    23  func Test_Issue1227(t *testing.T) {
    24  	gtest.C(t, func(t *gtest.T) {
    25  		type StructFromIssue1227 struct {
    26  			Name string `json:"n1"`
    27  		}
    28  		tests := []struct {
    29  			name   string
    30  			origin interface{}
    31  			want   string
    32  		}{
    33  			{
    34  				name:   "Case1",
    35  				origin: `{"n1":"n1"}`,
    36  				want:   "n1",
    37  			},
    38  			{
    39  				name:   "Case2",
    40  				origin: `{"name":"name"}`,
    41  				want:   "",
    42  			},
    43  			{
    44  				name:   "Case3",
    45  				origin: `{"NaMe":"NaMe"}`,
    46  				want:   "",
    47  			},
    48  			{
    49  				name:   "Case4",
    50  				origin: g.Map{"n1": "n1"},
    51  				want:   "n1",
    52  			},
    53  			{
    54  				name:   "Case5",
    55  				origin: g.Map{"NaMe": "n1"},
    56  				want:   "n1",
    57  			},
    58  		}
    59  		for _, tt := range tests {
    60  			p := StructFromIssue1227{}
    61  			if err := gconv.Struct(tt.origin, &p); err != nil {
    62  				t.Error(err)
    63  			}
    64  			t.Assert(p.Name, tt.want)
    65  		}
    66  	})
    67  
    68  	// Chinese key.
    69  	gtest.C(t, func(t *gtest.T) {
    70  		type StructFromIssue1227 struct {
    71  			Name string `json:"中文Key"`
    72  		}
    73  		tests := []struct {
    74  			name   string
    75  			origin interface{}
    76  			want   string
    77  		}{
    78  			{
    79  				name:   "Case1",
    80  				origin: `{"中文Key":"n1"}`,
    81  				want:   "n1",
    82  			},
    83  			{
    84  				name:   "Case2",
    85  				origin: `{"Key":"name"}`,
    86  				want:   "",
    87  			},
    88  			{
    89  				name:   "Case3",
    90  				origin: `{"NaMe":"NaMe"}`,
    91  				want:   "",
    92  			},
    93  			{
    94  				name:   "Case4",
    95  				origin: g.Map{"中文Key": "n1"},
    96  				want:   "n1",
    97  			},
    98  			{
    99  				name:   "Case5",
   100  				origin: g.Map{"中文KEY": "n1"},
   101  				want:   "n1",
   102  			},
   103  			{
   104  				name:   "Case5",
   105  				origin: g.Map{"KEY": "n1"},
   106  				want:   "",
   107  			},
   108  		}
   109  		for _, tt := range tests {
   110  			p := StructFromIssue1227{}
   111  			if err := gconv.Struct(tt.origin, &p); err != nil {
   112  				t.Error(err)
   113  			}
   114  			t.Assert(p.Name, tt.want)
   115  		}
   116  	})
   117  }
   118  
   119  // https://github.com/wangyougui/gf/issues/1607
   120  func Test_Issue1607(t *testing.T) {
   121  	gtest.C(t, func(t *gtest.T) {
   122  		type Demo struct {
   123  			B Float64
   124  		}
   125  		rat := &big.Rat{}
   126  		rat.SetFloat64(1.5)
   127  
   128  		var demos = make([]Demo, 1)
   129  		err := gconv.Scan([]map[string]interface{}{
   130  			{"A": 1, "B": rat},
   131  		}, &demos)
   132  		t.AssertNil(err)
   133  		t.Assert(demos[0].B, 1.5)
   134  	})
   135  }
   136  
   137  // https://github.com/wangyougui/gf/issues/1946
   138  func Test_Issue1946(t *testing.T) {
   139  	gtest.C(t, func(t *gtest.T) {
   140  		type B struct {
   141  			init *gtype.Bool
   142  			Name string
   143  		}
   144  		type A struct {
   145  			B *B
   146  		}
   147  		a := &A{
   148  			B: &B{
   149  				init: gtype.NewBool(true),
   150  			},
   151  		}
   152  		err := gconv.Struct(g.Map{
   153  			"B": g.Map{
   154  				"Name": "init",
   155  			},
   156  		}, a)
   157  		t.AssertNil(err)
   158  		t.Assert(a.B.Name, "init")
   159  		t.Assert(a.B.init.Val(), true)
   160  	})
   161  	// It cannot change private attribute.
   162  	gtest.C(t, func(t *gtest.T) {
   163  		type B struct {
   164  			init *gtype.Bool
   165  			Name string
   166  		}
   167  		type A struct {
   168  			B *B
   169  		}
   170  		a := &A{
   171  			B: &B{
   172  				init: gtype.NewBool(true),
   173  			},
   174  		}
   175  		err := gconv.Struct(g.Map{
   176  			"B": g.Map{
   177  				"init": 0,
   178  				"Name": "init",
   179  			},
   180  		}, a)
   181  		t.AssertNil(err)
   182  		t.Assert(a.B.Name, "init")
   183  		t.Assert(a.B.init.Val(), true)
   184  	})
   185  	// It can change public attribute.
   186  	gtest.C(t, func(t *gtest.T) {
   187  		type B struct {
   188  			Init *gtype.Bool
   189  			Name string
   190  		}
   191  		type A struct {
   192  			B *B
   193  		}
   194  		a := &A{
   195  			B: &B{
   196  				Init: gtype.NewBool(),
   197  			},
   198  		}
   199  		err := gconv.Struct(g.Map{
   200  			"B": g.Map{
   201  				"Init": 1,
   202  				"Name": "init",
   203  			},
   204  		}, a)
   205  		t.AssertNil(err)
   206  		t.Assert(a.B.Name, "init")
   207  		t.Assert(a.B.Init.Val(), true)
   208  	})
   209  }
   210  
   211  // https://github.com/wangyougui/gf/issues/2381
   212  func Test_Issue2381(t *testing.T) {
   213  	gtest.C(t, func(t *gtest.T) {
   214  		type Inherit struct {
   215  			Id        int64       `json:"id"          description:"Id"`
   216  			Flag      *gjson.Json `json:"flag"        description:"标签"`
   217  			Title     string      `json:"title"       description:"标题"`
   218  			CreatedAt *gtime.Time `json:"createdAt"   description:"创建时间"`
   219  		}
   220  		type Test1 struct {
   221  			Inherit
   222  		}
   223  		type Test2 struct {
   224  			Inherit
   225  		}
   226  		var (
   227  			a1 Test1
   228  			a2 Test2
   229  		)
   230  
   231  		a1 = Test1{
   232  			Inherit{
   233  				Id:        2,
   234  				Flag:      gjson.New("[1, 2]"),
   235  				Title:     "测试",
   236  				CreatedAt: gtime.Now(),
   237  			},
   238  		}
   239  		err := gconv.Scan(a1, &a2)
   240  		t.AssertNil(err)
   241  		t.Assert(a1.Id, a2.Id)
   242  		t.Assert(a1.Title, a2.Title)
   243  		t.Assert(a1.CreatedAt, a2.CreatedAt)
   244  		t.Assert(a1.Flag.String(), a2.Flag.String())
   245  	})
   246  }
   247  
   248  // https://github.com/wangyougui/gf/issues/2391
   249  func Test_Issue2391(t *testing.T) {
   250  	gtest.C(t, func(t *gtest.T) {
   251  		type Inherit struct {
   252  			Ids   []int
   253  			Ids2  []int64
   254  			Flag  *gjson.Json
   255  			Title string
   256  		}
   257  
   258  		type Test1 struct {
   259  			Inherit
   260  		}
   261  		type Test2 struct {
   262  			Inherit
   263  		}
   264  
   265  		var (
   266  			a1 Test1
   267  			a2 Test2
   268  		)
   269  
   270  		a1 = Test1{
   271  			Inherit{
   272  				Ids:   []int{1, 2, 3},
   273  				Ids2:  []int64{4, 5, 6},
   274  				Flag:  gjson.New("[\"1\", \"2\"]"),
   275  				Title: "测试",
   276  			},
   277  		}
   278  
   279  		err := gconv.Scan(a1, &a2)
   280  		t.AssertNil(err)
   281  		t.Assert(a1.Ids, a2.Ids)
   282  		t.Assert(a1.Ids2, a2.Ids2)
   283  		t.Assert(a1.Title, a2.Title)
   284  		t.Assert(a1.Flag.String(), a2.Flag.String())
   285  	})
   286  }
   287  
   288  // https://github.com/wangyougui/gf/issues/2395
   289  func Test_Issue2395(t *testing.T) {
   290  	gtest.C(t, func(t *gtest.T) {
   291  		type Test struct {
   292  			Num int
   293  		}
   294  		var ()
   295  		obj := Test{Num: 0}
   296  		t.Assert(gconv.Interfaces(obj), []interface{}{obj})
   297  	})
   298  }
   299  
   300  // https://github.com/wangyougui/gf/issues/2371
   301  func Test_Issue2371(t *testing.T) {
   302  	gtest.C(t, func(t *gtest.T) {
   303  		var (
   304  			s = struct {
   305  				Time time.Time `json:"time"`
   306  			}{}
   307  			jsonMap = map[string]interface{}{"time": "2022-12-15 16:11:34"}
   308  		)
   309  
   310  		err := gconv.Struct(jsonMap, &s)
   311  		t.AssertNil(err)
   312  		t.Assert(s.Time.UTC(), `2022-12-15 08:11:34 +0000 UTC`)
   313  	})
   314  }
   315  
   316  func Test_Issue2901(t *testing.T) {
   317  	type GameApp2 struct {
   318  		ForceUpdateTime *time.Time
   319  	}
   320  	gtest.C(t, func(t *gtest.T) {
   321  		src := map[string]interface{}{
   322  			"FORCE_UPDATE_TIME": time.Now(),
   323  		}
   324  		m := GameApp2{}
   325  		err := gconv.Scan(src, &m)
   326  		t.AssertNil(err)
   327  	})
   328  }