Skip to content

Commit 806dbdf

Browse files
committed
api: added constructors for operations
Closes #TNTP-4540.
1 parent ed91725 commit 806dbdf

File tree

3 files changed

+177
-8
lines changed

3 files changed

+177
-8
lines changed

operation/operation.go

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,65 @@ package operation
55
// Operation represents a storage operation to be executed.
66
// This is used within transactions and other operation contexts.
77
type Operation struct {
8-
// Type specifies the operation type (Get, Put, Delete).
9-
Type Type
10-
// Key is the target key for the operation.
11-
Key []byte
12-
// Value contains the data for put operations, nil for get/delete.
13-
Value []byte
14-
// Options contains additional operation configuration.
15-
Options []Option
8+
// tp specifies the operation type (Get, Put, Delete).
9+
tp Type
10+
// key is the target key for the operation.
11+
key []byte
12+
// value contains the data for put operations, nil for get/delete.
13+
value []byte
14+
// options contains additional operation configuration.
15+
options []Option
16+
}
17+
18+
// Type returns the operation type (Get, Put, or Delete).
19+
func (o Operation) Type() Type {
20+
return o.tp
21+
}
22+
23+
// Key returns the key associated with the operation.
24+
func (o Operation) Key() []byte {
25+
return o.key
26+
}
27+
28+
// Value returns the value associated with the operation.
29+
func (o Operation) Value() []byte {
30+
return o.value
31+
}
32+
33+
// Options returns the configuration options for the operation.
34+
func (o Operation) Options() []Option {
35+
return o.options
36+
}
37+
38+
// Get creates a new read operation for the specified key.
39+
// Returns an Operation configured for reading data from storage.
40+
func Get(key []byte, options ...Option) Operation {
41+
return Operation{
42+
tp: TypeGet,
43+
key: key,
44+
value: nil,
45+
options: options,
46+
}
47+
}
48+
49+
// Put creates a new write operation for the specified key-value pair.
50+
// Returns an Operation configured for writing data to storage.
51+
func Put(key, value []byte, options ...Option) Operation {
52+
return Operation{
53+
tp: TypePut,
54+
key: key,
55+
value: value,
56+
options: options,
57+
}
58+
}
59+
60+
// Delete creates a new delete operation for the specified key.
61+
// Returns an Operation configured for removing data from storage.
62+
func Delete(key []byte, options ...Option) Operation {
63+
return Operation{
64+
tp: TypeDelete,
65+
key: key,
66+
value: nil,
67+
options: options,
68+
}
1669
}

operation/operation_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package operation_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/tarantool/go-storage/operation"
9+
)
10+
11+
func TestGet(t *testing.T) {
12+
t.Parallel()
13+
14+
key := []byte("test-key")
15+
op := operation.Get(key)
16+
17+
assert.Equal(t, operation.TypeGet, op.Type())
18+
assert.Equal(t, key, op.Key())
19+
assert.Nil(t, op.Value())
20+
assert.Empty(t, op.Options())
21+
}
22+
23+
func TestGetWithOptions(t *testing.T) {
24+
t.Parallel()
25+
26+
key := []byte("test-key")
27+
op := operation.Get(key, operation.Option{}, operation.Option{})
28+
29+
assert.Equal(t, operation.TypeGet, op.Type())
30+
assert.Equal(t, key, op.Key())
31+
assert.Nil(t, op.Value())
32+
assert.Len(t, op.Options(), 2)
33+
}
34+
35+
func TestPut(t *testing.T) {
36+
t.Parallel()
37+
38+
key := []byte("test-key")
39+
value := []byte("test-value")
40+
op := operation.Put(key, value)
41+
42+
assert.Equal(t, operation.TypePut, op.Type())
43+
assert.Equal(t, key, op.Key())
44+
assert.Equal(t, value, op.Value())
45+
assert.Empty(t, op.Options())
46+
}
47+
48+
func TestPutWithOptions(t *testing.T) {
49+
t.Parallel()
50+
51+
key := []byte("test-key")
52+
value := []byte("test-value")
53+
op := operation.Put(key, value, operation.Option{}, operation.Option{})
54+
55+
assert.Equal(t, operation.TypePut, op.Type())
56+
assert.Equal(t, key, op.Key())
57+
assert.Equal(t, value, op.Value())
58+
assert.Len(t, op.Options(), 2)
59+
}
60+
61+
func TestDelete(t *testing.T) {
62+
t.Parallel()
63+
64+
key := []byte("test-key")
65+
op := operation.Delete(key)
66+
67+
assert.Equal(t, operation.TypeDelete, op.Type())
68+
assert.Equal(t, key, op.Key())
69+
assert.Nil(t, op.Value())
70+
assert.Empty(t, op.Options())
71+
}
72+
73+
func TestDeleteWithOptions(t *testing.T) {
74+
t.Parallel()
75+
76+
key := []byte("test-key")
77+
op := operation.Delete(key, operation.Option{}, operation.Option{})
78+
79+
assert.Equal(t, operation.TypeDelete, op.Type())
80+
assert.Equal(t, key, op.Key())
81+
assert.Nil(t, op.Value())
82+
assert.Len(t, op.Options(), 2)
83+
}

operation/type_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package operation_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/tarantool/go-storage/operation"
9+
)
10+
11+
func TestTypeString(t *testing.T) {
12+
t.Parallel()
13+
14+
tests := []struct {
15+
name string
16+
typ operation.Type
17+
expected string
18+
}{
19+
{"TypeGet", operation.TypeGet, "Get"},
20+
{"TypePut", operation.TypePut, "Put"},
21+
{"TypeDelete", operation.TypeDelete, "Delete"},
22+
{"UnknownType", operation.Type(99), "Unknown"},
23+
}
24+
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
t.Parallel()
28+
29+
result := tt.typ.String()
30+
assert.Equal(t, tt.expected, result)
31+
})
32+
}
33+
}

0 commit comments

Comments
 (0)