Merge pull request #12270 from burnison/11294-shortidfallback
Only use fallback to short IDs when obvious.
This commit is contained in:
commit
eef8989683
2 changed files with 33 additions and 4 deletions
|
@ -4,19 +4,27 @@ import (
|
|||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const shortLen = 12
|
||||
|
||||
// Determine if an arbitrary string *looks like* a short ID.
|
||||
func IsShortID(id string) bool {
|
||||
return regexp.MustCompile("^[a-z0-9]{12}$").MatchString(id)
|
||||
}
|
||||
|
||||
// TruncateID returns a shorthand version of a string identifier for convenience.
|
||||
// A collision with other shorthands is very unlikely, but possible.
|
||||
// In case of a collision a lookup with TruncIndex.Get() will fail, and the caller
|
||||
// will need to use a langer prefix, or the full-length Id.
|
||||
func TruncateID(id string) string {
|
||||
shortLen := 12
|
||||
trimTo := shortLen
|
||||
if len(id) < shortLen {
|
||||
shortLen = len(id)
|
||||
trimTo = len(id)
|
||||
}
|
||||
return id[:shortLen]
|
||||
return id[:trimTo]
|
||||
}
|
||||
|
||||
// GenerateRandomID returns an unique id
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package stringid
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenerateRandomID(t *testing.T) {
|
||||
id := GenerateRandomID()
|
||||
|
@ -33,3 +36,21 @@ func TestShortenIdInvalid(t *testing.T) {
|
|||
t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsShortIDNonHex(t *testing.T) {
|
||||
id := "some non-hex value"
|
||||
if IsShortID(id) {
|
||||
t.Fatalf("%s is not a short ID", id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsShortIDNotCorrectSize(t *testing.T) {
|
||||
id := strings.Repeat("a", shortLen+1)
|
||||
if IsShortID(id) {
|
||||
t.Fatalf("%s is not a short ID", id)
|
||||
}
|
||||
id = strings.Repeat("a", shortLen-1)
|
||||
if IsShortID(id) {
|
||||
t.Fatalf("%s is not a short ID", id)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue