Skip to content

Commit 23c8ee1

Browse files
minor refactor of replaceInvalidRune() in bridge.go (#1897)
Signed-off-by: Karthik Kondapally <[email protected]>
1 parent fa8da34 commit 23c8ee1

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

prometheus/graphite/bridge.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,13 @@ func replaceInvalidRune(c rune) rune {
307307
if c == ' ' {
308308
return '.'
309309
}
310-
// TODO: Apply De Morgan's law to the condition. Make sure to test the condition first.
311-
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':' || c == '-' || (c >= '0' && c <= '9')) { //nolint:staticcheck
310+
// !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':' || c == '-' || (c >= '0' && c <= '9'))
311+
if (c < 'a' || c > 'z') &&
312+
(c < 'A' || c > 'Z') &&
313+
c != '_' &&
314+
c != ':' &&
315+
c != '-' &&
316+
(c < '0' || c > '9') {
312317
return '_'
313318
}
314319
return c

prometheus/graphite/bridge_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,33 @@ func ExampleBridge() {
468468
// Start pushing metrics to Graphite in the Run() loop.
469469
b.Run(ctx)
470470
}
471+
472+
func TestReplaceInvalidRune(t *testing.T) {
473+
tests := []struct {
474+
in rune
475+
want rune
476+
}{
477+
{' ', '.'},
478+
479+
{'a', 'a'},
480+
{'B', 'B'},
481+
{'0', '0'},
482+
{'9', '9'},
483+
{'_', '_'},
484+
{':', ':'},
485+
{'-', '-'},
486+
487+
{'#', '_'},
488+
{'$', '_'},
489+
{'@', '_'},
490+
{'!', '_'},
491+
{'~', '_'},
492+
}
493+
494+
for _, tt := range tests {
495+
got := replaceInvalidRune(tt.in)
496+
if got != tt.want {
497+
t.Fatalf("replaceInvalidRune(%q) = %q; want %q", tt.in, got, tt.want)
498+
}
499+
}
500+
}

0 commit comments

Comments
 (0)