-
Notifications
You must be signed in to change notification settings - Fork 1
/
hide.go
67 lines (58 loc) · 1.34 KB
/
hide.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
package goblet
import (
"reflect"
"regexp"
"strings"
)
var hideMatcher = regexp.MustCompile("^hide(\\((([^,]+|,)*)\\))*$")
func autoHide(data interface{}, ctx *Context) interface{} {
if data == nil {
return nil
}
origin := reflect.ValueOf(data)
autoHideElem(origin, ctx.ReqURL().Path)
return origin.Interface()
}
func autoHideElem(origin reflect.Value, path string) {
var val reflect.Value
if origin.Kind() == reflect.Ptr {
val = origin.Elem()
} else {
val = origin
}
if val.Kind() == reflect.Struct {
valtype := val.Type()
for i := 0; i < valtype.NumField(); i++ {
t := valtype.Field(i)
v := val.Field(i)
tags := strings.Split(string(t.Tag.Get("goblet")), ",")
typeName := strings.ToLower(t.Name)
if (typeName == "pwd" || typeName == "password") && v.CanSet() {
v.SetString("hidden")
continue
}
for _, tag := range tags {
found := hideMatcher.FindStringSubmatch(tag)
if len(found) > 0 {
if found[2] != "" {
for _, f := range strings.Split(found[2], ",") {
if f == path {
if v.CanSet() {
v.SetString("hidden")
}
}
}
} else {
if v.CanSet() {
v.SetString("hidden")
}
}
}
}
}
} else if val.Kind() == reflect.Slice {
for index := 0; index < val.Len(); index++ {
autoHideElem(val.Index(index), path)
}
}
}