-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
105 lines (100 loc) · 2.28 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package go_property_mapper
import "reflect"
type User struct {
UserId *string `col:"user-id"`
FirstName *string `col:"first-name"`
LastName *string `col:"last-name"`
Address *string `col:"address"`
Phone *int64 `col:"phone"`
Country *string `col:"country"`
Pin *int `col:"pin"`
Age *int `col:"age"`
Nominee *string `col:"nominee"`
Qualification *string `col:"qualification"`
BirthDate *string `col:"birth-date"`
Salary *float64 `col:"salary"`
Father *string `col:"father"`
Mother *string `col:"mother"`
SpouseName *string `col:"spouse-name"`
NumChildren *int `col:"num-children"`
VehiclesOwned *int `col:"vehicles-owned"`
Company *string `col:"company"`
Designation *string `col:"designation"`
Department *string `col:"department"`
IsVeteran *bool `col:"is-veteran"`
}
func GetColsAndVal(u interface{}) map[string]interface{} {
userProps := map[string]interface{}{}
r := reflect.TypeOf(u)
v := reflect.ValueOf(u)
for i := 0; i < r.NumField(); i++ {
n := r.Field(i).Name
if s, ex := r.FieldByName(n); ex && !v.FieldByName(n).IsNil() {
userProps[s.Tag.Get("col")] = v.FieldByName(n).Elem().Interface()
}
}
return userProps
}
func GetTagsReflect() map[string]bool {
e := User{}
userProps := map[string]bool{}
r := reflect.TypeOf(e)
for i := 0; i < r.NumField(); i++ {
f := r.Field(i)
n := f.Name
//print(n + " : ")
if s, ex := r.FieldByName(n); ex {
userProps[s.Tag.Get("col")] = true
//println(s.Tag.Get("col"))
}
}
return userProps
}
func GetTagName(i int) string {
switch i {
case 1:
return "user-id"
case 2:
return "first-name"
case 3:
return "last-name"
case 4:
return "address"
case 5:
return "phone"
case 6:
return "country"
case 7:
return "pin"
case 8:
return "age"
case 9:
return "nominee"
case 10:
return "qualification"
case 11:
return "birth-date"
case 12:
return "salary"
case 13:
return "father"
case 14:
return "mother"
case 15:
return "spouse-name"
case 16:
return "num-children"
case 17:
return "vehicles-owned"
case 18:
return "company"
case 19:
return "designation"
case 20:
return "department"
case 21:
return "is-veteran"
default:
return ""
}
}