-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathidentity.go
58 lines (48 loc) · 1.17 KB
/
identity.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
47
48
49
50
51
52
53
54
55
56
57
58
package lime
import (
"fmt"
"strings"
)
// Identity represents a member of a domain.
type Identity struct {
// Name represents the Identity unique name on its domain.
Name string
// Domain represents the network domain name of the Identity.
Domain string
}
func (i Identity) String() string {
if i == (Identity{}) {
return ""
}
if i.Domain == "" {
return i.Name
}
return fmt.Sprintf("%v@%v", i.Name, i.Domain)
}
// ParseIdentity parses the string To a valid Identity.
func ParseIdentity(s string) Identity {
var name, domain string
values := strings.Split(s, "@")
if len(values) > 1 {
domain = values[1]
}
name = values[0]
return Identity{name, domain}
}
func (i Identity) MarshalText() ([]byte, error) {
return []byte(i.String()), nil
}
func (i *Identity) UnmarshalText(text []byte) error {
identity := ParseIdentity(string(text))
*i = identity
return nil
}
// ToNode creates a Node instance based on the identity, with an
// empty value for the instance property.
func (i Identity) ToNode() Node {
return Node{i, ""}
}
// IsComplete indicates if all Identity fields has values.
func (i *Identity) IsComplete() bool {
return i.Name != "" && i.Domain != ""
}