-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode.go
50 lines (41 loc) · 952 Bytes
/
node.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
package lime
import (
"fmt"
"strings"
)
// Node represents an element of a network.
type Node struct {
Identity
// Instance represents the name of the instance used by the node to connect to the network.
Instance string
}
func (n Node) String() string {
if n == (Node{}) {
return ""
}
if n.Instance == "" {
return n.Identity.String()
}
return fmt.Sprintf("%v/%v", n.Identity, n.Instance)
}
func ParseNode(s string) Node {
var instance string
values := strings.Split(s, "/")
if len(values) > 1 {
instance = values[1]
}
identity := ParseIdentity(values[0])
return Node{identity, instance}
}
func (n Node) MarshalText() ([]byte, error) {
return []byte(n.String()), nil
}
func (n *Node) UnmarshalText(text []byte) error {
node := ParseNode(string(text))
*n = node
return nil
}
// IsComplete indicates if all Node fields has values.
func (n *Node) IsComplete() bool {
return n.Identity.IsComplete() && n.Instance != ""
}