Skip to content

Commit 3358f4a

Browse files
committed
1 parent 58363e5 commit 3358f4a

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

management/user.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type User struct {
6262
// True if the user's email is verified, false otherwise. If it is true then
6363
// the user will not receive a verification email, unless verify_email: true
6464
// was specified.
65-
EmailVerified *bool `json:"email_verified,omitempty"`
65+
EmailVerified *bool `json:"-"`
6666

6767
// If true, the user will receive a verification email after creation, even
6868
// if created with email_verified set to true. If false, the user will not
@@ -87,6 +87,58 @@ type User struct {
8787
Blocked *bool `json:"blocked,omitempty"`
8888
}
8989

90+
// UnmarshalJSON is a custom deserializer for the User type.
91+
//
92+
// We have to use a custom one due to possible inconsistencies in value types.
93+
func (u *User) UnmarshalJSON(b []byte) error {
94+
type user User
95+
type userAlias struct {
96+
*user
97+
RawEmailVerified interface{} `json:"email_verified,omitempty"`
98+
}
99+
100+
alias := &userAlias{(*user)(u), nil}
101+
102+
err := json.Unmarshal(b, alias)
103+
if err != nil {
104+
return err
105+
}
106+
107+
if alias.RawEmailVerified != nil {
108+
var emailVerified bool
109+
switch rawEmailVerified := alias.RawEmailVerified.(type) {
110+
case bool:
111+
emailVerified = rawEmailVerified
112+
case string:
113+
emailVerified, err = strconv.ParseBool(rawEmailVerified)
114+
if err != nil {
115+
return err
116+
}
117+
default:
118+
panic(reflect.TypeOf(rawEmailVerified))
119+
}
120+
alias.EmailVerified = &emailVerified
121+
}
122+
123+
return nil
124+
}
125+
126+
// MarshalJSON is a custom serializer for the User type.
127+
func (u *User) MarshalJSON() ([]byte, error) {
128+
type user User
129+
type userAlias struct {
130+
*user
131+
RawEmailVerified interface{} `json:"email_verified,omitempty"`
132+
}
133+
134+
alias := &userAlias{user: (*user)(u)}
135+
if u.EmailVerified != nil {
136+
alias.RawEmailVerified = u.EmailVerified
137+
}
138+
139+
return json.Marshal(alias)
140+
}
141+
90142
type UserIdentity struct {
91143
Connection *string `json:"connection,omitempty"`
92144
UserID *string `json:"-"`

0 commit comments

Comments
 (0)