diff --git a/pkg/name/name.go b/pkg/name/name.go index 3fb5cb6aae..15b4db6e76 100644 --- a/pkg/name/name.go +++ b/pkg/name/name.go @@ -20,8 +20,12 @@ var ( ) // Limit the length of a string to count characters. If the string's length is -// greater than count, it will be truncated and a hash will be appended to the -// end. +// greater than count, it will be truncated and a separator will be appended to +// the end, followed by a hash. +// If the last character of the truncated string is the separator, then the +// separator itself is omitted. This prevents the result from containing two +// consecutive separators. In such a case, the result will be [count -1] +// characters long. // If count is too small to include the shortened hash the string is simply // truncated. func Limit(s string, count int) string { @@ -29,10 +33,21 @@ func Limit(s string, count int) string { return s } - if count <= 6 { + const hexLen int = 5 + separator := "-" + + if count <= hexLen+len(separator) { return s[:count] } - return fmt.Sprintf("%s-%s", s[:count-6], Hex(s, 5)) + + nbCharsBeforeTrim := count - hexLen - len(separator) + + // If the last character of the truncated string is the separator, include it instead of the separator. + if string(s[nbCharsBeforeTrim-1]) == separator { + separator = "" + } + + return fmt.Sprintf("%s%s%s", s[:nbCharsBeforeTrim], separator, Hex(s, hexLen)) } // Hex returns a hex-encoded hash of the string and truncates it to length. diff --git a/pkg/name/name_test.go b/pkg/name/name_test.go index a0a7708413..f7967ac98d 100644 --- a/pkg/name/name_test.go +++ b/pkg/name/name_test.go @@ -31,6 +31,7 @@ var _ = Describe("Name", func() { {arg: "12345678", n: 8, result: "12345678"}, {arg: "12345678", n: 7, result: "1-25d55"}, {arg: "123456789", n: 8, result: "12-25f9e"}, + {arg: "1-3456789", n: 8, result: "1-9b657"}, // no double dash in the result } It("matches expected results", func() { @@ -51,6 +52,8 @@ var _ = Describe("Name", func() { {arg: "long-name-test-0.App_ ", result: "long-name-test-0-app-5bf6b3fb"}, {arg: "long-name-test--App_-_12.factor", result: "long-name-test-app-12-factor-0efbac37"}, {arg: "bundle.name.example.com", result: "bundle-name-example-com-645ef821"}, + // no double dash in the result + {arg: str53[0:46] + "-1234567", result: "1234567890123456789012345678901234567890123456-d0bce"}, } It("matches expected results", func() {