grafana/pkg/util/shortid_generator.go
Hugo Häggmark fc73bc1161
LibraryElements: Enables creating library elements with specific UID (#39019)
* LibraryPanels: Enables create/update library panels with specific UID

* Chore: added check for uid length after PR comments

* Refactor: creates IsShortUIDTooLong function

* Refactor: adds UID to PATCH endpoint

* Refactor: clarifies the patch code

* Refactor: changes  after PR comments
2021-09-10 11:22:13 +02:00

32 lines
676 B
Go

package util
import (
"regexp"
"github.com/teris-io/shortid"
)
var allowedChars = shortid.DefaultABC
var validUIDPattern = regexp.MustCompile(`^[a-zA-Z0-9\-\_]*$`).MatchString
func init() {
gen, _ := shortid.New(1, allowedChars, 1)
shortid.SetDefault(gen)
}
// IsValidShortUID checks if short unique identifier contains valid characters
func IsValidShortUID(uid string) bool {
return validUIDPattern(uid)
}
// IsShortUIDTooLong checks if short unique identifier is too long
func IsShortUIDTooLong(uid string) bool {
return len(uid) > 40
}
// GenerateShortUID generates a short unique identifier.
func GenerateShortUID() string {
return shortid.MustGenerate()
}