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

Method Eval

runtime/sam/expr/cast.go:203–236  ·  view source on GitHub ↗
(val super.Value)

Source from the content-addressed store, hash-verified

201}
202
203func (c *casterString) Eval(val super.Value) super.Value {
204 val = val.Under()
205 id := val.Type().ID()
206 switch id {
207 case super.IDBytes:
208 if !utf8.Valid(val.Bytes()) {
209 return c.sctx.WrapError("cannot cast to string: invalid UTF-8", val)
210 }
211 return super.NewValue(super.TypeString, val.Bytes())
212 case super.IDString:
213 return super.NewValue(super.TypeString, val.Bytes())
214 case super.IDInt8, super.IDInt16, super.IDInt32, super.IDInt64:
215 return super.NewString(strconv.FormatInt(val.Int(), 10))
216 case super.IDUint8, super.IDUint16, super.IDUint32, super.IDUint64:
217 return super.NewString(strconv.FormatUint(val.Uint(), 10))
218 case super.IDFloat16, super.IDFloat32:
219 return super.NewString(strconv.FormatFloat(val.Float(), 'g', -1, 32))
220 case super.IDFloat64:
221 return super.NewString(strconv.FormatFloat(val.Float(), 'g', -1, 64))
222 case super.IDNull:
223 return c.sctx.WrapError("cannot cast to string", val)
224 }
225 if enum, ok := val.Type().(*super.TypeEnum); ok {
226 selector := super.DecodeUint(val.Bytes())
227 symbol, err := enum.Symbol(int(selector))
228 if err != nil {
229 return c.sctx.NewError(err)
230 }
231 return super.NewString(symbol)
232 }
233 // Otherwise, we'll use a canonical SUP value for the string rep
234 // of an arbitrary value cast to a string.
235 return super.NewString(sup.FormatValue(val))
236}
237
238type casterBytes struct {
239 sctx *super.Context

Callers

nothing calls this directly

Calls 14

NewValueFunction · 0.92
NewStringFunction · 0.92
DecodeUintFunction · 0.92
FormatValueFunction · 0.92
UnderMethod · 0.80
WrapErrorMethod · 0.80
FloatMethod · 0.80
SymbolMethod · 0.80
NewErrorMethod · 0.80
IDMethod · 0.65
TypeMethod · 0.65
BytesMethod · 0.45

Tested by

no test coverage detected