-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.go
70 lines (65 loc) · 1.32 KB
/
mod.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
package vector_inspector
import (
"github.com/koykov/dyntpl"
"github.com/koykov/vector"
)
func modCoalesce(ctx *dyntpl.Ctx, buf *any, val any, args []any) error {
if len(args) < 1 {
return dyntpl.ErrModPoorArgs
}
var root *vector.Node
switch x := val.(type) {
case vector.Interface:
root = x.Root()
case *vector.Node:
root = x
default:
return vector.ErrIncompatType
}
if root == nil || root.Type() == vector.TypeNull {
return nil
}
var node *vector.Node
for i := 0; i < len(args); i++ {
path := ctx.BufAcc.StakeOut().WriteX(args[i]).StakedString()
if ctx.BufAcc.Error() != nil {
continue
}
if node = root.Dot(path); node.Type() != vector.TypeNull {
break
}
}
if node == nil || node.Type() == vector.TypeNull {
return nil
}
*buf = node
return nil
}
func modMarshal(ctx *dyntpl.Ctx, buf *any, val any, args []any) error {
var root *vector.Node
var src any
if val != nil {
src = val
} else if len(args) > 0 {
src = args[0]
}
if src == nil {
return nil
}
switch x := src.(type) {
case vector.Interface:
root = x.Root()
case *vector.Node:
root = x
default:
return vector.ErrIncompatType
}
if root == nil || root.Type() == vector.TypeNull {
return nil
}
ctx.BufAcc.StakeOut()
w := ctx.BufAcc.ToWriter()
_ = root.Marshal(w)
*buf = ctx.BufAcc.StakedBytes()
return nil
}