-
Notifications
You must be signed in to change notification settings - Fork 12
/
context.go
91 lines (81 loc) · 2.09 KB
/
context.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
package jio
import (
"reflect"
"strings"
)
// NewContext Generates a context object with the provided data.
func NewContext(data interface{}) *Context {
return &Context{
root: data,
Value: data,
fields: make([]string, 0, 3),
}
}
// Context contains data and toolkit
type Context struct {
Value interface{}
Err error
root interface{}
fields []string
storage map[string]interface{}
skip bool
kindCache map[*interface{}]reflect.Kind
}
// Ref return the reference value.
// The reference path support use `.` access object property, just like javascript.
func (ctx *Context) Ref(refPath string) (value interface{}, ok bool) {
fields := strings.Split(refPath, ".")
value = ctx.root
var valueMap map[string]interface{}
for _, field := range fields {
valueMap, ok = value.(map[string]interface{})
if !ok {
return
}
value, ok = valueMap[field]
if !ok {
return
}
}
return
}
// FieldPath the field path of the current value.
func (ctx *Context) FieldPath() string {
return strings.Join(ctx.fields, ".")
}
// Abort throw an error and skip the following check rules.
func (ctx *Context) Abort(err error) {
ctx.Err = err
ctx.skip = true
}
// Skip the following check rules.
func (ctx *Context) Skip() {
ctx.skip = true
}
// Set is used to store a new key/value pair exclusively for this context.
func (ctx *Context) Set(name string, value interface{}) {
if ctx.storage == nil {
ctx.storage = make(map[string]interface{})
}
ctx.storage[name] = value
}
// Get returns the value for the given key, ie: (value, true).
func (ctx *Context) Get(name string) (interface{}, bool) {
if ctx.storage == nil {
ctx.storage = make(map[string]interface{})
}
value, ok := ctx.storage[name]
return value, ok
}
// AssertKind assert the value type and cache.
func (ctx *Context) AssertKind(kind reflect.Kind) bool {
if ctx.kindCache == nil {
ctx.kindCache = make(map[*interface{}]reflect.Kind)
}
cachedKind, ok := ctx.kindCache[&ctx.Value]
if !ok {
cachedKind = reflect.TypeOf(ctx.Value).Kind()
ctx.kindCache[&ctx.Value] = cachedKind
}
return cachedKind == kind
}