/
user.go
55 lines (49 loc) · 1.24 KB
/
user.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
package data
import "time"
// User represents a user of the system
type User struct {
ID string `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Phone string `json:"phone"`
Email string `json:"email"`
Pass string `json:"pass"`
}
// ToPoints converts a user structure into points
func (u *User) ToPoints() Points {
now := time.Now()
return Points{
{Type: PointTypeFirstName, Time: now, Text: u.FirstName},
{Type: PointTypeLastName, Time: now, Text: u.LastName},
{Type: PointTypePhone, Time: now, Text: u.Phone},
{Type: PointTypeEmail, Time: now, Text: u.Email},
{Type: PointTypePass, Time: now, Text: u.Pass},
}
}
// ToNode converts a user structure into a node
func (u *User) ToNode() Node {
return Node{
Type: NodeTypeUser,
Points: u.ToPoints(),
}
}
// NodeToUser converts a node to a user
func NodeToUser(node Node) (User, error) {
ret := User{}
ret.ID = node.ID
for _, p := range node.Points {
switch p.Type {
case PointTypeFirstName:
ret.FirstName = p.Text
case PointTypeLastName:
ret.LastName = p.Text
case PointTypeEmail:
ret.Email = p.Text
case PointTypePhone:
ret.Phone = p.Text
case PointTypePass:
ret.Pass = p.Text
}
}
return ret, nil
}