-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
188 lines (163 loc) · 4.78 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"flag"
"fmt"
rejson "go-rejson"
"log"
"github.com/gomodule/redigo/redis"
)
var addr = flag.String("Server", "localhost:6379", "Redis server address")
// Name - student name
type Name struct {
First string `json:"first,omitempty"`
Middle string `json:"middle,omitempty"`
Last string `json:"last,omitempty"`
}
// Student - student object
type Student struct {
// CHECKPOINT -
// Info is an embedded pointer. This is where redigo's base
// library faces the issue.
Info *StudentDetails `json:"info,omitempty"`
Rank int `json:"rank,omitempty"`
}
type StudentDetails struct {
FirstName string
LastName string
Major string
}
func main() {
flag.Parse()
conn, err := redis.Dial("tcp", *addr)
if err != nil {
log.Fatalf("Failed to connect to redis-server @ %s", *addr)
return
}
student := Student{
Info: &StudentDetails{
FirstName: "John",
LastName: "Doe",
Major: "CSE",
},
Rank: 1,
}
// CHECKPOINT -
// Add the student object to the store as a HMSET.
err = addStructHash(conn, "JohnDoeHash", student)
if err != nil {
log.Fatalf("Failed to addStructHash - %s", err)
return
}
// CHECKPOINT -
// Add the student object to the store as a JSON.SET
err = addStructReJSON(conn, "JohnDoeJSON", student)
if err != nil {
log.Fatalf("Failed to addStructReJSON - %s", err)
return
}
// CHECKPOINT -
// Redigo stores embedded objects as MultiBulk entries, which are essentially
// just string objects. All key/value pairs are stored as strings.
// The only way we can read the stored Hash members is to use the HGETALL and return
// the values as a map[string]string.
outStudentMap, err := redis.StringMap(getStructHash(conn, "JohnDoeHash"))
if err != nil {
log.Fatalf("Failed to getStructHash - %s", err)
return
}
fmt.Printf("[HASH] Student Info %v [Type %T]\n", outStudentMap["Info"], outStudentMap["Info"])
// OUTPUT :
// [HASH] Student Info &{John Doe CSE} [Type string]
// =====================================
// 127.0.0.1:6379> HGET JohnDoeHash Info
// Info
// &{John Doe CSE}
// =====================================
outJSON, err := getStructReJSON(conn, "JohnDoeJSON")
if err != nil {
log.Fatalf("Failed to getStructReJSON - %s", err)
return
}
outStudent := &Student{}
err = json.Unmarshal(outJSON.([]byte), outStudent)
if err != nil {
log.Fatalf("Failed to JSON Unmarshal - %s", err)
return
}
fmt.Printf("[ReJSON] Student Info %v [Type %T]\n", outStudent.Info, outStudent.Info)
// OUTPUT :
// [ReJSON] Student Info &{John Doe CSE} [Type *main.StudentDetails]
// =====================================
// 127.0.0.1:6379> JSON.GET JohnDoeJSON INDENT "\t" NEWLINE "\n" SPACE " " .info
// {
// "FirstName": "John",
// "LastName": "Doe",
// "Major": "CSE"
// }
// =====================================
// CHECKPOINT :
// Alternatively we could still use Redigo HSET to store our object as a JSON string
err = addStructHashWithJSON(conn, "JohnDoeHashJSON", student)
if err != nil {
log.Fatalf("Failed to addStructHashWithJSON - %s", err)
return
}
outHashJSON, err := getStructHashWithJSON(conn, "JohnDoeHashJSON")
if err != nil {
log.Fatalf("Failed to getStructHashWithJSON - %s", err)
return
}
outHashJSONStudent := &Student{}
err = json.Unmarshal(outHashJSON.([]byte), outHashJSONStudent)
if err != nil {
log.Fatalf("Failed to JSON Unmarshal - %s", err)
return
}
fmt.Printf("[HSET JSON] Student Info %v [Type %T]\n", outHashJSONStudent.Info, outHashJSONStudent.Info)
// OUTPUT :
// [HSET JSON] Student Info &{John Doe CSE} [Type *main.StudentDetails]
// =====================================
// 127.0.0.1:6379> HGETALL JohnDoeHashJSON
// JSON
// {"info":{"FirstName":"John","LastName":"Doe","Major":"CSE"},"rank":1}
// =====================================
}
func addStructHash(conn redis.Conn, key string, value interface{}) (err error) {
_, err = conn.Do("HMSET", redis.Args{key}.AddFlat(value)...)
if err != nil {
return
}
return
}
func getStructHash(conn redis.Conn, key string) (value interface{}, err error) {
return conn.Do("HGETALL", "JohnDoeHash")
}
func addStructReJSON(conn redis.Conn, key string, value interface{}) (err error) {
_, err = rejson.JSONSet(conn, key, ".", value, false, false)
if err != nil {
return
}
return
}
func getStructReJSON(conn redis.Conn, key string) (value interface{}, err error) {
return rejson.JSONGet(conn, key, "")
}
func addStructHashWithJSON(conn redis.Conn, key string, value interface{}) (err error) {
b, err := json.Marshal(value)
if err != nil {
return
}
_, err = conn.Do("HSET", key, "JSON", string(b))
if err != nil {
return
}
return
}
func getStructHashWithJSON(conn redis.Conn, key string) (value interface{}, err error) {
value, err = conn.Do("HGET", key, "JSON")
if err != nil {
return
}
return
}