/
context.go
53 lines (43 loc) · 1.08 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
package client
import (
"strings"
)
// Context holds variables in present context
type Context struct {
variables []Variable
}
// Variable is struct definition for variable
type Variable struct {
name string
value string
}
// DefineVar defines variable and puts it in present context
func (c *Context) DefineVar(variable string, value string) {
var v = Variable{name: variable, value: value}
c.variables = append(c.variables, v)
}
// GetVars returns all defined variables in context
func (c *Context) GetVars() []Variable {
return c.variables
}
// GetVar finds var by name and returns its value
func (c *Context) GetVar(name string) Variable {
for _, value := range c.variables {
if value.name == name {
return value
}
}
return Variable{
name: name,
value: name,
}
}
// Transpile text change variables from context
func (c *Context) Transpile(toCompile string) string {
for _, variable := range c.variables {
toCompile = strings.Replace(toCompile, "$"+variable.name, variable.value, -1)
}
return toCompile
}
// CurrentContext of client
var CurrentContext = new(Context)