Skip to content

Commit d7bc9d0

Browse files
committed
strip 'u64' suffix from rust-style json
Signed-off-by: MikeN. <mikenichols@provable.com>
1 parent 7441476 commit d7bc9d0

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

exporter/util.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@ func SanitizeValue(s string) (float64, error) {
6363
func SanitizeIntValue(s string) (int64, error) {
6464
var err error
6565
var value int64
66+
var cleanedString = s
6667
var resultErr string
6768

68-
if value, err = strconv.ParseInt(s, 10, 64); err == nil {
69+
// Check if the string ends with "u64" and strip it off.
70+
if strings.HasSuffix(s, "u64") {
71+
cleanedString = strings.TrimSuffix(s, "u64")
72+
}
73+
74+
if value, err = strconv.ParseInt(cleanedString, 10, 64); err == nil {
6975
return value, nil
7076
}
7177
resultErr = fmt.Sprintf("%s", err)

exporter/util_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,53 @@ func TestSanitizeValueNaN(t *testing.T) {
5757
t.Fatalf("Value sanitization test for %f fails unexpectedly.", math.NaN())
5858
}
5959
}
60+
61+
func TestSanitizeIntValue(t *testing.T) {
62+
tests := []struct {
63+
Input string
64+
ExpectedOutput int64
65+
ShouldSucceed bool
66+
}{
67+
// Baseline int64 parsing.
68+
{"1234", 1234, true},
69+
{"0", 0, true},
70+
{"-1234", -1234, true},
71+
{"9223372036854775807", 9223372036854775807, true}, // math.MaxInt64
72+
{"-9223372036854775808", -9223372036854775808, true},
73+
74+
// Rust-style "u64" suffix should be stripped before parsing.
75+
{"1234u64", 1234, true},
76+
{"0u64", 0, true},
77+
{"9223372036854775807u64", 9223372036854775807, true},
78+
79+
// Values exceeding int64 range must still fail even after stripping.
80+
{"9223372036854775808u64", 0, false}, // math.MaxInt64 + 1
81+
{"18446744073709551615u64", 0, false}, // math.MaxUint64
82+
83+
// Suffix stripping is exact: only trailing "u64", case-sensitive, whole suffix.
84+
{"1234U64", 0, false}, // uppercase not stripped
85+
{"1234u32", 0, false}, // only u64 handled
86+
{"u641234", 0, false}, // suffix must be at end
87+
{"u64", 0, false}, // empty after stripping
88+
89+
// Non-numeric and float inputs are not integers.
90+
{"abcd", 0, false},
91+
{"1234.5", 0, false},
92+
{"1234.5u64", 0, false}, // stripped to "1234.5" - still not an int
93+
{"", 0, false},
94+
{"true", 0, false},
95+
}
96+
97+
for i, test := range tests {
98+
actualOutput, err := SanitizeIntValue(test.Input)
99+
if err != nil && test.ShouldSucceed {
100+
t.Fatalf("Int value sanitization test %d failed with an unexpected error.\nINPUT:\n%q\nERR:\n%s", i, test.Input, err)
101+
}
102+
if err == nil && !test.ShouldSucceed {
103+
t.Fatalf("Int value sanitization test %d succeeded unexpectedly.\nINPUT:\n%q\nGOT:\n%d", i, test.Input, actualOutput)
104+
}
105+
if test.ShouldSucceed && actualOutput != test.ExpectedOutput {
106+
t.Fatalf("Int value sanitization test %d fails unexpectedly.\nINPUT:\n%q\nGOT:\n%d\nEXPECTED:\n%d", i, test.Input, actualOutput, test.ExpectedOutput)
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)