-
Notifications
You must be signed in to change notification settings - Fork 297
/
value.go
211 lines (181 loc) · 4.67 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
package eval
import (
"fmt"
"github.com/elves/elvish/util"
"github.com/xiaq/persistent/hashmap"
)
// Definitions for Value interfaces, some simple Value types and some common
// Value helpers.
const (
NoPretty = util.MinInt
)
// Value is an elvish value.
type Value interface {
Kinder
Equaler
Hasher
Reprer
}
// Kinder is anything with kind string.
type Kinder interface {
Kind() string
}
// Reprer is anything with a Repr method.
type Reprer interface {
// Repr returns a string that represents a Value. The string either be a
// literal of that Value that is preferably deep-equal to it (like `[a b c]`
// for a list), or a string enclosed in "<>" containing the kind and
// identity of the Value(like `<fn 0xdeadcafe>`).
//
// If indent is at least 0, it should be pretty-printed with the current
// indentation level of indent; the indent of the first line has already
// been written and shall not be written in Repr. The returned string
// should never contain a trailing newline.
Repr(indent int) string
}
// Equaler is anything that knows how to compare itself against other values.
type Equaler interface {
Equal(interface{}) bool
}
// Hasher is anything that knows how to compute its hash code.
type Hasher interface {
Hash() uint32
}
// Booler is anything that can be converted to a bool.
type Booler interface {
Bool() bool
}
// Stringer is anything that can be converted to a string.
type Stringer interface {
String() string
}
// Lener is anything with a length.
type Lener interface {
Len() int
}
// Iterable is anything that can be iterated.
type Iterable interface {
Iterate(func(Value) bool)
}
type IterableValue interface {
Iterable
Value
}
func collectFromIterable(it Iterable) []Value {
var vs []Value
if lener, ok := it.(Lener); ok {
vs = make([]Value, 0, lener.Len())
}
it.Iterate(func(v Value) bool {
vs = append(vs, v)
return true
})
return vs
}
// IterateKeyer is anything with keys that can be iterated.
type IterateKeyer interface {
IterateKey(func(Value) bool)
}
// IteratePairer is anything with key-value pairs that can be iterated.
type IteratePairer interface {
IteratePair(func(Value, Value) bool)
}
var (
NoArgs = []Value{}
NoOpts = map[string]Value{}
)
// Callable is anything may be called on an evalCtx with a list of Value's.
type Callable interface {
Call(ec *EvalCtx, args []Value, opts map[string]Value)
}
type CallableValue interface {
Value
Callable
}
func mustFn(v Value) Callable {
fn, ok := v.(Callable)
if !ok {
throw(fmt.Errorf("a %s is not callable", v.Kind()))
}
return fn
}
// Indexer is anything that can be indexed by Values and yields Values.
type Indexer interface {
Index(idx []Value) []Value
}
// IndexOneer is anything that can be indexed by one Value and yields one Value.
type IndexOneer interface {
IndexOne(idx Value) Value
}
func mustIndexer(v Value, ec *EvalCtx) Indexer {
indexer, ok := getIndexer(v, ec)
if !ok {
throw(fmt.Errorf("a %s is not indexable", v.Kind()))
}
return indexer
}
// getIndexer adapts a Value to an Indexer if there is an adapter. It adapts a
// Fn if ec is not nil.
func getIndexer(v Value, ec *EvalCtx) (Indexer, bool) {
if indexer, ok := v.(Indexer); ok {
return indexer, true
}
if indexOneer, ok := v.(IndexOneer); ok {
return IndexOneerIndexer{indexOneer}, true
}
return nil, false
}
// IndexOneerIndexer adapts an IndexOneer to an Indexer by calling all the
// indicies on the IndexOner and collect the results.
type IndexOneerIndexer struct {
IndexOneer
}
func (ioi IndexOneerIndexer) Index(vs []Value) []Value {
results := make([]Value, len(vs))
for i, v := range vs {
results[i] = ioi.IndexOneer.IndexOne(v)
}
return results
}
// Assocer is anything tha can return a slightly modified version of itself as a
// new Value.
type Assocer interface {
Assoc(k, v Value) Value
}
// IndexOneAssocer combines IndexOneer and Assocer.
type IndexOneAssocer interface {
IndexOneer
Assocer
}
// FromJSONInterface converts a interface{} that results from json.Unmarshal to
// a Value.
func FromJSONInterface(v interface{}) Value {
if v == nil {
// TODO Use a more appropriate type
return String("")
}
switch v.(type) {
case bool:
return Bool(v.(bool))
case float64, string:
// TODO Use a numeric type for float64
return String(fmt.Sprint(v))
case []interface{}:
a := v.([]interface{})
vs := make([]Value, len(a))
for i, v := range a {
vs[i] = FromJSONInterface(v)
}
return NewList(vs...)
case map[string]interface{}:
m := v.(map[string]interface{})
mv := hashmap.Empty
for k, v := range m {
mv = mv.Assoc(String(k), FromJSONInterface(v))
}
return NewMap(mv)
default:
throw(fmt.Errorf("unexpected json type: %T", v))
return nil // not reached
}
}