-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhmap.go
60 lines (54 loc) · 1.92 KB
/
hmap.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
package redis
import (
"context"
"encoding/json"
"github.com/Pantani/errors"
)
// GetAllHMObjects get all objects from a hash map table.
// It returns the objects and an error if occurs.
func (db *Redis) GetAllHMObjects(ctx context.Context, entity string) (map[string]string, error) {
cmd := db.client.HGetAll(ctx, entity)
if err := cmd.Err(); err != nil {
return nil, errors.E("not found", err, errors.Params{"entity": entity})
}
return cmd.Val(), nil
}
// GetHMObject get an object from a hash map table.
// It returns an error if occurs.
func (db *Redis) GetHMObject(ctx context.Context, entity, key string, value interface{}) error {
cmd := db.client.HMGet(ctx, entity, key)
if err := cmd.Err(); err != nil {
return errors.E("not found", err, errors.Params{"entity": entity, "key": key})
}
val, ok := cmd.Val()[0].(string)
if !ok {
return errors.E("not found", errors.Params{"entity": entity, "key": key, "value": val})
}
err := json.Unmarshal([]byte(val), value)
if err != nil {
return errors.E("fail to unmarshal value", err, errors.Params{"entity": entity, "key": key, "value": val})
}
return nil
}
// AddHMObject add an object to a hash map table.
// It returns an error if occurs.
func (db *Redis) AddHMObject(ctx context.Context, entity, key string, value interface{}) error {
j, err := json.Marshal(value)
if err != nil {
return errors.E(err, errors.Params{"key": key})
}
cmd := db.client.HMSet(ctx, entity, map[string]interface{}{key: j})
if err := cmd.Err(); err != nil {
return errors.E("not stored", err, errors.Params{"entity": entity, "key": key})
}
return nil
}
// DeleteHMObject delete an object from a hash map table.
// It returns an error if occurs.
func (db *Redis) DeleteHMObject(ctx context.Context, entity, key string) error {
cmd := db.client.HDel(ctx, entity, key)
if err := cmd.Err(); err != nil {
return errors.E("not deleted", err, errors.Params{"entity": entity, "key": key})
}
return nil
}