PageRenderTime 72ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/internal/domain/id.go

https://bitbucket.org/kamilsk/click
Go | 24 lines | 13 code | 6 blank | 5 comment | 3 complexity | 60b33db281156013c8294b9653c5add6 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 domain
  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 (s ID) IsEmpty() bool {
  9. return s == ""
  10. }
  11. // IsValid returns true if the ID is not empty and compatible with RFC 4122.
  12. func (s ID) IsValid() bool {
  13. return !(s == "") && uuid.MatchString(string(s)) // IsEmpty and String were inlined manually
  14. }
  15. // String implements built-in `fmt.Stringer` interface and returns the underlying string value.
  16. func (s ID) String() string {
  17. return string(s)
  18. }