(val super.Value)
| 201 | } |
| 202 | |
| 203 | func (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 | |
| 238 | type casterBytes struct { |
| 239 | sctx *super.Context |
nothing calls this directly
no test coverage detected