github.com/simpleiot/simpleiot@v0.18.3/data/user.go (about)

     1  package data
     2  
     3  import "time"
     4  
     5  // User represents a user of the system
     6  type User struct {
     7  	ID        string `json:"id"`
     8  	FirstName string `json:"firstName"`
     9  	LastName  string `json:"lastName"`
    10  	Phone     string `json:"phone"`
    11  	Email     string `json:"email"`
    12  	Pass      string `json:"pass"`
    13  }
    14  
    15  // ToPoints converts a user structure into points
    16  func (u *User) ToPoints() Points {
    17  	now := time.Now()
    18  	return Points{
    19  		{Type: PointTypeFirstName, Time: now, Text: u.FirstName},
    20  		{Type: PointTypeLastName, Time: now, Text: u.LastName},
    21  		{Type: PointTypePhone, Time: now, Text: u.Phone},
    22  		{Type: PointTypeEmail, Time: now, Text: u.Email},
    23  		{Type: PointTypePass, Time: now, Text: u.Pass},
    24  	}
    25  }
    26  
    27  // ToNode converts a user structure into a node
    28  func (u *User) ToNode() Node {
    29  	return Node{
    30  		Type:   NodeTypeUser,
    31  		Points: u.ToPoints(),
    32  	}
    33  }
    34  
    35  // NodeToUser converts a node to a user
    36  func NodeToUser(node Node) (User, error) {
    37  	ret := User{}
    38  	ret.ID = node.ID
    39  	for _, p := range node.Points {
    40  		switch p.Type {
    41  		case PointTypeFirstName:
    42  			ret.FirstName = p.Text
    43  		case PointTypeLastName:
    44  			ret.LastName = p.Text
    45  		case PointTypeEmail:
    46  			ret.Email = p.Text
    47  		case PointTypePhone:
    48  			ret.Phone = p.Text
    49  		case PointTypePass:
    50  			ret.Pass = p.Text
    51  		}
    52  	}
    53  
    54  	return ret, nil
    55  }