(val ref.Val)
| 275 | } |
| 276 | |
| 277 | func (un *unparser) visitConstVal(val ref.Val) error { |
| 278 | optional := false |
| 279 | if optVal, ok := val.(*types.Optional); ok { |
| 280 | if !optVal.HasValue() { |
| 281 | un.str.WriteString("optional.none()") |
| 282 | return nil |
| 283 | } |
| 284 | optional = true |
| 285 | un.str.WriteString("optional.of(") |
| 286 | val = optVal.GetValue() |
| 287 | } |
| 288 | switch val := val.(type) { |
| 289 | case types.Bool: |
| 290 | un.str.WriteString(strconv.FormatBool(bool(val))) |
| 291 | case types.Bytes: |
| 292 | // bytes constants are surrounded with b"<bytes>" |
| 293 | un.str.WriteString(`b"`) |
| 294 | un.str.WriteString(bytesToOctets([]byte(val))) |
| 295 | un.str.WriteString(`"`) |
| 296 | case types.Double: |
| 297 | // represent the float using the minimum required digits |
| 298 | d := strconv.FormatFloat(float64(val), 'g', -1, 64) |
| 299 | un.str.WriteString(d) |
| 300 | if !strings.ContainsAny(d, ".eE") { |
| 301 | un.str.WriteString(".0") |
| 302 | } |
| 303 | case types.Int: |
| 304 | i := strconv.FormatInt(int64(val), 10) |
| 305 | un.str.WriteString(i) |
| 306 | case types.Null: |
| 307 | un.str.WriteString("null") |
| 308 | case types.String: |
| 309 | // strings will be double quoted with quotes escaped. |
| 310 | un.str.WriteString(strconv.Quote(string(val))) |
| 311 | case types.Uint: |
| 312 | // uint literals have a 'u' suffix. |
| 313 | ui := strconv.FormatUint(uint64(val), 10) |
| 314 | un.str.WriteString(ui) |
| 315 | un.str.WriteString("u") |
| 316 | case *types.Optional: |
| 317 | if err := un.visitConstVal(val); err != nil { |
| 318 | return err |
| 319 | } |
| 320 | default: |
| 321 | return errors.New("unsupported constant") |
| 322 | } |
| 323 | if optional { |
| 324 | un.str.WriteString(")") |
| 325 | } |
| 326 | return nil |
| 327 | } |
| 328 | func (un *unparser) visitConst(expr ast.Expr) error { |
| 329 | val := expr.AsLiteral() |
| 330 | if err := un.visitConstVal(val); err != nil { |
no test coverage detected