Skip to content

Commit 62d3dd9

Browse files
authored
feat: Change user custom data field type from any to json.RawMessage to facilitate easier retrieval from db. (#109)
* feat: Type the custom data field on the user model as json.RawMessage so mongo doesn't unmarshal to bson, therefore allowing a custom type to be correctly added and retrieved from the db. Otherwise, you need to convert the bson to your custom struct manually. * fix: return bytes.Equal directly to please golint * feat: Type wrap json.RawMessage and add Marshal and Unmarshal methods to streamline implementation of custom data. * feat: Add CustomData type to client Data field.
1 parent 1a861ce commit 62d3dd9

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package storage
22

33
import (
4-
"reflect"
4+
"bytes"
55

66
"github.com/ory/fosite"
77
)
@@ -116,7 +116,7 @@ type Client struct {
116116
Published bool `bson:"published" json:"published" xml:"published"`
117117

118118
// Data enables stuffing in extra client data that can be typechecked at a later point.
119-
Data any `bson:"data" json:"data,omitempty" xml:"data,omitempty"`
119+
Data CustomData `bson:"data" json:"data,omitempty" xml:"data,omitempty"`
120120
}
121121

122122
// GetID returns the client's Client ID.
@@ -386,7 +386,7 @@ func (c *Client) Equal(x Client) bool {
386386
return false
387387
}
388388

389-
return reflect.DeepEqual(c.Data, x.Data)
389+
return bytes.Equal(c.Data, x.Data)
390390
}
391391

392392
// IsEmpty returns whether or not the client resource is an empty record.

custom_data.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package storage
2+
3+
import "encoding/json"
4+
5+
type CustomData json.RawMessage
6+
7+
func (c *CustomData) Marshal(v any) error {
8+
b, err := json.Marshal(v)
9+
if err != nil {
10+
return err
11+
}
12+
*c = b
13+
return nil
14+
}
15+
16+
func (c *CustomData) Unmarshal(v any) error {
17+
err := json.Unmarshal(*c, v)
18+
if err != nil {
19+
return err
20+
}
21+
return nil
22+
}

user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package storage
22

33
import (
44
// Standard Library Imports
5+
"bytes"
56
"context"
67
"fmt"
7-
"reflect"
88
"strings"
99

1010
// External Imports
@@ -82,8 +82,8 @@ type User struct {
8282
// MFAFactors contains the MFA Factor ID to the type of factor the user is signed up to.
8383
MFAFactors map[string]MultiFactorType `bson:"mfaFactors" json:"mfaFactors,omitempty" xml:"mfaFactors,omitempty"`
8484

85-
// Data enables stuffing in extra user data that can be typechecked at a later point.
86-
Data any `bson:"data" json:"data,omitempty" xml:"data,omitempty"`
85+
// Data enables stuffing in extra user data that can be unmarshalled at a later point.
86+
Data CustomData `bson:"data" json:"data,omitempty" xml:"data,omitempty"`
8787
}
8888

8989
// MultiFactorType specifies the types of authentication a user can enrol in;
@@ -253,7 +253,7 @@ func (u User) Equal(x User) bool {
253253
}
254254
}
255255

256-
return reflect.DeepEqual(u.Data, x.Data)
256+
return bytes.Equal(u.Data, x.Data)
257257
}
258258

259259
// IsEmpty returns true if the current user holds no data.

user_test.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,36 +1121,20 @@ func TestUser_Equal(t *testing.T) {
11211121
{
11221122
description: "custom data differences should report not equal",
11231123
x: User{
1124-
Data: struct {
1125-
birthday string
1126-
}{
1127-
birthday: "1999-01-01",
1128-
},
1124+
Data: CustomData(`{"birthday":"1999-01-01"}`),
11291125
},
11301126
y: User{
1131-
Data: struct {
1132-
birthday string
1133-
}{
1134-
birthday: "2000-01-01",
1135-
},
1127+
Data: CustomData(`{"birthday":"2001-01-01"}`),
11361128
},
11371129
expected: false,
11381130
},
11391131
{
11401132
description: "custom data should report equal",
11411133
x: User{
1142-
Data: struct {
1143-
birthday string
1144-
}{
1145-
birthday: "1999-01-01",
1146-
},
1134+
Data: CustomData(`{"birthday":"1999-01-01"}`),
11471135
},
11481136
y: User{
1149-
Data: struct {
1150-
birthday string
1151-
}{
1152-
birthday: "1999-01-01",
1153-
},
1137+
Data: CustomData(`{"birthday":"1999-01-01"}`),
11541138
},
11551139
expected: true,
11561140
},

0 commit comments

Comments
 (0)