Skip to content

Commit eef3593

Browse files
committed
wip
1 parent 7f8e67f commit eef3593

1 file changed

Lines changed: 41 additions & 38 deletions

File tree

runtime/sam/expr/function/downcast.go

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package function
22

33
import (
4-
"runtime/debug"
4+
"fmt"
55
"slices"
66

77
"github.com/brimdata/super"
@@ -40,15 +40,15 @@ func (d *downcast) Cast(from super.Value, to super.Type) (super.Value, bool) {
4040
}
4141

4242
func (d *downcast) downcast(typ super.Type, bytes scode.Bytes, to super.Type) (super.Value, *super.Value) {
43-
//fmt.Println("DOWNCAST", sup.String(super.NewValue(typ, bytes)), "TO", sup.String(to))
43+
fmt.Println("DOWNCAST", sup.String(super.NewValue(typ, bytes)), "TO", sup.String(to))
4444
// XXX typ, bytes = deunion(typ, bytes)
4545
if _, ok := to.(*super.TypeUnion); !ok {
4646
if fusionType, ok := typ.(*super.TypeFusion); ok {
4747
superBytes, subtype := fusionType.Deref(d.sctx, bytes)
4848
return d.downcast(fusionType.Type, superBytes, subtype)
4949
}
5050
}
51-
typ, bytes = deunion(typ, bytes)
51+
//typ, bytes = deunion(typ, bytes)
5252
switch to := to.(type) {
5353
case *super.TypeRecord:
5454
return d.toRecord(typ, bytes, to)
@@ -210,6 +210,7 @@ func (d *downcast) toError(typ super.Type, bytes scode.Bytes, to *super.TypeErro
210210
}
211211

212212
func (d *downcast) toNamed(typ super.Type, bytes scode.Bytes, to *super.TypeNamed) (super.Value, *super.Value) {
213+
fmt.Println("TO NAMED", sup.String(super.NewValue(typ, bytes)), "TO", sup.String(to))
213214
/*
214215
fromType, ok := typ.(*super.TypeNamed)
215216
if !ok {
@@ -239,59 +240,61 @@ func (d *downcast) toNamed(typ super.Type, bytes scode.Bytes, to *super.TypeName
239240
}
240241
return super.NewValue(to, val.Bytes()), errVal
241242
*/
243+
if unionType, ok := typ.(*super.TypeUnion); ok {
244+
typ, bytes = deunion(typ, bytes)
245+
// If we are casting a union type to a named, we need to look through the
246+
// union for the named type in question since type fusion fuses named
247+
// types by name. Then when we find the name, we need to form the subtype
248+
// from the union options present. XXX devise a torture test that
249+
// reconstruct nested unions from the flattened named union...?
250+
for _, t := range unionType.Types {
251+
if named, ok := t.(*super.TypeNamed); ok && named.Name == to.Name {
252+
val, errVal := d.downcast(typ, bytes, to.Type)
253+
if errVal != nil {
254+
return super.Value{}, errVal
255+
}
256+
return super.NewValue(to, val.Bytes()), nil
257+
}
258+
259+
}
260+
return super.Value{}, d.errMismatch(typ, bytes, to)
261+
}
242262
/*
243-
if unionType, ok := typ.(*super.TypeUnion); !ok {
244-
// If we are casting a union type to a named, we need to look through the
245-
// union for the named type in question since type fusion fuses named
246-
// types by name. Then when we find the name, we need to form the subtype
247-
// from the union options present. XXX devise a torture test that
248-
// reconstruct nested unions from the flattened named union...?
249-
for _, t := range unionType.Types {
250-
if named, ok := t.(*super.TypeNamed); ok && t.Name == to.Name {
251-
val, errVal := d.downcast(named.Type, bytes, to.Type)
263+
fromType, ok := typ.(*super.TypeNamed)
264+
if !ok {
265+
if unionType, ok := typ.(*super.TypeUnion); ok {
266+
if unionType.TagOf(to) >= 0 {
267+
typ, bytes = deunion(typ, bytes)
268+
val, errVal := d.downcast(typ, bytes, to)
252269
if errVal != nil {
253270
return super.Value{}, errVal
254271
}
255-
return super.NewValue(to, val.Bytes()), nil
256-
}
257-
fused := noFusion(f.fuse(existingNamed.Type, noFusion(named.Type)))
258-
var err error
259-
out[i], err = f.sctx.LookupTypeNamed(named.Name, fused)
260-
if err != nil {
261-
panic(err)
272+
return super.NewValue(to, val.Bytes()), errVal
262273
}
263-
264274
}
265-
266-
}
267-
if !ok {
275+
//fmt.Println("MISS", sup.String(typ))
268276
return super.Value{}, d.errMismatch(typ, bytes, to)
269277
}
270278
*/
271-
fromType, ok := typ.(*super.TypeNamed)
272-
if !ok {
273-
if unionType, ok := typ.(*super.TypeUnion); ok {
274-
if unionType.TagOf(to) >= 0 {
275-
typ, bytes = deunion(typ, bytes)
276-
val, errVal := d.downcast(typ, bytes, to)
277-
if errVal != nil {
278-
return super.Value{}, errVal
279-
}
280-
return super.NewValue(to, val.Bytes()), errVal
281-
}
279+
if fromType, ok := typ.(*super.TypeNamed); ok {
280+
if fromType.Name != to.Name {
281+
return super.Value{}, d.errMismatch(typ, bytes, to)
282282
}
283-
//fmt.Println("MISS", sup.String(typ))
284-
return super.Value{}, d.errMismatch(typ, bytes, to)
283+
val, errVal := d.downcast(fromType.Type, bytes, to.Type)
284+
if errVal != nil {
285+
return super.Value{}, errVal
286+
}
287+
return super.NewValue(to, val.Bytes()), errVal
285288
}
286-
val, errVal := d.downcast(fromType.Type, bytes, to.Type)
289+
val, errVal := d.downcast(typ, bytes, to.Type)
287290
if errVal != nil {
288291
return super.Value{}, errVal
289292
}
290293
return super.NewValue(to, val.Bytes()), errVal
291294
}
292295

293296
func (d *downcast) errMismatch(typ super.Type, bytes []byte, to super.Type) *super.Value {
294-
debug.PrintStack()
297+
//debug.PrintStack()
295298
return d.sctx.WrapError("downcast: type mismatch to "+sup.FormatType(to), super.NewValue(typ, bytes)).Ptr()
296299
}
297300

0 commit comments

Comments
 (0)