Skip to content

Commit

Permalink
fix issue in name trimming not adding all values (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatache authored Dec 17, 2021
1 parent 53b62ca commit e6b64ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 1 addition & 2 deletions pkg/naming/triming.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func init() {
func Truncate(format string, max int, values ...interface{}) string {
var truncated []interface{}
result := fmt.Sprintf(format, values...)

if excess := len(result) - max; excess > 0 {
// we try to reduce the first string we find
for _, value := range values {
if excess == 0 {
truncated = append(truncated, value)
continue
}

Expand All @@ -55,7 +55,6 @@ func Truncate(format string, max int, values ...interface{}) string {

truncated = append(truncated, value)
}

result = fmt.Sprintf(format, truncated...)
}

Expand Down
11 changes: 10 additions & 1 deletion pkg/naming/triming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func TestTruncate(t *testing.T) {
expected: "4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174--collector",
cap: "first value gets dropped, second truncated",
},
{
format: "%s-%s-collector",
max: 63,
values: []interface{}{"4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85b7644b6b5", "d0c1e62"},
expected: "4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11e-d0c1e62-collector",
cap: "first value gets truncated, second added",
},
{
format: "%d-%s-collector",
max: 63,
Expand All @@ -67,7 +74,9 @@ func TestTruncate(t *testing.T) {
cap: "first value gets passed, second truncated",
},
} {
assert.Equal(t, tt.expected, Truncate(tt.format, tt.max, tt.values...))
t.Run(tt.cap, func(t *testing.T) {
assert.Equal(t, tt.expected, Truncate(tt.format, tt.max, tt.values...))
})
}
}

Expand Down

0 comments on commit e6b64ee

Please sign in to comment.