-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
44 lines (39 loc) · 835 Bytes
/
users.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
package auth
import (
"encoding/json"
"fmt"
"github.com/sauerbraten/waiter/pkg/protocol/role"
)
type User struct {
Name string `json:"name"`
PublicKey PublicKey `json:"public_key"`
Role role.ID `json:"-"`
}
func (u *User) MarshalJSON() ([]byte, error) {
proxy := struct {
User
Role string `json:"role"`
}{
User: *u,
Role: u.Role.String(),
}
return json.Marshal(proxy)
}
func (u *User) UnmarshalJSON(data []byte) error {
proxy := &struct {
Name string `json:"name"`
PublicKey PublicKey `json:"public_key"`
Role string `json:"role"`
}{}
err := json.Unmarshal(data, proxy)
if err != nil {
return err
}
u.Name = proxy.Name
u.PublicKey = proxy.PublicKey
u.Role = role.Parse(proxy.Role)
if u.Role == -1 {
return fmt.Errorf("invalid value for 'role'")
}
return nil
}