Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1101: Refactor evalAPPEND to handle leading zeros in value #1107

Merged
merged 4 commits into from
Oct 16, 2024
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
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