Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 5 additions & 1 deletion internal/resp/decoder/decode_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func decodeArray(reader *bytes.Reader) (resp_value.Value, error) {
}
}

if length < 0 {
if length == -1 {
return resp_value.NewNilValue(), nil
}

if length < -1 {
// Ensure error points to the correct byte
reader.Seek(int64(offsetBeforeLength), io.SeekStart)

Expand Down
102 changes: 102 additions & 0 deletions internal/resp_assertions/xread_response_assertion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package resp_assertions

import (
"encoding/json"
"fmt"
"reflect"

resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value"
)

type StreamEntry struct {
Id string
FieldValuePairs [][]string
}

type StreamResponse struct {
Key string
Entries []StreamEntry
}

type XReadResponseAssertion struct {
ExpectedStreamResponses []StreamResponse
}

func NewXReadResponseAssertion(expectedValue []StreamResponse) RESPAssertion {
return XReadResponseAssertion{ExpectedStreamResponses: expectedValue}
}

func (a XReadResponseAssertion) Run(value resp_value.Value) error {
if value.Type != resp_value.ARRAY {
return fmt.Errorf("Expected array, got %s", value.Type)
}

expected := a.normalizeExpected()
actual := normalizeActual(value)

if !reflect.DeepEqual(expected, actual) {
expectedJSON, err := json.MarshalIndent(expected, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal expected value: %w", err)
}
actualJSON, err := json.MarshalIndent(actual, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal actual value: %w", err)
}
return fmt.Errorf("XREAD response mismatch:\nExpected:\n%s\nGot:\n%s", expectedJSON, actualJSON)
}

return nil
}

func (a XReadResponseAssertion) normalizeExpected() []interface{} {
result := make([]interface{}, len(a.ExpectedStreamResponses))

for i, stream := range a.ExpectedStreamResponses {
entries := make([][]interface{}, 0, len(stream.Entries))
for _, entry := range stream.Entries {
flatPairs := make([]string, 0, len(entry.FieldValuePairs)*2)
for _, pair := range entry.FieldValuePairs {
flatPairs = append(flatPairs, pair[0], pair[1])
}
entries = append(entries, []interface{}{entry.Id, flatPairs})
}
result[i] = []interface{}{stream.Key, entries}
}

return result
}

func normalizeActual(v resp_value.Value) interface{} {
switch v.Type {
case resp_value.BULK_STRING:
return v.String()
case resp_value.ARRAY:
arr := v.Array()
result := make([]interface{}, len(arr))
for i, elem := range arr {
// Special handling for stream entries (second element of the outer array)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall but I don't understand why we need this - let's go over on today's call!

if i == 1 && len(arr) == 2 {
entries := elem.Array()
typedEntries := make([][]interface{}, len(entries))
for j, entry := range entries {
entryArr := entry.Array()
if len(entryArr) == 2 {
fvPairs := entryArr[1].Array()
strPairs := make([]string, len(fvPairs))
for k, pair := range fvPairs {
strPairs[k] = pair.String()
}
typedEntries[j] = []interface{}{normalizeActual(entryArr[0]), strPairs}
}
}
result[i] = typedEntries
continue
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for stream entry structure in normalizeActual

The current implementation assumes specific indices in the array structures without proper validation, which could lead to panics if the RESP data has an unexpected structure.

			if i == 1 && len(arr) == 2 {
				entries := elem.Array()
				typedEntries := make([][]interface{}, len(entries))
				for j, entry := range entries {
					entryArr := entry.Array()
+					if len(entryArr) != 2 {
+						// Handle unexpected array size with a default value or log a warning
+						typedEntries[j] = []interface{}{"invalid_entry", []string{}}
+						continue
+					}
					if len(entryArr) == 2 {
						fvPairs := entryArr[1].Array()
						strPairs := make([]string, len(fvPairs))
						for k, pair := range fvPairs {
							strPairs[k] = pair.String()
						}
						typedEntries[j] = []interface{}{normalizeActual(entryArr[0]), strPairs}
					}
				}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if i == 1 && len(arr) == 2 {
entries := elem.Array()
typedEntries := make([][]interface{}, len(entries))
for j, entry := range entries {
entryArr := entry.Array()
if len(entryArr) == 2 {
fvPairs := entryArr[1].Array()
strPairs := make([]string, len(fvPairs))
for k, pair := range fvPairs {
strPairs[k] = pair.String()
}
typedEntries[j] = []interface{}{normalizeActual(entryArr[0]), strPairs}
}
}
result[i] = typedEntries
continue
}
if i == 1 && len(arr) == 2 {
entries := elem.Array()
typedEntries := make([][]interface{}, len(entries))
for j, entry := range entries {
entryArr := entry.Array()
if len(entryArr) != 2 {
// Handle unexpected array size with a default value or log a warning
typedEntries[j] = []interface{}{"invalid_entry", []string{}}
continue
}
if len(entryArr) == 2 {
fvPairs := entryArr[1].Array()
strPairs := make([]string, len(fvPairs))
for k, pair := range fvPairs {
strPairs[k] = pair.String()
}
typedEntries[j] = []interface{}{normalizeActual(entryArr[0]), strPairs}
}
}
result[i] = typedEntries
continue
}

result[i] = normalizeActual(elem)
}
return result
default:
return v.String()
}
}
Loading
Loading