ResolveAsType implements the Constant interface.
( ctx context.Context, semaCtx *SemaContext, typ *types.T, )
| 568 | |
| 569 | // ResolveAsType implements the Constant interface. |
| 570 | func (expr *StrVal) ResolveAsType( |
| 571 | ctx context.Context, semaCtx *SemaContext, typ *types.T, |
| 572 | ) (TypedExpr, error) { |
| 573 | if expr.scannedAsBytes { |
| 574 | // We're looking at typing a byte literal constant into some value type. |
| 575 | switch typ.Family() { |
| 576 | case types.BytesFamily: |
| 577 | expr.resBytes = DBytes(expr.s) |
| 578 | return &expr.resBytes, nil |
| 579 | case types.EnumFamily: |
| 580 | return MakeDEnumFromPhysicalRepresentation(typ, []byte(expr.s)) |
| 581 | case types.UuidFamily: |
| 582 | return ParseDUuidFromBytes([]byte(expr.s)) |
| 583 | case types.StringFamily: |
| 584 | // bpchar types truncate trailing whitespace. |
| 585 | if typ.Oid() == oid.T_bpchar { |
| 586 | expr.resString = DString(strings.TrimRight(expr.s, " ")) |
| 587 | return &expr.resString, nil |
| 588 | } |
| 589 | expr.resString = DString(expr.s) |
| 590 | return &expr.resString, nil |
| 591 | } |
| 592 | return nil, errors.AssertionFailedf("attempt to type byte array literal to %T", typ) |
| 593 | } |
| 594 | |
| 595 | // Typing a string literal constant into some value type. |
| 596 | switch typ.Family() { |
| 597 | case types.StringFamily: |
| 598 | if typ.Oid() == oid.T_name { |
| 599 | expr.resString = DString(expr.s) |
| 600 | return NewDNameFromDString(&expr.resString), nil |
| 601 | } |
| 602 | // bpchar types truncate trailing whitespace. |
| 603 | if typ.Oid() == oid.T_bpchar { |
| 604 | expr.resString = DString(strings.TrimRight(expr.s, " ")) |
| 605 | return &expr.resString, nil |
| 606 | } |
| 607 | expr.resString = DString(expr.s) |
| 608 | return &expr.resString, nil |
| 609 | |
| 610 | case types.BytesFamily: |
| 611 | return ParseDByte(expr.s) |
| 612 | |
| 613 | default: |
| 614 | val, dependsOnContext, err := ParseAndRequireString(typ, expr.s, dummyParseTimeContext{}) |
| 615 | if err != nil { |
| 616 | return nil, err |
| 617 | } |
| 618 | if !dependsOnContext { |
| 619 | return val, nil |
| 620 | } |
| 621 | // Interpreting a string as one of these types may depend on the timezone or |
| 622 | // the current time; the value won't be safe to reuse later. So in this case |
| 623 | // we return a CastExpr and let the conversion happen at evaluation time. We |
| 624 | // still want to error out if the conversion is not possible though (this is |
| 625 | // used when resolving overloads). |
| 626 | expr.resString = DString(expr.s) |
| 627 | c := NewTypedCastExpr(&expr.resString, typ) |
nothing calls this directly
no test coverage detected