Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 61 additions & 8 deletions operation/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,65 @@ package operation
// Operation represents a storage operation to be executed.
// This is used within transactions and other operation contexts.
type Operation struct {
// Type specifies the operation type (Get, Put, Delete).
Type Type
// Key is the target key for the operation.
Key []byte
// Value contains the data for put operations, nil for get/delete.
Value []byte
// Options contains additional operation configuration.
Options []Option
// tp specifies the operation type (Get, Put, Delete).
tp Type
// key is the target key for the operation.
key []byte
// value contains the data for put operations, nil for get/delete.
value []byte
// options contains additional operation configuration.
options []Option
}

// Type returns the operation type (Get, Put, or Delete).
func (o Operation) Type() Type {
return o.tp
}

// Key returns the key associated with the operation.
func (o Operation) Key() []byte {
return o.key
}

// Value returns the value associated with the operation.
func (o Operation) Value() []byte {
return o.value
}

// Options returns the configuration options for the operation.
func (o Operation) Options() []Option {
return o.options
}

// Get creates a new read operation for the specified key.
// Returns an Operation configured for reading data from storage.
func Get(key []byte, options ...Option) Operation {
return Operation{
tp: TypeGet,
key: key,
value: nil,
options: options,
}
}

// Put creates a new write operation for the specified key-value pair.
// Returns an Operation configured for writing data to storage.
func Put(key, value []byte, options ...Option) Operation {
return Operation{
tp: TypePut,
key: key,
value: value,
options: options,
}
}

// Delete creates a new delete operation for the specified key.
// Returns an Operation configured for removing data from storage.
func Delete(key []byte, options ...Option) Operation {
return Operation{
tp: TypeDelete,
key: key,
value: nil,
options: options,
}
}
83 changes: 83 additions & 0 deletions operation/operation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package operation_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/tarantool/go-storage/operation"
)

func TestGet(t *testing.T) {
t.Parallel()

key := []byte("test-key")
op := operation.Get(key)

assert.Equal(t, operation.TypeGet, op.Type())
assert.Equal(t, key, op.Key())
assert.Nil(t, op.Value())
assert.Empty(t, op.Options())
}

func TestGetWithOptions(t *testing.T) {
t.Parallel()

key := []byte("test-key")
op := operation.Get(key, operation.Option{}, operation.Option{})

assert.Equal(t, operation.TypeGet, op.Type())
assert.Equal(t, key, op.Key())
assert.Nil(t, op.Value())
assert.Len(t, op.Options(), 2)
}

func TestPut(t *testing.T) {
t.Parallel()

key := []byte("test-key")
value := []byte("test-value")
op := operation.Put(key, value)

assert.Equal(t, operation.TypePut, op.Type())
assert.Equal(t, key, op.Key())
assert.Equal(t, value, op.Value())
assert.Empty(t, op.Options())
}

func TestPutWithOptions(t *testing.T) {
t.Parallel()

key := []byte("test-key")
value := []byte("test-value")
op := operation.Put(key, value, operation.Option{}, operation.Option{})

assert.Equal(t, operation.TypePut, op.Type())
assert.Equal(t, key, op.Key())
assert.Equal(t, value, op.Value())
assert.Len(t, op.Options(), 2)
}

func TestDelete(t *testing.T) {
t.Parallel()

key := []byte("test-key")
op := operation.Delete(key)

assert.Equal(t, operation.TypeDelete, op.Type())
assert.Equal(t, key, op.Key())
assert.Nil(t, op.Value())
assert.Empty(t, op.Options())
}

func TestDeleteWithOptions(t *testing.T) {
t.Parallel()

key := []byte("test-key")
op := operation.Delete(key, operation.Option{}, operation.Option{})

assert.Equal(t, operation.TypeDelete, op.Type())
assert.Equal(t, key, op.Key())
assert.Nil(t, op.Value())
assert.Len(t, op.Options(), 2)
}
33 changes: 33 additions & 0 deletions operation/type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package operation_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/tarantool/go-storage/operation"
)

func TestTypeString(t *testing.T) {
t.Parallel()

tests := []struct {
name string
typ operation.Type
expected string
}{
{"TypeGet", operation.TypeGet, "Get"},
{"TypePut", operation.TypePut, "Put"},
{"TypeDelete", operation.TypeDelete, "Delete"},
{"UnknownType", operation.Type(99), "Unknown"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

result := tt.typ.String()
assert.Equal(t, tt.expected, result)
})
}
}
Loading