| 24 | } |
| 25 | |
| 26 | func recode(dst scode.Bytes, typ *super.TypeRecord, in scode.Bytes) (scode.Bytes, error) { |
| 27 | if typ.Opts != 0 { |
| 28 | panic("need to add support for optional fields in flattener") |
| 29 | } |
| 30 | if in == nil { |
| 31 | for _, f := range typ.Fields { |
| 32 | if typ, ok := super.TypeUnder(f.Type).(*super.TypeRecord); ok { |
| 33 | var err error |
| 34 | dst, err = recode(dst, typ, nil) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | } else { |
| 39 | dst = scode.Append(dst, nil) |
| 40 | } |
| 41 | } |
| 42 | return dst, nil |
| 43 | } |
| 44 | it := scode.NewRecordIter(in, typ.Opts) |
| 45 | fieldno := 0 |
| 46 | for !it.Done() { |
| 47 | f := typ.Fields[fieldno] |
| 48 | val, none := it.Next(f.Opt) |
| 49 | fieldno++ |
| 50 | if none { |
| 51 | dst = scode.Append(dst, nil) |
| 52 | continue |
| 53 | } |
| 54 | if childType, ok := super.TypeUnder(f.Type).(*super.TypeRecord); ok { |
| 55 | var err error |
| 56 | dst, err = recode(dst, childType, val) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | } else { |
| 61 | dst = scode.Append(dst, val) |
| 62 | } |
| 63 | } |
| 64 | return dst, nil |
| 65 | } |
| 66 | |
| 67 | func (f *Flattener) Flatten(r super.Value) (super.Value, error) { |
| 68 | typ := r.Type() |