github.com/gogf/gf@v1.16.9/internal/structs/structs_z_unit_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 structs_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/gogf/gf/internal/structs"
    13  
    14  	"github.com/gogf/gf/frame/g"
    15  
    16  	"github.com/gogf/gf/test/gtest"
    17  )
    18  
    19  func Test_Basic(t *testing.T) {
    20  	gtest.C(t, func(t *gtest.T) {
    21  		type User struct {
    22  			Id   int
    23  			Name string `params:"name"`
    24  			Pass string `my-tag1:"pass1" my-tag2:"pass2" params:"pass"`
    25  		}
    26  		var user User
    27  		m, _ := structs.TagMapName(user, []string{"params"})
    28  		t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
    29  		m, _ = structs.TagMapName(&user, []string{"params"})
    30  		t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
    31  
    32  		m, _ = structs.TagMapName(&user, []string{"params", "my-tag1"})
    33  		t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
    34  		m, _ = structs.TagMapName(&user, []string{"my-tag1", "params"})
    35  		t.Assert(m, g.Map{"name": "Name", "pass1": "Pass"})
    36  		m, _ = structs.TagMapName(&user, []string{"my-tag2", "params"})
    37  		t.Assert(m, g.Map{"name": "Name", "pass2": "Pass"})
    38  	})
    39  
    40  	gtest.C(t, func(t *gtest.T) {
    41  		type Base struct {
    42  			Pass1 string `params:"password1"`
    43  			Pass2 string `params:"password2"`
    44  		}
    45  		type UserWithBase struct {
    46  			Id   int
    47  			Name string
    48  			Base `params:"base"`
    49  		}
    50  		user := new(UserWithBase)
    51  		m, _ := structs.TagMapName(user, []string{"params"})
    52  		t.Assert(m, g.Map{
    53  			"base":      "Base",
    54  			"password1": "Pass1",
    55  			"password2": "Pass2",
    56  		})
    57  	})
    58  
    59  	gtest.C(t, func(t *gtest.T) {
    60  		type Base struct {
    61  			Pass1 string `params:"password1"`
    62  			Pass2 string `params:"password2"`
    63  		}
    64  		type UserWithEmbeddedAttribute struct {
    65  			Id   int
    66  			Name string
    67  			Base
    68  		}
    69  		type UserWithoutEmbeddedAttribute struct {
    70  			Id   int
    71  			Name string
    72  			Pass Base
    73  		}
    74  		user1 := new(UserWithEmbeddedAttribute)
    75  		user2 := new(UserWithoutEmbeddedAttribute)
    76  		m, _ := structs.TagMapName(user1, []string{"params"})
    77  		t.Assert(m, g.Map{"password1": "Pass1", "password2": "Pass2"})
    78  		m, _ = structs.TagMapName(user2, []string{"params"})
    79  		t.Assert(m, g.Map{})
    80  	})
    81  }
    82  
    83  func Test_StructOfNilPointer(t *testing.T) {
    84  	gtest.C(t, func(t *gtest.T) {
    85  		type User struct {
    86  			Id   int
    87  			Name string `params:"name"`
    88  			Pass string `my-tag1:"pass1" my-tag2:"pass2" params:"pass"`
    89  		}
    90  		var user *User
    91  		m, _ := structs.TagMapName(user, []string{"params"})
    92  		t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
    93  		m, _ = structs.TagMapName(&user, []string{"params"})
    94  		t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
    95  
    96  		m, _ = structs.TagMapName(&user, []string{"params", "my-tag1"})
    97  		t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
    98  		m, _ = structs.TagMapName(&user, []string{"my-tag1", "params"})
    99  		t.Assert(m, g.Map{"name": "Name", "pass1": "Pass"})
   100  		m, _ = structs.TagMapName(&user, []string{"my-tag2", "params"})
   101  		t.Assert(m, g.Map{"name": "Name", "pass2": "Pass"})
   102  	})
   103  }
   104  
   105  func Test_FieldMap(t *testing.T) {
   106  	gtest.C(t, func(t *gtest.T) {
   107  		type User struct {
   108  			Id   int
   109  			Name string `params:"name"`
   110  			Pass string `my-tag1:"pass1" my-tag2:"pass2" params:"pass"`
   111  		}
   112  		var user *User
   113  		m, _ := structs.FieldMap(structs.FieldMapInput{
   114  			Pointer:          user,
   115  			PriorityTagArray: []string{"params"},
   116  			RecursiveOption:  structs.RecursiveOptionEmbedded,
   117  		})
   118  		t.Assert(len(m), 3)
   119  		_, ok := m["Id"]
   120  		t.Assert(ok, true)
   121  		_, ok = m["Name"]
   122  		t.Assert(ok, false)
   123  		_, ok = m["name"]
   124  		t.Assert(ok, true)
   125  		_, ok = m["Pass"]
   126  		t.Assert(ok, false)
   127  		_, ok = m["pass"]
   128  		t.Assert(ok, true)
   129  	})
   130  	gtest.C(t, func(t *gtest.T) {
   131  		type User struct {
   132  			Id   int
   133  			Name string `params:"name"`
   134  			Pass string `my-tag1:"pass1" my-tag2:"pass2" params:"pass"`
   135  		}
   136  		var user *User
   137  		m, _ := structs.FieldMap(structs.FieldMapInput{
   138  			Pointer:          user,
   139  			PriorityTagArray: nil,
   140  			RecursiveOption:  structs.RecursiveOptionEmbedded,
   141  		})
   142  		t.Assert(len(m), 3)
   143  		_, ok := m["Id"]
   144  		t.Assert(ok, true)
   145  		_, ok = m["Name"]
   146  		t.Assert(ok, true)
   147  		_, ok = m["name"]
   148  		t.Assert(ok, false)
   149  		_, ok = m["Pass"]
   150  		t.Assert(ok, true)
   151  		_, ok = m["pass"]
   152  		t.Assert(ok, false)
   153  	})
   154  }
   155  
   156  func Test_StructType(t *testing.T) {
   157  	gtest.C(t, func(t *gtest.T) {
   158  		type B struct {
   159  			Name string
   160  		}
   161  		type A struct {
   162  			B
   163  		}
   164  		r, err := structs.StructType(new(A))
   165  		t.AssertNil(err)
   166  		t.Assert(r.Signature(), `github.com/gogf/gf/internal/structs_test/structs_test.A`)
   167  	})
   168  	gtest.C(t, func(t *gtest.T) {
   169  		type B struct {
   170  			Name string
   171  		}
   172  		type A struct {
   173  			B
   174  		}
   175  		r, err := structs.StructType(new(A).B)
   176  		t.AssertNil(err)
   177  		t.Assert(r.Signature(), `github.com/gogf/gf/internal/structs_test/structs_test.B`)
   178  	})
   179  	gtest.C(t, func(t *gtest.T) {
   180  		type B struct {
   181  			Name string
   182  		}
   183  		type A struct {
   184  			*B
   185  		}
   186  		r, err := structs.StructType(new(A).B)
   187  		t.AssertNil(err)
   188  		t.Assert(r.String(), `structs_test.B`)
   189  	})
   190  	// Error.
   191  	gtest.C(t, func(t *gtest.T) {
   192  		type B struct {
   193  			Name string
   194  		}
   195  		type A struct {
   196  			*B
   197  			Id int
   198  		}
   199  		_, err := structs.StructType(new(A).Id)
   200  		t.AssertNE(err, nil)
   201  	})
   202  }
   203  
   204  func Test_StructTypeBySlice(t *testing.T) {
   205  	gtest.C(t, func(t *gtest.T) {
   206  		type B struct {
   207  			Name string
   208  		}
   209  		type A struct {
   210  			Array []*B
   211  		}
   212  		r, err := structs.StructType(new(A).Array)
   213  		t.AssertNil(err)
   214  		t.Assert(r.Signature(), `github.com/gogf/gf/internal/structs_test/structs_test.B`)
   215  	})
   216  	gtest.C(t, func(t *gtest.T) {
   217  		type B struct {
   218  			Name string
   219  		}
   220  		type A struct {
   221  			Array []B
   222  		}
   223  		r, err := structs.StructType(new(A).Array)
   224  		t.AssertNil(err)
   225  		t.Assert(r.Signature(), `github.com/gogf/gf/internal/structs_test/structs_test.B`)
   226  	})
   227  	gtest.C(t, func(t *gtest.T) {
   228  		type B struct {
   229  			Name string
   230  		}
   231  		type A struct {
   232  			Array *[]B
   233  		}
   234  		r, err := structs.StructType(new(A).Array)
   235  		t.AssertNil(err)
   236  		t.Assert(r.Signature(), `github.com/gogf/gf/internal/structs_test/structs_test.B`)
   237  	})
   238  }
   239  
   240  func TestType_FieldKeys(t *testing.T) {
   241  	gtest.C(t, func(t *gtest.T) {
   242  		type B struct {
   243  			Id   int
   244  			Name string
   245  		}
   246  		type A struct {
   247  			Array []*B
   248  		}
   249  		r, err := structs.StructType(new(A).Array)
   250  		t.AssertNil(err)
   251  		t.Assert(r.FieldKeys(), g.Slice{"Id", "Name"})
   252  	})
   253  }