@@ -9,51 +9,75 @@ import (
9
9
"github.com/jmespath/go-jmespath"
10
10
)
11
11
12
- // DefaultFuncs returns a list of default functions for binding in the template
13
- func (t * Template ) DefaultFuncs () map [string ]interface {} {
14
- return map [string ]interface {}{
15
- "unixtime" : func () interface {} {
16
- return time .Now ().Unix ()
17
- },
12
+ // QueryObject applies a JMESPath query specified by the expression, against the target object.
13
+ func QueryObject (exp string , target interface {}) (interface {}, error ) {
14
+ query , err := jmespath .Compile (exp )
15
+ if err != nil {
16
+ return nil , err
17
+ }
18
+ return query .Search (target )
19
+ }
18
20
19
- "var" : func (name , doc string , v ... interface {}) interface {} {
20
- if found , has := t .binds [name ]; has {
21
- return found
22
- }
23
- return v // default
24
- },
21
+ // SplitLines splits the input into a string slice.
22
+ func SplitLines (o interface {}) ([]string , error ) {
23
+ ret := []string {}
24
+ switch o := o .(type ) {
25
+ case string :
26
+ return strings .Split (o , "\n " ), nil
27
+ case []byte :
28
+ return strings .Split (string (o ), "\n " ), nil
29
+ }
30
+ return ret , fmt .Errorf ("not-supported-value-type" )
31
+ }
25
32
26
- "global" : func (name string , v interface {}) interface {} {
27
- t .binds [name ] = v
28
- return ""
29
- },
33
+ // FromJSON decode the input JSON encoded as string or byte slice into a map.
34
+ func FromJSON (o interface {}) (interface {}, error ) {
35
+ ret := map [string ]interface {}{}
36
+ switch o := o .(type ) {
37
+ case string :
38
+ err := json .Unmarshal ([]byte (o ), & ret )
39
+ return ret , err
40
+ case []byte :
41
+ err := json .Unmarshal (o , & ret )
42
+ return ret , err
43
+ }
44
+ return ret , fmt .Errorf ("not-supported-value-type" )
45
+ }
30
46
31
- "q" : func (q string , o interface {}) (interface {}, error ) {
32
- query , err := jmespath .Compile (q )
33
- if err != nil {
34
- return nil , err
35
- }
36
- return query .Search (o )
37
- },
47
+ // ToJSON encodes the input struct into a JSON string.
48
+ func ToJSON (o interface {}) (string , error ) {
49
+ buff , err := json .MarshalIndent (o , "" , " " )
50
+ return string (buff ), err
51
+ }
38
52
39
- "jsonEncode" : func (o interface {}) (string , error ) {
40
- buff , err := json .MarshalIndent (o , "" , " " )
41
- return string (buff ), err
42
- },
53
+ // FromMap decodes map into raw struct
54
+ func FromMap (m map [string ]interface {}, raw interface {}) error {
55
+ // The safest way, but the slowest, is to just marshal and unmarshal back
56
+ buff , err := ToJSON (m )
57
+ if err != nil {
58
+ return err
59
+ }
60
+ return json .Unmarshal ([]byte (buff ), raw )
61
+ }
43
62
44
- "jsonDecode" : func (o interface {}) (interface {}, error ) {
45
- ret := map [string ]interface {}{}
46
- switch o := o .(type ) {
47
- case string :
48
- err := json .Unmarshal ([]byte (o ), & ret )
49
- return ret , err
50
- case []byte :
51
- err := json .Unmarshal (o , & ret )
52
- return ret , err
53
- }
54
- return ret , fmt .Errorf ("not-supported-value-type" )
55
- },
63
+ // ToMap encodes the input as a map
64
+ func ToMap (raw interface {}) (map [string ]interface {}, error ) {
65
+ buff , err := ToJSON (raw )
66
+ if err != nil {
67
+ return nil , err
68
+ }
69
+ out , err := FromJSON (buff )
70
+ return out .(map [string ]interface {}), err
71
+ }
72
+
73
+ // UnixTime returns a timestamp in unix time
74
+ func UnixTime () interface {} {
75
+ return time .Now ().Unix ()
76
+ }
56
77
78
+ // DefaultFuncs returns a list of default functions for binding in the template
79
+ func (t * Template ) DefaultFuncs () map [string ]interface {} {
80
+ return map [string ]interface {}{
57
81
"include" : func (p string , opt ... interface {}) (string , error ) {
58
82
var o interface {}
59
83
if len (opt ) > 0 {
@@ -78,15 +102,22 @@ func (t *Template) DefaultFuncs() map[string]interface{} {
78
102
return included .Render (o )
79
103
},
80
104
81
- "lines" : func (o interface {}) ([]string , error ) {
82
- ret := []string {}
83
- switch o := o .(type ) {
84
- case string :
85
- return strings .Split (o , "\n " ), nil
86
- case []byte :
87
- return strings .Split (string (o ), "\n " ), nil
105
+ "var" : func (name , doc string , v ... interface {}) interface {} {
106
+ if found , has := t .binds [name ]; has {
107
+ return found
88
108
}
89
- return ret , fmt . Errorf ( "not-supported-value-type" )
109
+ return v // default
90
110
},
111
+
112
+ "global" : func (name string , v interface {}) interface {} {
113
+ t .binds [name ] = v
114
+ return ""
115
+ },
116
+
117
+ "q" : QueryObject ,
118
+ "unixtime" : UnixTime ,
119
+ "lines" : SplitLines ,
120
+ "to_json" : ToJSON ,
121
+ "from_json" : FromJSON ,
91
122
}
92
123
}
0 commit comments