PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 1ms

/internal/service/types/id.go

https://bitbucket.org/kamilsk/click
Go | 24 lines | 13 code | 6 blank | 5 comment | 2 complexity | b2729f5797798dd3398fa2745d78aa48 MD5 | raw file
Possible License(s): BSD-2-Clause, MIT, JSON, BSD-3-Clause, 0BSD, Apache-2.0, MPL-2.0-no-copyleft-exception
  1. package types
  2. import "regexp"
  3. // [4] means that supported only v4.
  4. var uuid = regexp.MustCompile(`(?i:^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$)`)
  5. // ID wraps built-in `string` type and provides useful methods above it.
  6. type ID string
  7. // IsEmpty returns true if the ID is empty.
  8. func (id ID) IsEmpty() bool {
  9. return id == ""
  10. }
  11. // IsValid returns true if the ID is not empty and compatible with RFC 4122.
  12. func (id ID) IsValid() bool {
  13. return !id.IsEmpty() && uuid.MatchString(string(id))
  14. }
  15. // String implements built-in `fmt.Stringer` interface and returns the underlying string value.
  16. func (id ID) String() string {
  17. return string(id)
  18. }