Skip to content

Commit

Permalink
sql/internal/specutil: use string values for non number/bool literal
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Jul 25, 2024
1 parent d43199a commit fc10968
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
21 changes: 9 additions & 12 deletions sql/internal/specutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,13 +965,14 @@ func ColumnDefault(c *schema.Column) (cty.Value, error) {
return cty.StringVal(s), nil
case strings.ToLower(x.V) == "true", strings.ToLower(x.V) == "false":
return cty.BoolVal(strings.ToLower(x.V) == "true"), nil
case strings.Contains(x.V, "."):
f, err := strconv.ParseFloat(x.V, 64)
if err != nil {
return cty.NilVal, err
}
return cty.NumberFloatVal(f), nil
case sqlx.IsLiteralNumber(x.V) && !textlike:
if strings.Contains(x.V, ".") {
f, err := strconv.ParseFloat(x.V, 64)
if err != nil {
return cty.NilVal, err
}
return cty.NumberFloatVal(f), nil
}
switch i, err := strconv.ParseInt(x.V, 10, 64); {
case errors.Is(err, strconv.ErrRange):
u, err := strconv.ParseUint(x.V, 10, 64)
Expand All @@ -985,12 +986,8 @@ func ColumnDefault(c *schema.Column) (cty.Value, error) {
return cty.NumberIntVal(i), nil
}
default:
// Literal values (non-expressions) are returned
// as strings for text-like types.
if textlike {
return cty.StringVal(x.V), nil
}
return cty.NilVal, fmt.Errorf("unsupported literal value %s for column %s", x.V, c.Name)
// Literal values (non-expressions) are returned as strings.
return cty.StringVal(x.V), nil
}
default:
return cty.NilVal, fmt.Errorf("converting expr %T to literal value for column %s", x, c.Name)
Expand Down
8 changes: 8 additions & 0 deletions sql/internal/specutil/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ func TestDefault(t *testing.T) {
v: cty.NumberFloatVal(-1024.1024),
x: "-1024.1024",
},
{
v: cty.StringVal("{}"),
x: "{}",
},
{
v: cty.StringVal("1a.1"),
x: "1a.1",
},
} {
// From cty.Value (HCL) to database literal.
x, err := Default(tt.v)
Expand Down

0 comments on commit fc10968

Please sign in to comment.