-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.go
More file actions
61 lines (47 loc) · 962 Bytes
/
env.go
File metadata and controls
61 lines (47 loc) · 962 Bytes
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
package interactive
import (
"fmt"
"github.com/elos/data"
"github.com/elos/models"
"github.com/robertkrimen/otto"
)
const (
ottoObjectString = "[object Object]"
jsonParseFormat = "JSON.parse(%s)"
)
type Credentials struct {
ID string
Key string
}
type Env struct {
db data.DB
otto *otto.Otto
user *models.User
}
func NewEnv(db data.DB, u *models.User) *Env {
e := new(Env)
e.db = db
e.otto = otto.New()
e.otto.Set("me", u)
e.otto.Set("db", db)
e.user = u
return e
}
func (e *Env) Interpret(entry string) string {
if len(entry) == 0 {
return entry
}
value, err := e.otto.Run(entry)
if err != nil {
return err.Error()
}
/* results in SyntaxError: invalid character 'o' looking for beginning of value
if value.IsObject() {
return e.Interpret(fmt.Sprintf(jsonParseFormat, entry))
}
*/
return fmt.Sprintf("%v", value)
}
func (e *Env) Set(variableName string, value interface{}) {
e.otto.Set(variableName, value)
}