-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
95 lines (77 loc) · 2.03 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package types
import (
"github.com/google/uuid"
"github.com/mitchellh/mapstructure"
"encoding/json"
"time"
)
var (
userPrivateFields []string = []string{
"email",
"created",
}
)
type User struct {
ID string `json:"id" db:"id"`
Email string `json:"email" db:"email"`
Nick string `json:"nick" db:"nick"`
Bio string `json:"bio" db:"bio"`
SubscriberCount int `json:"subscriber_count" db:"subscriber_count"`
SubscriptionCount int `json:"subscription_count" db:"subscription_count"`
PostCount int `json:"post_count" db:"post_count"`
Created int64 `json:"created" db:"created"`
Moderator bool `json:"moderator" db:"moderator"`
Admin bool `json:"admin" db:"admin"`
}
func (it *User) FromMap(data map[string]interface{}) (err error) {
var config mapstructure.DecoderConfig = mapstructure.DecoderConfig{
Metadata: nil,
TagName: "json",
Result: &it,
}
var decoder *mapstructure.Decoder
if decoder, err = mapstructure.NewDecoder(&config); err == nil {
err = decoder.Decode(data)
}
return
}
func (user User) Map() (data map[string]interface{}) {
var bytes []byte
bytes, _ = json.Marshal(user)
json.Unmarshal(bytes, &data)
return
}
func (user User) PublicMap() (data map[string]interface{}) {
data = user.Map()
var field string
for _, field = range userPrivateFields {
delete(data, field)
}
return
}
func (user User) JSON() (data []byte, err error) {
data, err = json.Marshal(user)
return
}
func NewUser(nick, bio, email string) (made User) {
made = User{
Nick: nick,
Email: email,
Bio: bio,
ID: uuid.New().String(),
Created: time.Now().Unix(),
}
return
}
func UserFromMap(data map[string]interface{}) (it User, err error) {
var config mapstructure.DecoderConfig = mapstructure.DecoderConfig{
Metadata: nil,
TagName: "json",
Result: &it,
}
var decoder *mapstructure.Decoder
if decoder, err = mapstructure.NewDecoder(&config); err == nil {
err = decoder.Decode(data)
}
return
}