(val ref.Val)
| 313 | } |
| 314 | |
| 315 | func (un *unparser) visitConstVal(val ref.Val) error { |
| 316 | optional := false |
| 317 | if optVal, ok := val.(*types.Optional); ok { |
| 318 | if !optVal.HasValue() { |
| 319 | un.str.WriteString("optional.none()") |
| 320 | return nil |
| 321 | } |
| 322 | optional = true |
| 323 | un.str.WriteString("optional.of(") |
| 324 | val = optVal.GetValue() |
| 325 | } |
| 326 | switch val := val.(type) { |
| 327 | case types.Bool: |
| 328 | un.str.WriteString(strconv.FormatBool(bool(val))) |
| 329 | case types.Bytes: |
| 330 | // bytes constants are surrounded with b"<bytes>" |
| 331 | un.str.WriteString(`b"`) |
| 332 | un.str.WriteString(bytesToOctets([]byte(val))) |
| 333 | un.str.WriteString(`"`) |
| 334 | case types.Double: |
| 335 | // represent the float using the minimum required digits |
| 336 | d := strconv.FormatFloat(float64(val), 'g', -1, 64) |
| 337 | un.str.WriteString(d) |
| 338 | if !strings.ContainsAny(d, ".eE") { |
| 339 | un.str.WriteString(".0") |
| 340 | } |
| 341 | case types.Int: |
| 342 | i := strconv.FormatInt(int64(val), 10) |
| 343 | un.str.WriteString(i) |
| 344 | case types.Null: |
| 345 | un.str.WriteString("null") |
| 346 | case types.String: |
| 347 | // strings will be double quoted with quotes escaped. |
| 348 | un.str.WriteString(strconv.Quote(string(val))) |
| 349 | case types.Uint: |
| 350 | // uint literals have a 'u' suffix. |
| 351 | ui := strconv.FormatUint(uint64(val), 10) |
| 352 | un.str.WriteString(ui) |
| 353 | un.str.WriteString("u") |
| 354 | case *types.Optional: |
| 355 | if err := un.visitConstVal(val); err != nil { |
| 356 | return err |
| 357 | } |
| 358 | default: |
| 359 | return errors.New("unsupported constant") |
| 360 | } |
| 361 | if optional { |
| 362 | un.str.WriteString(")") |
| 363 | } |
| 364 | return nil |
| 365 | } |
| 366 | func (un *unparser) visitConst(expr ast.Expr) error { |
| 367 | val := expr.AsLiteral() |
| 368 | if err := un.visitConstVal(val); err != nil { |
no test coverage detected