Skip to content

Commit

Permalink
#1101: Refactor evalAPPEND to handle leading zeros in value (#1107)
Browse files Browse the repository at this point in the history
  • Loading branch information
shashi-sah2003 authored Oct 16, 2024
1 parent b190803 commit a4dad48
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions integration_tests/commands/async/append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ func TestAPPEND(t *testing.T) {
},
expected: []interface{}{int64(0), int64(1), int64(2), "12", "OK", int64(2), "12"},
},
{
name: "APPEND with leading zeros",
commands: []string{
"DEL key",
"APPEND key 0043",
"GET key",
"APPEND key 0034",
"GET key",
},
expected: []interface{}{int64(0), int64(4), "0043", int64(8), "00430034"},
},
{
name: "APPEND with Various Data Types",
commands: []string{
Expand Down
9 changes: 9 additions & 0 deletions internal/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -4582,6 +4582,15 @@ func evalAPPEND(args []string, store *dstore.Store) []byte {

if obj == nil {
// Key does not exist path

// check if the value starts with '0' and has more than 1 character to handle leading zeros
if len(value) > 1 && value[0] == '0' {
// treat as string if has leading zeros
store.Put(key, store.NewObj(value, -1, object.ObjTypeString, object.ObjEncodingRaw))
return clientio.Encode(len(value), false)
}

// Deduce type and encoding based on the value if no leading zeros
oType, oEnc := deduceTypeEncoding(value)

var storedValue interface{}
Expand Down
17 changes: 17 additions & 0 deletions internal/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5171,6 +5171,23 @@ func testEvalAPPEND(t *testing.T, store *dstore.Store) {
input: []string{"bitKey", "val"},
output: diceerrors.NewErrWithFormattedMessage(diceerrors.WrongTypeErr),
},
"append value with leading zeros":{
setup: func() {
store.Del("key_with_leading_zeros")
},
input: []string{"key_with_leading_zeros", "0043"},
output: clientio.Encode(4, false), // The length of "0043" is 4
validator: func(output []byte){
obj := store.Get("key_with_leading_zeros")
_, enc := object.ExtractTypeEncoding(obj)
if enc != object.ObjEncodingRaw {
t.Errorf("expected encoding to be Raw for string with leading zeros")
}
if obj.Value.(string) != "0043" {
t.Errorf("expected value to be '0043', got %v", obj.Value)
}
},
},
}

runEvalTests(t, tests, evalAPPEND, store)
Expand Down

0 comments on commit a4dad48

Please sign in to comment.