Skip to content

Commit

Permalink
refactor: migrate validator field to newvalidator
Browse files Browse the repository at this point in the history
  • Loading branch information
c-harish committed Dec 25, 2024
1 parent 1f27c40 commit 7940290
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 55 deletions.
2 changes: 1 addition & 1 deletion config/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func applyDefaultValuesFromTags(config *Config, fieldName string) error {

func validateWALConfig(sl validator.StructLevel) {
config := sl.Current().Interface().(Config)

// LogDir validation
if config.WAL.LogDir == "" {
sl.ReportError(config.WAL.LogDir, "LogDir", "LogDir", "required", "cannot be empty")
Expand Down
6 changes: 3 additions & 3 deletions internal/eval/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,9 @@ var (
Name: "JSON.ARRINDEX",
Info: `JSON.ARRINDEX key path value [start [stop]]
Search for the first occurrence of a JSON value in an array`,
NewEval: evalJSONARRINDEX,
Arity: -3,
KeySpecs: KeySpecs{BeginIndex: 1},
NewEval: evalJSONARRINDEX,
Arity: -3,
KeySpecs: KeySpecs{BeginIndex: 1},
IsMigrated: true,
}

Expand Down
55 changes: 14 additions & 41 deletions internal/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"github.com/dicedb/dice/internal/cmd"
"math"
"reflect"
"strconv"
Expand All @@ -33,6 +32,7 @@ import (
"github.com/bytedance/sonic"
"github.com/dicedb/dice/config"
"github.com/dicedb/dice/internal/clientio"
"github.com/dicedb/dice/internal/cmd"
diceerrors "github.com/dicedb/dice/internal/errors"
"github.com/dicedb/dice/internal/eval/sortedset"
"github.com/dicedb/dice/internal/object"
Expand All @@ -47,7 +47,6 @@ type evalTestCase struct {
setup func()
input []string
output []byte
validator func(output []byte)
newValidator func(output interface{})
migratedOutput EvalResponse
}
Expand Down Expand Up @@ -4367,11 +4366,7 @@ func runEvalTests(t *testing.T, tests map[string]evalTestCase, evalFunc func([]s

output := evalFunc(tc.input, store)

if tc.validator != nil {
tc.validator(output)
} else {
assert.Equal(t, string(tc.output), string(output))
}
assert.Equal(t, string(tc.output), string(output))
})
}
}
Expand All @@ -4386,7 +4381,6 @@ func runMigratedEvalTests(t *testing.T, tests map[string]evalTestCase, evalFunc
}

output := evalFunc(tc.input, store)

if tc.newValidator != nil {
if tc.migratedOutput.Error != nil {
tc.newValidator(tc.migratedOutput.Error)
Expand Down Expand Up @@ -5274,7 +5268,7 @@ func testEvalJSONARRPOP(t *testing.T, store *dstore.Store) {
},
input: []string{"MOCK_KEY", "$", "2"},
output: []byte(":0\r\n"),
validator: func(output []byte) {
newValidator: func(output interface{}) {
key := "MOCK_KEY"
obj := store.Get(key)
want := []interface{}{float64(0), float64(1), float64(3), float64(4), float64(5)}
Expand All @@ -5297,7 +5291,7 @@ func testEvalJSONARRPOP(t *testing.T, store *dstore.Store) {
},
input: []string{"MOCK_KEY", "$.b", "2"},
output: []byte("*1\r\n:2\r\n"),
validator: func(output []byte) {
newValidator: func(output interface{}) {
key := "MOCK_KEY"
path := "$.b"
obj := store.Get(key)
Expand All @@ -5319,30 +5313,7 @@ func testEvalJSONARRPOP(t *testing.T, store *dstore.Store) {
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store = setupTest(store)

if tt.setup != nil {
tt.setup()
}
response := evalJSONARRPOP(tt.input, store)

if tt.migratedOutput.Result != nil {
if slice, ok := tt.migratedOutput.Result.([]interface{}); ok {
assert.Equal(t, slice, response.Result)
} else {
assert.Equal(t, tt.migratedOutput.Result, response.Result)
}
}

if tt.migratedOutput.Error != nil {
assert.EqualError(t, response.Error, tt.migratedOutput.Error.Error())
} else {
assert.NoError(t, response.Error)
}
})
}
runMigratedEvalTests(t, tests, evalJSONARRPOP, store)
}

func testEvalTYPE(t *testing.T, store *dstore.Store) {
Expand Down Expand Up @@ -6747,11 +6718,12 @@ func testEvalAPPEND(t *testing.T, store *dstore.Store) {
},
input: []string{"key", "123"},
migratedOutput: EvalResponse{Result: 3, Error: nil},
validator: func(output []byte) {
newValidator: func(output interface{}) {
obj := store.Get("key")
oType := obj.Type
if oType != object.ObjTypeInt {
t.Errorf("unexpected encoding")
return
}
},
},
Expand Down Expand Up @@ -6802,11 +6774,12 @@ func testEvalAPPEND(t *testing.T, store *dstore.Store) {
},
input: []string{"key", "2"},
migratedOutput: EvalResponse{Result: 2, Error: nil},
validator: func(output []byte) {
newValidator: func(output interface{}) {
obj := store.Get("key")
oType := obj.Type
if oType != object.ObjTypeString {
t.Errorf("unexpected encoding")
return
}
},
},
Expand Down Expand Up @@ -9356,7 +9329,7 @@ func testEvalLRANGE(t *testing.T, store *dstore.Store) {
}

func testEvalJSONARRINDEX(t *testing.T, store *dstore.Store) {
normalArray := `[0,1,2,3,4,3]`
normalArray := `[0,1,2,3,4,3]`
tests := []evalTestCase{
{
name: "nil value",
Expand Down Expand Up @@ -9388,7 +9361,7 @@ func testEvalJSONARRINDEX(t *testing.T, store *dstore.Store) {
input: []string{"EXISTING_KEY", "$", "3", "abc"},
migratedOutput: EvalResponse{
Result: nil,
Error: errors.New("ERR Couldn't parse as integer"),
Error: errors.New("ERR Couldn't parse as integer"),
},
},
{
Expand All @@ -9403,7 +9376,7 @@ func testEvalJSONARRINDEX(t *testing.T, store *dstore.Store) {
input: []string{"EXISTING_KEY", "$", "3", "4", "abc"},
migratedOutput: EvalResponse{
Result: nil,
Error: errors.New("ERR Couldn't parse as integer"),
Error: errors.New("ERR Couldn't parse as integer"),
},
},
{
Expand All @@ -9418,7 +9391,7 @@ func testEvalJSONARRINDEX(t *testing.T, store *dstore.Store) {
input: []string{"EXISTING_KEY", "$", "4", "4", "5"},
migratedOutput: EvalResponse{
Result: []interface{}{4},
Error: nil,
Error: nil,
},
},
}
Expand All @@ -9440,4 +9413,4 @@ func testEvalJSONARRINDEX(t *testing.T, store *dstore.Store) {
}
})
}
}
}
20 changes: 10 additions & 10 deletions internal/eval/store_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -6979,9 +6979,9 @@ func evalJSONARRINDEX(args []string, store *dstore.Store) *EvalResponse {

adjustedStart, adjustedStop := adjustIndices(start, stop, length)

if adjustedStart == -1 {
arrIndexList = append(arrIndexList, -1)
continue
if adjustedStart == -1 {
arrIndexList = append(arrIndexList, -1)
continue
}

// Range [start, stop) : start is inclusive, stop is exclusive
Expand All @@ -7004,18 +7004,18 @@ func evalJSONARRINDEX(args []string, store *dstore.Store) *EvalResponse {
return makeEvalResult(arrIndexList)
}

// adjustIndices adjusts the start and stop indices for array traversal.
// It handles negative indices and ensures they are within the array bounds.
func adjustIndices(start, stop, length int) (adjustedStart, adjustedStop int) {
// adjustIndices adjusts the start and stop indices for array traversal.
// It handles negative indices and ensures they are within the array bounds.
func adjustIndices(start, stop, length int) (adjustedStart, adjustedStop int) {
if length == 0 {
return -1, -1
return -1, -1
}
if start < 0 {
start += length
start += length
}

if stop <= 0 {
stop += length
if stop <= 0 {
stop += length
}
if start < 0 {
start = 0
Expand Down

0 comments on commit 7940290

Please sign in to comment.