github.com/viant/toolbox@v0.34.5/struct_helper_test.go (about) 1 package toolbox_test 2 3 import ( 4 "reflect" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/viant/toolbox" 10 ) 11 12 func TestProcessStruct(t *testing.T) { 13 14 type Super struct { 15 Parent int 16 } 17 type User struct { 18 *Super 19 Name string `column:"name"` 20 DateOfBirth time.Time `column:"date" dateFormat:"2006-01-02 15:04:05.000000"` 21 Id int `autogenrated:"true"` 22 prv int 23 Other string `transient:"true"` 24 } 25 26 user := User{Id: 1, Other: "!@#", Name: "foo", Super: &Super{12}} 27 var userMap = make(map[string]interface{}) 28 err := toolbox.ProcessStruct(&user, func(fieldType reflect.StructField, field reflect.Value) error { 29 value := field.Interface() 30 userMap[fieldType.Name] = value 31 return nil 32 }) 33 assert.Nil(t, err) 34 assert.Equal(t, 5, len(userMap)) 35 36 assert.Equal(t, 12, userMap["Parent"]) 37 assert.Equal(t, 1, userMap["Id"]) 38 assert.Equal(t, "!@#", userMap["Other"]) 39 } 40 41 func TestBuildTagMapping(t *testing.T) { 42 43 type User struct { 44 Name string `column:"name"` 45 DateOfBirth time.Time `column:"date" dateFormat:"2006-01-02 15:04:05.000000"` 46 Id int `autogenrated:"true"` 47 Other string `transient:"true"` 48 } 49 50 { 51 tags := []string{"column", "autogenrated"} 52 result := toolbox.BuildTagMapping((*User)(nil), "column", "transient", true, true, tags) 53 54 { 55 actual := len(result) 56 expected := 3 57 assert.Equal(t, actual, expected, "Extract mapping count") 58 } 59 { 60 actual, _ := result["name"]["fieldName"] 61 expected := "Name" 62 assert.Equal(t, actual, expected, "Extract name mapping") 63 } 64 65 { 66 actual, _ := result["id"]["autogenrated"] 67 expected := "true" 68 assert.Equal(t, actual, expected, "Extract id flaged as autogenerated") 69 } 70 } 71 { 72 tags := []string{"column", "autogenrated"} 73 result := toolbox.BuildTagMapping((*User)(nil), "fieldName", "transient", true, false, tags) 74 actual, _ := result["Name"]["fieldName"] 75 expected := "Name" 76 assert.Equal(t, actual, expected, "Extract name mapping") 77 } 78 79 { 80 tags := []string{"column", "autogenrated"} 81 result := toolbox.BuildTagMapping((*User)(nil), "column", "transient", false, false, tags) 82 { 83 actual := len(result) 84 expected := 2 85 assert.Equal(t, actual, expected, "Extract mapping count") 86 } 87 } 88 89 type User2 struct { 90 Name string `json:"name" column:"name"` 91 DateOfBirth time.Time `json:"date" column:"date" dateFormat:"2006-01-02 15:04:05.000000"` 92 Id int `json:"id" autogenrated:"true"` 93 Other string `json:"other" transient:"true"` 94 } 95 96 { 97 tags := []string{"column", "autogenrated"} 98 result := toolbox.BuildTagMapping((*User)(nil), "column", "transient", true, true, tags) 99 100 { 101 actual := len(result) 102 expected := 3 103 assert.Equal(t, actual, expected, "Extract mapping count") 104 } 105 { 106 actual, _ := result["name"]["fieldName"] 107 expected := "Name" 108 assert.Equal(t, actual, expected, "Extract name mapping") 109 } 110 111 { 112 actual, _ := result["id"]["autogenrated"] 113 expected := "true" 114 assert.Equal(t, actual, expected, "Extract id flaged as autogenerated") 115 } 116 } 117 118 } 119 120 func TestBuildEmbededStructTagMapping(t *testing.T) { 121 122 type Super struct { 123 Id int `autogenrated:"true"` 124 Name string `column:"name"` 125 } 126 127 type User struct { 128 *Super 129 DateOfBirth time.Time `column:"date" dateFormat:"2006-01-02 15:04:05.000000"` 130 Other string `transient:"true"` 131 } 132 133 tags := []string{"column", "autogenrated"} 134 result := toolbox.BuildTagMapping((*User)(nil), "column", "transient", true, true, tags) 135 136 { 137 actual := len(result) 138 expected := 3 139 assert.Equal(t, actual, expected, "Extract mapping count") 140 } 141 { 142 actual, _ := result["name"]["fieldName"] 143 expected := "Name" 144 assert.Equal(t, actual, expected, "Extract name mapping") 145 } 146 147 { 148 actual, _ := result["id"]["autogenrated"] 149 expected := "true" 150 assert.Equal(t, actual, expected, "Extract id flaged as autogenerated") 151 } 152 153 } 154 155 type Type4 struct { 156 Id int 157 } 158 159 type Type3 struct { 160 Name map[string]string 161 Type4 map[string]*Type4 162 } 163 164 type Type2 struct { 165 F1 int 166 F3 *Type3 167 } 168 169 type Type1 struct { 170 F1 int 171 F2 *Type2 172 F3 []interface{} 173 F4 map[string]interface{} 174 F5 []*Type3 175 } 176 177 type SuperType1 struct { 178 *Type1 179 } 180 181 type SuperType2 struct { 182 Type1 183 } 184 185 func Test_InitStruct(t *testing.T) { 186 187 { 188 var t1 = &Type1{} 189 toolbox.InitStruct(t1) 190 assert.NotNil(t, t1.F2) 191 assert.NotNil(t, t1.F3) 192 assert.NotNil(t, t1.F4) 193 assert.NotNil(t, t1.F5) 194 195 } 196 { 197 var t1 = &SuperType1{} 198 toolbox.InitStruct(t1) 199 assert.NotNil(t, t1.F2) 200 assert.NotNil(t, t1.F3) 201 assert.NotNil(t, t1.F4) 202 assert.NotNil(t, t1.F5) 203 } 204 { 205 var t1 = &SuperType2{} 206 toolbox.InitStruct(t1) 207 assert.NotNil(t, t1.F2) 208 assert.NotNil(t, t1.F3) 209 assert.NotNil(t, t1.F4) 210 assert.NotNil(t, t1.F5) 211 } 212 213 } 214 215 func Test_GetStructMeta(t *testing.T) { 216 217 var t1 = &Type1{} 218 toolbox.InitStruct(t1) 219 meta := toolbox.GetStructMeta(t1) 220 assert.NotNil(t, meta) 221 222 }