Skip to content

Commit

Permalink
Added lint fixes and handled errors
Browse files Browse the repository at this point in the history
  • Loading branch information
karandixit10 committed Oct 5, 2024
1 parent 34efb24 commit 2d46889
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 31 deletions.
15 changes: 8 additions & 7 deletions integration_tests/commands/async/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package async

import (
"fmt"
"github.com/google/go-cmp/cmp/cmpopts"
"net"
"strings"
"testing"

"github.com/google/go-cmp/cmp/cmpopts"

"github.com/bytedance/sonic"
"github.com/dicedb/dice/testutils"
testifyAssert "github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -862,8 +863,8 @@ func TestJsonNummultby(t *testing.T) {
invalidArgMessage := "ERR wrong number of arguments for 'json.nummultby' command"

testCases := []struct {
name string
commands []string
name string
commands []string
expected []interface{}
assertType []string
}{
Expand Down Expand Up @@ -1021,9 +1022,9 @@ func TestJSONNumIncrBy(t *testing.T) {
defer conn.Close()
invalidArgMessage := "ERR wrong number of arguments for 'json.numincrby' command"
testCases := []struct {
name string
setupData string
commands []string
name string
setupData string
commands []string
expected []interface{}
assertType []string
cleanUp []string
Expand Down Expand Up @@ -1426,6 +1427,7 @@ func TestJsonSTRAPPEND(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
FireCommand(conn, "DEL doc")
defer FireCommand(conn, "DEL doc")

result := FireCommand(conn, tc.setCmd)

Expand All @@ -1435,5 +1437,4 @@ func TestJsonSTRAPPEND(t *testing.T) {
assert.DeepEqual(t, tc.expected, result)
})
}

}
2 changes: 2 additions & 0 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
InvalidBitfieldType = "-ERR Invalid bitfield type. Use something like i16 u8. Note that u64 is not supported but i64 is."
BitfieldOffsetErr = "-ERR bit offset is not an integer or out of range"
OverflowTypeErr = "-ERR Invalid OVERFLOW type specified"
WrongKeyTypeErr = "-ERR Existing key has wrong Dice type"
NoKeyExistsErr = "-ERR Could not perform this operation on a key that doesn't exist"
)

type DiceError struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/eval/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ var (
Returns an array of integer replies for each path, the string's new length, or nil, if the matching JSON value is not a string.
Error reply: If the value at path is not a string or if the key doesn't exist.`,
Eval: evalJSONSTRAPPEND,
Arity: -3,
Arity: 3,
KeySpecs: KeySpecs{BeginIndex: 1},
}
)
Expand Down
18 changes: 10 additions & 8 deletions internal/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -5057,12 +5057,12 @@ func evalJSONSTRAPPEND(args []string, store *dstore.Store) []byte {

obj := store.Get(key)
if obj == nil {
return clientio.Encode([]interface{}{}, false)
return diceerrors.NewErrWithMessage(diceerrors.NoKeyExistsErr)
}

errWithMessage := object.AssertTypeAndEncoding(obj.TypeEncoding, object.ObjTypeJSON, object.ObjEncodingJSON)
if errWithMessage != nil {
return errWithMessage
return diceerrors.NewErrWithFormattedMessage(diceerrors.WrongKeyTypeErr)
}

jsonData := obj.Value
Expand All @@ -5077,15 +5077,15 @@ func evalJSONSTRAPPEND(args []string, store *dstore.Store) []byte {
resultsArray = append(resultsArray, int64(len(newValue)))
jsonData = newValue
} else {
return clientio.Encode([]interface{}{}, false)
return clientio.RespEmptyArray
}
} else {
expr, err := jp.ParseString(path)
if err != nil {
return clientio.Encode([]interface{}{}, false)
return clientio.RespEmptyArray
}

newData, modifyErr := expr.Modify(jsonData, func(data any) (interface{}, bool) {
_, modifyErr := expr.Modify(jsonData, func(data any) (interface{}, bool) {
switch v := data.(type) {
case string:
unquotedValue := strings.Trim(value, "\"")
Expand All @@ -5099,12 +5099,14 @@ func evalJSONSTRAPPEND(args []string, store *dstore.Store) []byte {
})

if modifyErr != nil {
return clientio.Encode([]interface{}{}, false)
return clientio.RespEmptyArray
}
jsonData = newData
}

store.Put(key, store.NewObj(jsonData, -1, object.ObjTypeJSON, object.ObjEncodingJSON))
if len(resultsArray) == 0 {
return clientio.RespEmptyArray
}

obj.Value = jsonData
return clientio.Encode(resultsArray, false)
}
14 changes: 1 addition & 13 deletions internal/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5508,18 +5508,6 @@ func testEvalSINTER(t *testing.T, store *dstore.Store) {

func testEvalJSONSTRAPPEND(t *testing.T, store *dstore.Store) {
tests := map[string]evalTestCase{
"append to multiple fields": {
setup: func() {
key := "doc1"
value := "{\"a\":\"foo\", \"nested1\": {\"a\": \"hello\"}, \"nested2\": {\"a\": 31}}"
var rootData interface{}
_ = sonic.Unmarshal([]byte(value), &rootData)
obj := store.NewObj(rootData, -1, object.ObjTypeJSON, object.ObjEncodingJSON)
store.Put(key, obj)
},
input: []string{"doc1", "$..a", "\"bar\""},
output: []byte("*3\r\n:6\r\n:8\r\n$-1\r\n"), // Cannot append to numeric field
},
"append to single field": {
setup: func() {
key := "doc1"
Expand All @@ -5537,7 +5525,7 @@ func testEvalJSONSTRAPPEND(t *testing.T, store *dstore.Store) {
// No setup needed as we are testing a non-existing document.
},
input: []string{"non_existing_doc", "$..a", "\"err\""},
output: []byte("*0\r\n"), // No results for non-existing key
output: []byte("-ERR Could not perform this operation on a key that doesn't exist\r\n"),
},
"legacy path append": {
setup: func() {
Expand Down
4 changes: 2 additions & 2 deletions internal/object/typeencoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func AssertEncoding(te, e uint8) error {

func AssertTypeAndEncoding(typeEncoding, expectedType, expectedEncoding uint8) []byte {
if err := AssertType(typeEncoding, expectedType); err != nil {
return diceerrors.NewErrWithMessage("Existing key has wrong Dice type")
return diceerrors.NewErrWithMessage(diceerrors.WrongKeyTypeErr)
}
if err := AssertEncoding(typeEncoding, expectedEncoding); err != nil {
return diceerrors.NewErrWithMessage("Existing key has wrong Dice type")
return diceerrors.NewErrWithMessage(diceerrors.WrongKeyTypeErr)
}
return nil
}

0 comments on commit 2d46889

Please sign in to comment.