(from super.Value, to super.Type)
| 46 | } |
| 47 | |
| 48 | func (c *cast) Cast(from super.Value, to super.Type) (super.Value, bool) { |
| 49 | from = from.Deunion() |
| 50 | switch fromType := from.Type(); { |
| 51 | case fromType == to: |
| 52 | return from, true |
| 53 | case fromType.ID() == to.ID(): |
| 54 | return super.NewValue(to, from.Bytes()), true |
| 55 | case fromType == super.TypeNull: |
| 56 | union := c.sctx.Nullable(to) |
| 57 | var b scode.Builder |
| 58 | super.BuildUnion(&b, union.TagOf(super.TypeNull), nil) |
| 59 | return super.NewValue(union, b.Bytes().Body()), true |
| 60 | } |
| 61 | switch to := to.(type) { |
| 62 | case *super.TypeRecord: |
| 63 | return c.toRecord(from, to) |
| 64 | case *super.TypeArray, *super.TypeSet: |
| 65 | return c.toArrayOrSet(from, to) |
| 66 | case *super.TypeMap: |
| 67 | return c.toMap(from, to) |
| 68 | case *super.TypeUnion: |
| 69 | return c.toUnion(from, to) |
| 70 | case *super.TypeError: |
| 71 | return c.toError(from, to) |
| 72 | case *super.TypeNamed: |
| 73 | return c.toNamed(from, to) |
| 74 | default: |
| 75 | caster := expr.LookupPrimitiveCaster(c.sctx, to) |
| 76 | if caster == nil { |
| 77 | return c.error(from, to) |
| 78 | } |
| 79 | val := caster.Eval(from) |
| 80 | return val, !val.IsError() |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func (c *cast) error(from super.Value, to super.Type) (super.Value, bool) { |
| 85 | return c.sctx.WrapError("cannot cast to "+sup.FormatType(to), from), false |
no test coverage detected