-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
120 lines (104 loc) · 2.86 KB
/
map.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package grocery
import (
"encoding/json"
"sync"
)
// Map is used to represent maps stored in Redis. See CustomMapType and Store
// for more information.
type Map struct {
sync.Map
}
// CustomMapType must be embedded by any struct that is used as a model's field
// and should be represented as a map in Redis. The parent struct must also
// embed grocery.Map. As a motivating example, let's imagine we want to store
// projects in Redis, and each project should contain a map that stores user
// IDs along with each user's role for that project.
//
// type User struct {
// grocery.Base
// Name string `grocery:"name"`
//
// // Role is not stored in Redis because it is specific to each project
// // the user is part of.
// Role string `grocery:"-"`
// }
//
// type Project struct {
// grocery.Base
// Users *UserRolesMap `grocery:"users"`
// }
//
// type UserRolesMap struct {
// grocery.CustomMapType
// grocery.Map
// users map[string]*User
// }
//
// func (m *UserRolesMap) Setup() {
// // Setup gets called once the map's contents have already been loaded
// // from Redis. In this function, we have the opportunity to populate the
// // private m.users map with our own User structs, instead of using the
// // underlying grocery.Map (which is a sync.Map internally) to just
// // access user IDs.
//
// userIDs := make([]string, 0, m.Count())
//
// m.Range(func(key, value interface{}) bool {
// userIDs = append(userIDs, key.(string))
// return true
// })
//
// // Load users from Redis
// users := make([]User, len(userIDs))
// grocery.LoadAll(userIDs, &users)
//
// // Populate m.users
// m.users = make(map[string]*User)
//
// for i := 0; i < len(userIDs); i++ {
// // Use underlying map data to get user's role
// users[i].Role, _ = m.Load(userIDs[i])
//
// // Then, store retrieved user model in m.users
// m.users[userIDs[i]] = &users[i]
// }
// }
type CustomMapType interface {
// Called once the map's data is loaded from Redis, allowing us to populate
// any custom fields.
Setup()
}
func NewMap(data map[string]string) *Map {
m := &Map{Map: sync.Map{}}
for k, v := range data {
m.Store(k, v)
}
return m
}
// Count returns the number of items stored in the map.
func (m *Map) Count() int {
num := 0
m.Range(func(key, value any) bool {
num += 1
return true
})
return num
}
func (m *Map) MarshalJSON() ([]byte, error) {
res := make(map[string]string)
m.Range(func(key, value interface{}) bool {
res[key.(string)] = value.(string)
return true
})
return json.Marshal(res)
}
func (m *Map) UnmarshalJSON(data []byte) error {
res := make(map[string]string)
if err := json.Unmarshal(data, &res); err != nil {
return err
}
for k, v := range res {
m.Store(k, v)
}
return nil
}