-
Notifications
You must be signed in to change notification settings - Fork 22
/
integrate.go
331 lines (313 loc) · 9.95 KB
/
integrate.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//
// Copyright © 2018 Aljabr, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package symbol
import (
"reflect"
"github.com/kocircuit/kocircuit/lang/circuit/model"
"github.com/kocircuit/kocircuit/lang/go/gate"
"github.com/kocircuit/kocircuit/lang/go/kit/tree"
)
// IntegrateInterface integrates a Ko value into a Go value of given type,
// returning the Go value as interface{}.
func IntegrateInterface(span *model.Span, s Symbol, t reflect.Type) (interface{}, error) {
if v, err := Integrate(span, s, t); err != nil {
return nil, err
} else {
return v.Interface(), nil
}
}
// Integrate a Ko value into a Go value of given type.
func Integrate(span *model.Span, s Symbol, t reflect.Type) (reflect.Value, error) {
ctx := &typingCtx{Span: span}
return ctx.Integrate(s, t)
}
func (ctx *typingCtx) Integrate(s Symbol, t reflect.Type) (reflect.Value, error) {
if r, err := ctx.integrateNamed(s, t); err == nil { // try
return r, nil
}
// if s is named, deconstruct its go value
if named, ok := s.(*NamedSymbol); ok {
if dec, err := ctx.DeconstructKind(named.Value); err != nil {
return reflect.Value{}, err
} else {
s = dec
}
}
return ctx.IntegrateKind(s, t)
}
func (ctx *typingCtx) integrateNamed(s Symbol, t reflect.Type) (reflect.Value, error) {
tName := tree.TypeName(t)
if tName == "" {
return reflect.Value{}, ctx.Errorf(nil, "to-type is not named")
}
if t.Kind() == reflect.Interface {
// interface named types are handled in Integrate
return reflect.Value{}, ctx.Errorf(nil, "to-type is an interface")
}
sNamed, ok := s.(*NamedSymbol)
if !ok {
return reflect.Value{}, ctx.Errorf(nil, "from-symbol is not named")
}
sGoType := sNamed.GoType()
if sGoType == t {
return sNamed.Value, nil
} else {
return reflect.Value{}, ctx.Errorf(nil,
"cannot integrate named type %s to named type %s",
tree.TypeName(sGoType), tree.TypeName(t),
)
}
}
func (ctx *typingCtx) IntegrateKind(s Symbol, t reflect.Type) (reflect.Value, error) {
if t == typeOfSymbol {
return reflect.ValueOf(s), nil
}
switch t.Kind() {
case reflect.Invalid:
panic("o")
case reflect.String:
if g, err := ctx.IntegrateBasic(s, t); err == nil {
return g, nil
} else if blob, ok := s.(*BlobSymbol); ok { // blob -> string
return blob.Value.Convert(t), nil
}
case reflect.Bool:
return ctx.IntegrateBasic(s, t)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return ctx.IntegrateBasicBits(s, t)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return ctx.IntegrateBasicBits(s, t)
case reflect.Float32, reflect.Float64:
return ctx.IntegrateBasicBits(s, t)
case reflect.Uintptr: // defer to IntegrateFrom Named/Opaque
case reflect.Complex64: // defer to IntegrateFrom Named/Opaque
case reflect.Complex128: // defer to IntegrateFrom Named/Opaque
case reflect.Array: // defer to IntegrateFrom Named/Opaque
case reflect.Chan: // defer to IntegrateFrom Named/Opaque
case reflect.UnsafePointer: // defer to IntegrateFrom Named/Opaque
case reflect.Func: // defer to IntegrateFrom Named/Opaque
case reflect.Interface:
return s.DisassembleToGo(ctx.Span)
case reflect.Map:
if IsEmptySymbol(s) {
return reflect.Zero(t), nil
} else if extracted, err := ExtractMap(ctx.Span, s, t); err == nil {
return ctx.IntegrateKind(extracted, t) // try again
} else if ms, ok := s.(*MapSymbol); ok {
return ctx.IntegrateMapMap(ms, t)
} else {
// defer to IntegrateFrom Named/Opaque
}
case reflect.Ptr:
if IsEmptySymbol(s) {
return reflect.Zero(t), nil
} else {
if elem, err := ctx.Integrate(s, t.Elem()); err != nil {
return reflect.Value{}, err
} else {
var u reflect.Value
if elem.CanAddr() {
u = elem
} else {
u = reflect.New(elem.Type()).Elem()
u.Set(elem)
}
w := reflect.New(t).Elem()
w.Set(u.Addr())
return w, nil
}
}
case reflect.Slice:
if IsEmptySymbol(s) {
return reflect.Zero(t), nil
} else if t == byteSliceType {
if blob, isBlob := s.(*BlobSymbol); isBlob { // blob -> []byte
return blob.Value, nil
} else if s, isString := AsBasicString(s); isString { // string -> []byte
return reflect.ValueOf(s).Convert(t), nil
}
}
return ctx.IntegrateSlice(s, t)
case reflect.Struct: // catch missing fields
if IsEmptySymbol(s) {
return ctx.IntegrateStruct(MakeStructSymbol(nil), t)
} else if ss, ok := s.(*StructSymbol); ok {
return ctx.IntegrateStruct(ss, t)
} else if ms, ok := s.(*MapSymbol); ok {
return ctx.IntegrateMapStruct(ms, t)
}
}
//
switch u := s.(type) {
case *OpaqueSymbol:
return ctx.IntegrateFromOpaque(u, t)
case *NamedSymbol:
return ctx.IntegrateFromNamed(u, t)
}
return reflect.Value{}, ctx.Errorf(nil, "cannot integrate %s into %v", tree.Sprint(s), t)
}
func (ctx *typingCtx) IntegrateFromOpaque(u *OpaqueSymbol, t reflect.Type) (reflect.Value, error) {
if u.GoType().AssignableTo(t) {
if u.Value.CanAddr() {
return u.Value, nil
} else {
w := reflect.New(t).Elem()
w.Set(u.Value)
return w, nil
}
} else {
return reflect.Value{},
ctx.Errorf(nil, "cannot integrate opaque type %v into go type %v", u.Type(), t)
}
}
func (ctx *typingCtx) IntegrateFromNamed(u *NamedSymbol, t reflect.Type) (reflect.Value, error) {
goType := u.GoType()
if goType.AssignableTo(t) { // T -> interface
return u.Value, nil
} else if reflect.PtrTo(goType).AssignableTo(t) { // *T -> interface
if u.Value.CanAddr() {
return u.Value.Addr(), nil
} else {
w := reflect.New(goType)
w.Elem().Set(u.Value)
return w, nil
}
} else {
return reflect.Value{},
ctx.Errorf(nil, "cannot integrate named type %v into go type %v", u.GoType(), t)
}
}
func (ctx *typingCtx) IntegrateBasic(s Symbol, t reflect.Type) (reflect.Value, error) {
if basic, ok := s.(BasicSymbol); !ok {
return reflect.Value{}, ctx.Errorf(nil, "value %v is not basic, cannot integrate to %v", s, t)
} else {
stype := reflect.TypeOf(basic.Value)
if stype.ConvertibleTo(t) {
return reflect.ValueOf(basic.Value).Convert(t), nil
} else {
return reflect.Value{}, ctx.Errorf(nil, "value %v (of type %v) is not convertible to %v", s, s.Type(), t)
}
}
}
func (ctx *typingCtx) IntegrateBasicBits(s Symbol, t reflect.Type) (reflect.Value, error) {
if basic, ok := s.(BasicSymbol); !ok {
return reflect.Value{}, ctx.Errorf(nil, "value %v is not basic, cannot integrate to %v", s, t)
} else {
stype := reflect.TypeOf(basic.Value)
if stype.ConvertibleTo(t) && stype.Bits() <= t.Bits() {
return reflect.ValueOf(basic.Value).Convert(t), nil
} else {
return reflect.Value{}, ctx.Errorf(nil, "value %v (of type %v) is not convertible to %v", s, s.Type(), t)
}
}
}
func (ctx *typingCtx) IntegrateSlice(s Symbol, t reflect.Type) (reflect.Value, error) {
ss := s.LiftToSeries(ctx.Span)
elems := make([]reflect.Value, len(ss.Elem))
ctx2 := ctx.Refine("()")
for i, symElem := range ss.Elem {
if u, err := ctx2.Integrate(symElem, t.Elem()); err != nil {
return reflect.Value{}, err
} else {
elems[i] = u
}
}
w := reflect.MakeSlice(t, len(elems), len(elems))
for i, elem := range elems {
w.Index(i).Set(elem)
}
return w, nil
}
func (ctx *typingCtx) IntegrateStruct(ss *StructSymbol, t reflect.Type) (reflect.Value, error) {
w := reflect.New(t).Elem()
for i := 0; i < t.NumField(); i++ {
toField := t.Field(i)
if from := FindIntegrationField(ss, toField); from == nil {
if !gate.StructFieldIsOptional(toField) {
return reflect.Value{},
ctx.Errorf(nil, "go field %s in %v is required, not found in %v", toField.Name, t, ss)
}
} else {
if u, err := ctx.Refine(toField.Name).Integrate(from.Value, toField.Type); err != nil {
return reflect.Value{}, err
} else {
w.Field(i).Set(u)
}
}
}
return w, nil
}
func FindIntegrationField(from *StructSymbol, to reflect.StructField) *FieldSymbol {
name, hasKoName := gate.StructFieldKoProtoGoName(to)
if !hasKoName {
return nil
}
if gate.IsStructFieldMonadic(to) {
if monadicField := from.FindMonadic(); monadicField != nil {
return monadicField
}
}
return from.FindName(name)
}
func (ctx *typingCtx) IntegrateMapMap(ms *MapSymbol, t reflect.Type) (reflect.Value, error) {
if t.Key() != typeOfString {
return reflect.Value{}, ctx.Errorf(nil, "map %v cannot integrate into go map %v", ms, t)
}
w := reflect.MakeMap(t)
for k, vsym := range ms.Map {
if wsym, err := ctx.Refine(k).Integrate(vsym, t.Elem()); err != nil {
return reflect.Value{},
ctx.Errorf(err,
"map value %v (type %v) cannot integrate into go map value %v",
vsym, vsym.Type(), t.Elem(),
)
} else {
w.SetMapIndex(reflect.ValueOf(k), wsym)
}
}
return w, nil
}
func (ctx *typingCtx) IntegrateMapStruct(ms *MapSymbol, t reflect.Type) (reflect.Value, error) {
w := reflect.New(t).Elem()
for i := 0; i < t.NumField(); i++ {
toField := t.Field(i)
if fromValue := FindIntegrationKey(ms, toField); fromValue == nil {
if !gate.StructFieldIsOptional(toField) {
return reflect.Value{},
ctx.Errorf(nil, "go field %s in %v is required, not found in %v", toField.Name, t, ms)
}
} else {
if u, err := ctx.Refine(toField.Name).Integrate(fromValue, toField.Type); err != nil {
return reflect.Value{}, err
} else {
w.Field(i).Set(u)
}
}
}
return w, nil
}
func FindIntegrationKey(from *MapSymbol, to reflect.StructField) Symbol {
name, hasKoName := gate.StructFieldKoProtoGoName(to)
if !hasKoName {
return nil
}
if gate.IsStructFieldMonadic(to) {
if monadic, ok := from.Map[""]; ok {
return monadic
}
}
return from.Map[name]
}