github.com/zhongdalu/gf@v1.0.0/g/internal/structs/structs_test.go (about) 1 // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 package structs_test 8 9 import ( 10 "testing" 11 12 "github.com/zhongdalu/gf/g/internal/structs" 13 14 "github.com/zhongdalu/gf/g" 15 16 "github.com/zhongdalu/gf/g/test/gtest" 17 ) 18 19 func Test_Basic(t *testing.T) { 20 gtest.Case(t, func() { 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 gtest.Assert(structs.TagMapName(user, []string{"params"}, true), g.Map{"name": "Name", "pass": "Pass"}) 28 gtest.Assert(structs.TagMapName(&user, []string{"params"}, true), g.Map{"name": "Name", "pass": "Pass"}) 29 30 gtest.Assert(structs.TagMapName(&user, []string{"params", "my-tag1"}, true), g.Map{"name": "Name", "pass": "Pass"}) 31 gtest.Assert(structs.TagMapName(&user, []string{"my-tag1", "params"}, true), g.Map{"name": "Name", "pass1": "Pass"}) 32 gtest.Assert(structs.TagMapName(&user, []string{"my-tag2", "params"}, true), g.Map{"name": "Name", "pass2": "Pass"}) 33 }) 34 35 gtest.Case(t, func() { 36 type Base struct { 37 Pass1 string `params:"password1"` 38 Pass2 string `params:"password2"` 39 } 40 type UserWithBase struct { 41 Id int 42 Name string 43 Base `params:"base"` 44 } 45 user := new(UserWithBase) 46 gtest.Assert(structs.TagMapName(user, []string{"params"}, true), g.Map{ 47 "base": "Base", 48 "password1": "Pass1", 49 "password2": "Pass2", 50 }) 51 }) 52 53 gtest.Case(t, func() { 54 type Base struct { 55 Pass1 string `params:"password1"` 56 Pass2 string `params:"password2"` 57 } 58 type UserWithBase1 struct { 59 Id int 60 Name string 61 Base 62 } 63 type UserWithBase2 struct { 64 Id int 65 Name string 66 Pass Base 67 } 68 user1 := new(UserWithBase1) 69 user2 := new(UserWithBase2) 70 gtest.Assert(structs.TagMapName(user1, []string{"params"}, true), g.Map{"password1": "Pass1", "password2": "Pass2"}) 71 gtest.Assert(structs.TagMapName(user2, []string{"params"}, true), g.Map{"password1": "Pass1", "password2": "Pass2"}) 72 }) 73 }