forked from box-builder/box
/
util.go
178 lines (144 loc) · 3.78 KB
/
util.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
package builder
import (
"errors"
"fmt"
mruby "github.com/mitchellh/go-mruby"
)
func createException(m *mruby.Mrb, msg string) mruby.Value {
val, err := m.Class("Exception", nil).New(mruby.String(msg))
if err != nil {
panic(fmt.Sprintf("could not construct exception for return: %v", err))
}
return val
}
func extractStringArgs(args []*mruby.MrbValue) []string {
strArgs := []string{}
for _, arg := range args {
if arg != nil && arg.Type() != mruby.TypeProc {
strArgs = append(strArgs, arg.String())
}
}
return strArgs
}
func iterateRubyHash(arg *mruby.MrbValue, fn func(*mruby.MrbValue, *mruby.MrbValue) error) error {
hash := arg.Hash()
// mruby does not expose native maps, just ruby primitives, so we have to
// iterate through it with indexing functions instead of typical idioms.
keys, err := hash.Keys()
if err != nil {
return err
}
for i := 0; i < keys.Array().Len(); i++ {
key, err := keys.Array().Get(i)
if err != nil {
return err
}
value, err := hash.Get(key)
if err != nil {
return err
}
if err := fn(key, value); err != nil {
return err
}
}
return nil
}
func checkArgs(args []*mruby.MrbValue, l int) error {
if len(args) != l {
return fmt.Errorf("Expected %d arg, got %d", l, len(args))
}
return nil
}
func checkImage(b *Builder) error {
if b.ImageID() != "" {
return nil
}
return errors.New("from has not been called, no image can be used for this operation")
}
func standardCheck(b *Builder, args []*mruby.MrbValue, l int) error {
if err := checkArgs(args, l); err != nil {
return err
}
return checkImage(b)
}
// coerceArray converts a mruby array into interface{}, which encapsulates a
// []interface{}. This makes it a little easier to embed in the
// map[string]interface{} yielded by coerceHash. Note that any inner hashes and
// arrays will also be converted.
//
// If the value is not hash or array, it is assumed to be a type convertable to
// string.
func coerceArray(array *mruby.Array) (interface{}, error) {
retval := []interface{}{}
for i := 0; i < array.Len(); i++ {
val, err := array.Get(i)
if err != nil {
return nil, err
}
switch val.Type() {
case mruby.TypeArray:
ary, err := coerceArray(val.Array())
if err != nil {
return nil, err
}
retval = append(retval, ary)
case mruby.TypeHash:
ret, err := coerceHash(val.Hash())
if err != nil {
return nil, err
}
retval = append(retval, ret)
default:
retval = append(retval, val.String())
}
}
return retval, nil
}
// coerceHash converts a mruby hash into map[string]interface{}. The caller is
// expected to be responsible for coercing sub-types of the map out. Note that
// this will attempt to unwind arrays and hashes that are stored within it.
//
// If the value is not hash or array, it is assumed to be a type convertable to
// string.
func coerceHash(hash *mruby.Hash) (map[string]interface{}, error) {
retval := map[string]interface{}{}
keys, err := hash.Keys()
if err != nil {
return nil, err
}
keyary := keys.Array()
for i := 0; i < keyary.Len(); i++ {
arg, err := keyary.Get(i)
if err != nil {
return nil, err
}
typ := arg.Type()
if typ == mruby.TypeArray || typ == mruby.TypeHash {
return nil, errors.New("invalid type in key")
}
val, err := hash.Get(arg)
if err != nil {
return nil, err
}
typ = val.Type()
// XXX due to the recursion here we may have an endless stack if the data
// is apprpropriately configured.
switch typ {
case mruby.TypeArray:
ret, err := coerceArray(val.Array())
if err != nil {
return nil, err
}
retval[arg.String()] = ret
case mruby.TypeHash:
ret, err := coerceHash(val.Hash())
if err != nil {
return nil, err
}
retval[arg.String()] = ret
default:
retval[arg.String()] = val.String()
}
}
return retval, nil
}