-
Notifications
You must be signed in to change notification settings - Fork 656
/
name.go
46 lines (36 loc) · 1.01 KB
/
name.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package userbus
import (
"fmt"
"regexp"
)
// Name represents a name in the system.
type Name struct {
name string
}
// String returns the value of the name.
func (n Name) String() string {
return n.name
}
// Equal provides support for the go-cmp package and testing.
func (n Name) Equal(n2 Name) bool {
return n.name == n2.name
}
// =============================================================================
var nameRegEx = regexp.MustCompile("^[a-zA-Z0-9' -]{3,20}$")
// ParseName parses the string value and returns a name if the value complies
// with the rules for a name.
func ParseName(value string) (Name, error) {
if !nameRegEx.MatchString(value) {
return Name{}, fmt.Errorf("invalid name %q", value)
}
return Name{value}, nil
}
// MustParseName parses the string value and returns a name if the value
// complies with the rules for a name. If an error occurs the function panics.
func MustParseName(value string) Name {
name, err := ParseName(value)
if err != nil {
panic(err)
}
return name
}