/
condOK.go
53 lines (45 loc) · 1.23 KB
/
condOK.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
package dyntpl
import "github.com/koykov/inspector/testobj"
// CondOKFn describes helper func signature.
type CondOKFn func(ctx *Ctx, v *any, ok *bool, args []any)
var (
// Registry of condition-OK helpers.
condOKRegistry = map[string]CondOKFn{}
)
// RegisterCondOKFn registers new condition-OK helper.
func RegisterCondOKFn(name string, cond CondOKFn) {
condOKRegistry[name] = cond
}
// RegisterCondOKFnNS registers new condition-OK helper in given namespace.
func RegisterCondOKFnNS(namespace, name string, cond CondOKFn) {
if len(namespace) > 0 {
name = namespace + "::" + name
}
RegisterCondOKFn(name, cond)
}
// GetCondOKFn returns condition-OK helper from the registry.
func GetCondOKFn(name string) *CondOKFn {
if fn, ok := condOKRegistry[name]; ok {
return &fn
}
return nil
}
// Simple example of condition-OK helper func.
func testCondOK(ctx *Ctx, v *any, ok *bool, args []any) {
if len(args) == 0 {
*ok = false
return
}
if fin, ok1 := args[0].(**testobj.TestFinance); ok1 {
c := ctx.GetCounter("__testUserNextHistory999counter")
if c >= len((*fin).History) {
*ok = false
return
}
*v = &(*fin).History[c]
*ok = true
c++
ctx.SetCounter("__testUserNextHistory999counter", c)
}
}
var _ = RegisterCondOKFnNS