-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmap_get.go
94 lines (76 loc) · 1.76 KB
/
map_get.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
package goriak
import (
"errors"
riak "github.com/basho/riak-go-client"
)
type MapGetCommand struct {
c *Command
output interface{}
key string
builder *riak.FetchMapCommandBuilder
}
// Get retreives a Map from Riak.
// Get performs automatic conversion from Riak Maps to your Go datatype.
// See Set() for more information.
func (c *Command) Get(key string, output interface{}) *MapGetCommand {
builder := riak.NewFetchMapCommandBuilder().
WithBucket(c.bucket).
WithBucketType(c.bucketType).
WithKey(key)
return &MapGetCommand{
c: c,
output: output,
key: key,
builder: builder,
}
}
func (c *MapGetCommand) Run(session *Session) (*Result, error) {
middlewarer := &getMiddlewarer{
cmd: c,
}
return runMiddleware(middlewarer, c.c.runMiddleware, c.riakExec, session)
}
func (c *MapGetCommand) riakExec(session *Session) (*Result, error) {
cmd, err := c.builder.Build()
if err != nil {
return nil, err
}
err = session.riak.Execute(cmd)
if err != nil {
return nil, err
}
mapCommand := cmd.(*riak.FetchMapCommand)
if !mapCommand.Success() {
return nil, errors.New("Not successful")
}
if mapCommand.Response.IsNotFound {
return &Result{
NotFound: true,
}, errors.New("Not found")
}
req := requestData{
bucket: c.c.bucket,
bucketType: c.c.bucketType,
key: c.key,
}
err = decodeInterface(mapCommand.Response, c.output, req)
if err != nil {
return nil, err
}
return &Result{
Key: c.key,
Context: mapCommand.Response.Context,
}, nil
}
type getMiddlewarer struct {
cmd *MapGetCommand
}
func (c *getMiddlewarer) Key() string {
return c.cmd.key
}
func (c *getMiddlewarer) Bucket() string {
return c.cmd.c.bucket
}
func (c *getMiddlewarer) BucketType() string {
return c.cmd.c.bucketType
}