-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_att.go
47 lines (41 loc) · 1017 Bytes
/
check_att.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
package helpers
import (
"reflect"
"strings"
)
// CheckInTag represent the helpers for check attribute inside of struct
func CheckInTag(v interface{}, filterField, tag string) []string {
filterField = strings.Replace(filterField, " ", "", -1)
field := strings.Split(filterField, ",")
out := []string{}
mapField := make(map[string]bool)
for _, f := range field {
mapField[f] = true
}
val := reflect.ValueOf(v)
for i := 0; i < val.Type().NumField(); i++ {
if tag, ok := val.Type().Field(i).Tag.Lookup(tag); ok {
if _, ok := mapField[tag]; ok {
if tag != "-" {
out = append(out, tag)
}
}
}
}
return out
}
// CheckMatchTag represent the helpers for check attribute match of struct
func CheckMatchTag(v interface{}, field, tag string) string {
var out string
val := reflect.ValueOf(v)
for i := 0; i < val.Type().NumField(); i++ {
if tag, ok := val.Type().Field(i).Tag.Lookup(tag); ok {
if tag == field {
if tag != "-" {
out = tag
}
}
}
}
return out
}