-
Notifications
You must be signed in to change notification settings - Fork 7
/
person.go
41 lines (34 loc) · 914 Bytes
/
person.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
package commitinfo
import (
"fmt"
"github.com/lunarway/release-manager/internal/regexp"
"github.com/pkg/errors"
)
type PersonInfo struct {
Name string
Email string
}
func NewPersonInfo(name, email string) PersonInfo {
return PersonInfo{
Name: name,
Email: email,
}
}
func (i PersonInfo) String() string {
return fmt.Sprintf("%s <%s>", i.Name, i.Email)
}
func ParsePerson(personInfo string) (PersonInfo, error) {
matches := personInfoRegex.FindStringSubmatch(personInfo)
if matches == nil {
return PersonInfo{}, errors.Wrap(ErrNoMatch, fmt.Sprintf("string '%s' does not contain a person", personInfo))
}
return PersonInfo{
Name: matches[personInfoRegexLookup.Name],
Email: matches[personInfoRegexLookup.Email],
}, nil
}
var personInfoRegexLookup = struct {
Name int
Email int
}{}
var personInfoRegex = regexp.MustCompile(`^(?P<Name>.*) <(?P<Email>.*)>$`, &personInfoRegexLookup)