MCPcopy Create free account
hub / github.com/brimdata/super / Cast

Method Cast

runtime/sam/expr/function/cast.go:48–82  ·  view source on GitHub ↗
(from super.Value, to super.Type)

Source from the content-addressed store, hash-verified

46}
47
48func (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
84func (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

Callers 5

CallMethod · 0.95
toRecordMethod · 0.95
castNextMethod · 0.95
toErrorMethod · 0.95
toNamedMethod · 0.95

Calls 15

BytesMethod · 0.95
toRecordMethod · 0.95
toArrayOrSetMethod · 0.95
toMapMethod · 0.95
toUnionMethod · 0.95
toErrorMethod · 0.95
toNamedMethod · 0.95
errorMethod · 0.95
NewValueFunction · 0.92
BuildUnionFunction · 0.92
LookupPrimitiveCasterFunction · 0.92
DeunionMethod · 0.80

Tested by

no test coverage detected