-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
130 lines (103 loc) · 5.4 KB
/
model.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
// Landon Wainwright.
// Package keystore provides an in memory key/value store service library
package keystore
// Op is the operation type for the request to the data store
type Op uint
// Flags for the request operation type
const (
READ Op = 1 << iota // A request to read the value
WRITE Op = 1 << iota // A request to write a value
DELETE Op = 1 << iota // A request to delete the key and value
)
// Type allows the requester to specify the type of data it is expecting
// This allows the caller to either handle the value or the error directly without
// having to check the type. NONE can be specified meaning any type will be accepted
type Type uint
// Flags the data types
const (
BOOL Type = 1 << iota // Expecting a boolean
INT Type = 1 << iota // Expecting an int
FLOAT Type = 1 << iota // Expecting a float
STRING Type = 1 << iota // Expecting a string
ARRAY Type = 1 << iota // Expecting an array
MAP Type = 1 << iota // Expecting a map
NONE Type = 1 << iota // Expecting any type
)
// Request are the requests that will be sent over the channel
// for operations on the store, such as a read or write
type Request struct {
Op Op // The operation required
Key string // The key (used for all requests)
Value *ValueHolder // The request value (used for write requests only)
ResponseChannel chan *Response // The return channel
}
// Response will be returned for each Request containing the result of the operation.
// If the data type had been specified the value will be contained within the
// specific value type objects otherwise it will be placed in the arbitrary
// value field. On a write operation the value returned will be the existing value.
type Response struct {
Success bool // True if the operation was a success (Error may still be present for write and delete operations)
Error string // Will contain any errors
Value *ValueHolder // The response values
}
// ValueHolder wraps the value, but if no error the value will be of the type expected
type ValueHolder struct {
Type Type // The data type expected (if read Op)
Val interface{} // The arbitrary value if NONE DataType specified
}
// KeyValueStore Defines the interface for a simple key value store type.
// It can be used by both the server and the clients to provide a uniform
// API that only differs by the transport method
type KeyValueStore interface {
// GetValueType This is used to attempt to parse the value into the interface value provided
// See https://golang.org/pkg/encoding/json/#Unmarshal for more information
GetValueType(key string, v interface{}) error
// GetValue will return the raw value that has been stored
GetValue(key string) (interface{}, error)
// GetBool returns a bool for the key specified or if the key does not exist
// or the value is not of the correct type an error is returned
GetBool(key string) (interface{}, error)
// GetInt returns an int for the key specified or if the key does not exist
// or the value is not of the correct type an error is returned
GetInt(key string) (interface{}, error)
// GetFloat returns an float for the key specified or if the key does not exist
// or the value is not of the correct type an error is returned
GetFloat(key string) (interface{}, error)
// GetString returns a string for the key specified or if the key does not exist
// or the value is not of the correct type an error is returned
GetString(key string) (interface{}, error)
// GetArray returns an array type for the key specified or if the key does not exist
// or the value is not of the correct type an error is returned
GetArray(key string) (interface{}, error)
// GetMap returns a map type for the key specified or if the key does not exist
// or the value is not of the correct type an error is returned
GetMap(key string) (interface{}, error)
// SetValue will store an arbitrary type value for the key specified
SetValue(key string, value interface{}) error
// SetBool will store an boolean type value for the key specified
SetBool(key string, value interface{}) error
// SetInt will store an int type value for the key specified
SetInt(key string, value interface{}) error
// SetFloat will store a float type value for the key specified
SetFloat(key string, value interface{}) error
// SetString will store a string type value for the key specified
SetString(key string, value interface{}) error
// SetArray will store an array type value for the key specified
SetArray(key string, value interface{}) error
// SetMap will store a map type value for the key specified
SetMap(key string, value interface{}) error
// DeleteKey will delete the key and value from the store
DeleteKey(key string)
}
// NewReadRequest will generate a new Request for reading a key
func NewReadRequest(key string, dType Type) *Request {
return &Request{Op: READ, Key: key, Value: &ValueHolder{Type: dType}, ResponseChannel: make(chan *Response)}
}
// NewWriteRequest will generate a new Request for reading a key
func NewWriteRequest(key string, dType Type, value interface{}) *Request {
return &Request{Op: WRITE, Key: key, Value: &ValueHolder{Type: dType, Val: value}, ResponseChannel: make(chan *Response)}
}
// NewDeleteRequest will generate a new Request for reading a key
func NewDeleteRequest(key string) *Request {
return &Request{Op: DELETE, Key: key, Value: &ValueHolder{Type: NONE}, ResponseChannel: make(chan *Response)}
}