Skip to content

Commit

Permalink
refactor: KeyValue -> KVPair
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Apr 9, 2024
1 parent 1d41d25 commit f7b1211
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 32 deletions.
6 changes: 3 additions & 3 deletions internal/command/mset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// https://redis.io/commands/mset
type MSet struct {
baseCmd
kvals []redka.KeyValue
kvals []redka.KVPair
}

func parseMSet(b baseCmd) (*MSet, error) {
Expand All @@ -18,9 +18,9 @@ func parseMSet(b baseCmd) (*MSet, error) {
return cmd, ErrInvalidArgNum(cmd.name)
}

cmd.kvals = make([]redka.KeyValue, len(cmd.args)/2)
cmd.kvals = make([]redka.KVPair, len(cmd.args)/2)
for i := 0; i < len(cmd.args); i += 2 {
cmd.kvals[i/2] = redka.KeyValue{
cmd.kvals[i/2] = redka.KVPair{
Key: string(cmd.args[i]),
Value: cmd.args[i+1],
}
Expand Down
4 changes: 2 additions & 2 deletions internal/command/mset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMSetParse(t *testing.T) {
{
name: "mset name alice",
args: buildArgs("mset", "name", "alice"),
want: MSet{kvals: []redka.KeyValue{{Key: "name", Value: []byte("alice")}}},
want: MSet{kvals: []redka.KVPair{{Key: "name", Value: []byte("alice")}}},
err: nil,
},
{
Expand All @@ -41,7 +41,7 @@ func TestMSetParse(t *testing.T) {
{
name: "mset name alice age 25",
args: buildArgs("mset", "name", "alice", "age", "25"),
want: MSet{kvals: []redka.KeyValue{
want: MSet{kvals: []redka.KVPair{
{Key: "name", Value: []byte("alice")},
{Key: "age", Value: []byte("25")}},
},
Expand Down
6 changes: 3 additions & 3 deletions internal/command/msetnx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// https://redis.io/commands/msetnx
type MSetNX struct {
baseCmd
kvals []redka.KeyValue
kvals []redka.KVPair
}

func parseMSetNX(b baseCmd) (*MSetNX, error) {
Expand All @@ -19,9 +19,9 @@ func parseMSetNX(b baseCmd) (*MSetNX, error) {
return cmd, ErrInvalidArgNum(cmd.name)
}

cmd.kvals = make([]redka.KeyValue, len(cmd.args)/2)
cmd.kvals = make([]redka.KVPair, len(cmd.args)/2)
for i := 0; i < len(cmd.args); i += 2 {
cmd.kvals[i/2] = redka.KeyValue{
cmd.kvals[i/2] = redka.KVPair{
Key: string(cmd.args[i]),
Value: cmd.args[i+1],
}
Expand Down
4 changes: 2 additions & 2 deletions internal/command/msetnx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMSetNXParse(t *testing.T) {
{
name: "msetnx name alice",
args: buildArgs("msetnx", "name", "alice"),
want: MSetNX{kvals: []redka.KeyValue{{Key: "name", Value: []byte("alice")}}},
want: MSetNX{kvals: []redka.KVPair{{Key: "name", Value: []byte("alice")}}},
err: nil,
},
{
Expand All @@ -41,7 +41,7 @@ func TestMSetNXParse(t *testing.T) {
{
name: "msetnx name alice age 25",
args: buildArgs("msetnx", "name", "alice", "age", "25"),
want: MSetNX{kvals: []redka.KeyValue{
want: MSetNX{kvals: []redka.KVPair{
{Key: "name", Value: []byte("alice")},
{Key: "age", Value: []byte("25")}},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func (v Value) IsEmpty() bool {
return len(v) == 0
}

// KeyValue represents a key-value pair.
type KeyValue struct {
// KVPair represents a key-value pair.
type KVPair struct {
Key string
Value any
}
Expand Down
4 changes: 2 additions & 2 deletions internal/rstring/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (d *DB) GetSet(key string, value any, ttl time.Duration) (core.Value, error
}

// SetMany sets the values of multiple keys.
func (d *DB) SetMany(kvals ...core.KeyValue) error {
func (d *DB) SetMany(kvals ...core.KVPair) error {
err := d.Update(func(tx *Tx) error {
return tx.SetMany(kvals...)
})
Expand All @@ -94,7 +94,7 @@ func (d *DB) SetMany(kvals ...core.KeyValue) error {

// SetManyNX sets the values of multiple keys,
// but only if none of them yet exist.
func (d *DB) SetManyNX(kvals ...core.KeyValue) (bool, error) {
func (d *DB) SetManyNX(kvals ...core.KVPair) (bool, error) {
var ok bool
err := d.Update(func(tx *Tx) error {
var err error
Expand Down
4 changes: 2 additions & 2 deletions internal/rstring/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (tx *Tx) GetSet(key string, value any, ttl time.Duration) (core.Value, erro
}

// SetMany sets the values of multiple keys.
func (tx *Tx) SetMany(kvals ...core.KeyValue) error {
func (tx *Tx) SetMany(kvals ...core.KVPair) error {
for _, kv := range kvals {
if !core.IsValueType(kv.Value) {
return core.ErrInvalidType
Expand All @@ -199,7 +199,7 @@ func (tx *Tx) SetMany(kvals ...core.KeyValue) error {

// SetManyNX sets the values of multiple keys,
// but only if none of them exist yet.
func (tx *Tx) SetManyNX(kvals ...core.KeyValue) (bool, error) {
func (tx *Tx) SetManyNX(kvals ...core.KVPair) (bool, error) {
for _, kv := range kvals {
if !core.IsValueType(kv.Value) {
return false, core.ErrInvalidType
Expand Down
8 changes: 4 additions & 4 deletions redka.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type Key = core.Key
// It can be converted to other scalar types.
type Value = core.Value

// KeyValue represents a key-value pair.
type KeyValue = core.KeyValue
// KVPair represents a key-value pair.
type KVPair = core.KVPair

// Keys is a key repository.
type Keys interface {
Expand All @@ -56,8 +56,8 @@ type Strings interface {
SetNotExists(key string, value any, ttl time.Duration) (bool, error)
SetExists(key string, value any, ttl time.Duration) (bool, error)
GetSet(key string, value any, ttl time.Duration) (Value, error)
SetMany(kvals ...KeyValue) error
SetManyNX(kvals ...KeyValue) (bool, error)
SetMany(kvals ...KVPair) error
SetManyNX(kvals ...KVPair) (bool, error)
Incr(key string, delta int) (int, error)
IncrFloat(key string, delta float64) (float64, error)
Delete(keys ...string) (int, error)
Expand Down
24 changes: 12 additions & 12 deletions redka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,8 @@ func TestStringSetMany(t *testing.T) {

t.Run("create", func(t *testing.T) {
err := db.SetMany(
redka.KeyValue{Key: "name", Value: "alice"},
redka.KeyValue{Key: "age", Value: 25},
redka.KVPair{Key: "name", Value: "alice"},
redka.KVPair{Key: "age", Value: 25},
)
testx.AssertNoErr(t, err)
name, _ := db.Get("name")
Expand All @@ -788,8 +788,8 @@ func TestStringSetMany(t *testing.T) {
_ = db.Set("name", "alice")
_ = db.Set("age", 25)
err := db.SetMany(
redka.KeyValue{Key: "name", Value: "bob"},
redka.KeyValue{Key: "age", Value: 50},
redka.KVPair{Key: "name", Value: "bob"},
redka.KVPair{Key: "age", Value: 50},
)
testx.AssertNoErr(t, err)
name, _ := db.Get("name")
Expand All @@ -799,8 +799,8 @@ func TestStringSetMany(t *testing.T) {
})
t.Run("invalid type", func(t *testing.T) {
err := db.SetMany(
redka.KeyValue{Key: "name", Value: "alice"},
redka.KeyValue{Key: "age", Value: struct{ Name string }{"alice"}},
redka.KVPair{Key: "name", Value: "alice"},
redka.KVPair{Key: "age", Value: struct{ Name string }{"alice"}},
)
testx.AssertErr(t, err, redka.ErrInvalidType)
})
Expand All @@ -815,8 +815,8 @@ func TestStringSetManyNX(t *testing.T) {

t.Run("create", func(t *testing.T) {
ok, err := db.SetManyNX(
redka.KeyValue{Key: "age", Value: 25},
redka.KeyValue{Key: "city", Value: "wonderland"},
redka.KVPair{Key: "age", Value: 25},
redka.KVPair{Key: "city", Value: "wonderland"},
)
testx.AssertNoErr(t, err)
testx.AssertEqual(t, ok, true)
Expand All @@ -829,8 +829,8 @@ func TestStringSetManyNX(t *testing.T) {
_ = db.Set("age", 25)
_ = db.Set("city", "wonderland")
ok, err := db.SetManyNX(
redka.KeyValue{Key: "age", Value: 50},
redka.KeyValue{Key: "city", Value: "wonderland"},
redka.KVPair{Key: "age", Value: 50},
redka.KVPair{Key: "city", Value: "wonderland"},
)
testx.AssertNoErr(t, err)
testx.AssertEqual(t, ok, false)
Expand All @@ -841,8 +841,8 @@ func TestStringSetManyNX(t *testing.T) {
})
t.Run("invalid type", func(t *testing.T) {
ok, err := db.SetManyNX(
redka.KeyValue{Key: "name", Value: "alice"},
redka.KeyValue{Key: "age", Value: struct{ Name string }{"alice"}},
redka.KVPair{Key: "name", Value: "alice"},
redka.KVPair{Key: "age", Value: struct{ Name string }{"alice"}},
)
testx.AssertErr(t, err, redka.ErrInvalidType)
testx.AssertEqual(t, ok, false)
Expand Down

0 comments on commit f7b1211

Please sign in to comment.