Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix numeric unparsing for floats which represent integer values #686

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions parser/unparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ func (un *unparser) visitConst(expr *exprpb.Expr) error {
// represent the float using the minimum required digits
d := strconv.FormatFloat(c.GetDoubleValue(), 'g', -1, 64)
un.str.WriteString(d)
if !strings.Contains(d, ".") {
un.str.WriteString(".0")
}
case *exprpb.Constant_Int64Value:
i := strconv.FormatInt(c.GetInt64Value(), 10)
un.str.WriteString(i)
Expand Down
5 changes: 5 additions & 0 deletions parser/unparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func TestUnparse(t *testing.T) {
{name: "func_in", in: `a in b`},
{name: "list_empty", in: `[]`},
{name: "list_one", in: `[1]`},
{name: "list_ints", in: `[1, 2, 3]`},
{name: "list_doubles", in: `[1.0, 2.0, 3.0]`},
{name: "list_doubles", in: `[1.1, 2.1, 3.1]`},
{name: "list_uints", in: `[1u, 2u, 3u]`},
{name: "list_numeric", in: `[1, 2.0, 3u]`},
{name: "list_many", in: `["hello, world", "goodbye, world", "sure, why not?"]`},
{name: "lit_bytes", in: `b"\303\203\302\277"`},
{name: "lit_double", in: `-42.101`},
Expand Down