/
value.go
241 lines (222 loc) · 5.11 KB
/
value.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (C) 2016 Juniper Networks, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gohanscript
import (
"strconv"
"strings"
"github.com/flosch/pongo2"
)
//Value represents gohan script value interface
type Value interface {
Value(context *Context) interface{}
}
//Nil reprensents nil
type Nil struct {
}
//Value returns nil
func (i *Nil) Value(context *Context) interface{} {
return nil
}
//MiniGoValue reprensents minigo value
type MiniGoValue struct {
minigo *MiniGo
}
//Value returns nil
func (i *MiniGoValue) Value(context *Context) interface{} {
value, _ := i.minigo.Run(context)
return value
}
//NewMiniGoValue makes a MiniGoValue value
func NewMiniGoValue(param interface{}) (*MiniGoValue, error) {
code, ok := param.(string)
if !ok {
return nil, nil
}
if code == "" {
return nil, nil
}
if code[0] != '$' {
return nil, nil
}
minigoCode := code[1:]
if minigoCode[0] == '{' {
minigoCode = minigoCode[1:]
}
if minigoCode[len(minigoCode)-1] == '}' {
minigoCode = minigoCode[:len(minigoCode)-1]
}
minigo, err := CompileExpr(minigoCode)
if err != nil {
return nil, err
}
t := &MiniGoValue{
minigo: minigo,
}
return t, nil
}
//Constant represents donburi consntant.
type Constant struct {
value interface{}
}
//Value returns constant value
func (c *Constant) Value(context *Context) interface{} {
return c.value
}
//Template reprensents a value includes templates
type Template struct {
templates map[string]*pongo2.Template
param interface{}
minigos map[string]*MiniGo
}
//NewTemplate makes a template value
func NewTemplate(param interface{}) *Template {
t := &Template{
param: param,
templates: map[string]*pongo2.Template{},
minigos: map[string]*MiniGo{},
}
CacheTemplate(t.param, t.templates, t.minigos)
return t
}
//Value applies new template value
func (t *Template) Value(context *Context) interface{} {
if context == nil {
return t.param
}
return ApplyTemplate(t, t.param, context)
}
//NewValue makes gohan script value from an yaml data
func NewValue(a interface{}) (Value, error) {
if a == nil {
return nil, nil
}
miniGoValue, err := NewMiniGoValue(a)
if err != nil {
return nil, err
}
if miniGoValue != nil {
return miniGoValue, nil
}
if ContainsTemplate(a) {
return NewTemplate(a), nil
}
return &Constant{
value: a,
}, nil
}
//ApplyTemplate apply template code for input params.
func ApplyTemplate(template *Template, param interface{}, context *Context) (result interface{}) {
switch p := param.(type) {
case string:
if p == "" {
return nil
}
if p[0] == '$' {
minigo := template.minigos[p]
result, _ := minigo.Run(context)
return result
}
tpl := template.templates[p]
if tpl == nil {
return param
}
result, err := tpl.Execute(context.data)
if err != nil {
return param
}
i, err := strconv.Atoi(result)
if err == nil {
return i
}
return result
case map[string]interface{}:
result := map[string]interface{}{}
for key, value := range p {
result[ApplyTemplate(template, key, context).(string)] = ApplyTemplate(template, value, context)
}
return result
case []interface{}:
result := make([]interface{}, len(p))
for index, value := range p {
result[index] = ApplyTemplate(template, value, context)
}
return result
}
return param
}
//CacheTemplate caches template to the statement
func CacheTemplate(arg interface{}, templates map[string]*pongo2.Template, minigos map[string]*MiniGo) error {
switch a := arg.(type) {
case string:
if a == "" {
return nil
}
if a[0] == '$' {
miniGoValue, err := NewMiniGoValue(a)
if err != nil {
return err
}
minigos[a] = miniGoValue.minigo
} else {
tpl, err := pongo2.FromString(a)
if err != nil {
return err
}
templates[a] = tpl
}
case []interface{}:
for _, item := range a {
err := CacheTemplate(item, templates, minigos)
if err != nil {
return err
}
}
case map[string]interface{}:
for key, item := range a {
err := CacheTemplate(key, templates, minigos)
if err != nil {
return err
}
err = CacheTemplate(item, templates, minigos)
if err != nil {
return err
}
}
}
return nil
}
//ContainsTemplate checks an object contains template or not
func ContainsTemplate(arg interface{}) bool {
switch a := arg.(type) {
case string:
return strings.HasPrefix(a, "$") || strings.Contains(a, "{{")
case []interface{}:
for _, item := range a {
if ContainsTemplate(item) {
return true
}
}
case map[string]interface{}:
for key, item := range a {
if ContainsTemplate(key) {
return true
}
if ContainsTemplate(item) {
return true
}
}
}
return false
}