Skip to content

Commit 452ed7c

Browse files
committed
Fix formatting of large numbers
Fixes: magnusbaeck/logstash-filter-verifier#104
1 parent d150e9b commit 452ed7c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

ast/ast.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"strings"
78
)
89

910
// A Config node represents the root node of a Logstash configuration.
@@ -407,7 +408,14 @@ func (na NumberAttribute) String() string {
407408

408409
// ValueString returns the value of the node as a string representation.
409410
func (na NumberAttribute) ValueString() string {
410-
return fmt.Sprintf("%v", na.Value())
411+
value := fmt.Sprintf("%g", na.Value())
412+
if strings.Contains(value, "e") {
413+
if float64(int64(na.Value())+0) == na.Value() {
414+
return fmt.Sprintf("%d", int64(na.Value()))
415+
}
416+
return strings.TrimRight(fmt.Sprintf("%.10f", na.Value()), "0")
417+
}
418+
return value
411419
}
412420

413421
// CommentBlock returns the comment of the node.

logstash_config_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,29 @@ output {}
345345
# Comment
346346
}
347347
}
348+
`,
349+
},
350+
// https://github.com/magnusbaeck/logstash-filter-verifier/issues/104
351+
{
352+
name: "large numbers with and without precission",
353+
input: `filter {
354+
mutate {
355+
add_field => {
356+
"largeint" => 2000000
357+
"largeint_negative" => -2000000
358+
"int32max" => 2147483647
359+
"int32max_negative" => -2147483647
360+
"float_with_5_precision" => 0.00001
361+
"float_with_5_precision_negative" => -0.00001
362+
"float_with_10_precision" => 0.0000000001
363+
"float_with_10_precision_negative" => -0.0000000001
364+
"largefloat_with_10_precision" => 2000000.0000000005
365+
"largefloat_with_10_precision_negative" => -2000000.0000000005
366+
"int32max_with_10_precision" => 2147483647.0000009537
367+
"int32max_with_10_precision_negative" => -2147483647.0000009537
368+
}
369+
}
370+
}
348371
`,
349372
},
350373
}

0 commit comments

Comments
 (0)