Skip to content

Commit

Permalink
feat: Add bytes field to Value struct for Ints and Nils
Browse files Browse the repository at this point in the history
The `Value` struct in the `value.go` file has been updated to include a new `bytes` field. This field is used to store the byte representation of the value. The `NewNilValue` function has also been updated to set the `bytes` field to `[]byte("$-1\r\n")`.
  • Loading branch information
ryan-gang committed Jun 12, 2024
1 parent 5f7be75 commit af0e0d9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion internal/resp/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func NewIntegerValue(i int) Value {
return Value{
Type: INTEGER,
integer: i,
bytes: []byte(fmt.Sprintf("%d", i)),
}
}

Expand Down Expand Up @@ -70,7 +71,8 @@ func NewArrayValue(arr []Value) Value {

func NewNilValue() Value {
return Value{
Type: NIL,
Type: NIL,
bytes: []byte("$-1\r\n"),
}
}

Expand Down
8 changes: 7 additions & 1 deletion internal/resp_assertions/ordered_array_assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ func (a OrderedArrayAssertion) Run(value resp_value.Value) error {
return fmt.Errorf("Expected element #%d to be a %s, got %s", i+1, expectedValue.Type, actualElement.Type)
}

if expectedValue.Bytes() == nil {
// This should never happen, but just in case
// This is the case for ArrayValues
return fmt.Errorf("CodeCrafters internal error. expectedValue Bytes of type: %s is nil", expectedValue.Type)
}

// ToDo: Equal or EqualFold ?
if !bytes.Equal(actualElement.Bytes(), expectedValue.Bytes()) {
return fmt.Errorf("Expected element #%d to be %q, got %q", i+1, expectedValue.FormattedString(), actualElement.FormattedString())
return fmt.Errorf("Expected element #%d to be %s, got %s", i+1, expectedValue.FormattedString(), actualElement.FormattedString())
}
}

Expand Down

0 comments on commit af0e0d9

Please sign in to comment.