forked from mguzelevich/goatt
/
context.go
71 lines (58 loc) · 1.24 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
package main
import (
"bytes"
"fmt"
"text/template"
"github.com/pborman/uuid"
)
type Session map[string]interface{}
func (session Session) UUID() string {
_, ok := session["uuid"]
if !ok {
session["uuid"] = uuid.New()
}
return session["uuid"].(string)
}
type Step map[string]string
func (step Step) UUID() string {
_, ok := step["uuid"]
if !ok {
step["uuid"] = uuid.New()
}
fmt.Println("\nstep uuid", ok, step)
return step["uuid"]
}
type Context struct {
Constants interface{}
Step Step
Session Session
Result interface{}
}
func (ctx Context) UUID() string {
return uuid.New()
}
func (ctx Context) ClearStep() {
ctx.Step = Step{}
}
func (ctx Context) Evaluate(tmpl string) (string, error) {
template, err := template.New(uuid.New()).Parse(tmpl)
if err != nil {
panic(fmt.Sprintf("unable parse subject %q as a template", tmpl))
}
buffer := bytes.NewBuffer([]byte{})
if err := template.Execute(buffer, ctx); err != nil {
panic(fmt.Sprintf("failed to render template [%s]\n[%s]\n", err, ctx))
return "", err
}
return buffer.String(), nil
}
func InitContext(consts map[string]interface{}) *Context {
ctx := Context{
Constants: consts,
Step: Step{},
Session: Session{
"stepIdx": 0,
},
}
return &ctx
}