Skip to content

Commit 363c9f9

Browse files
Merge pull request #260 from codecrafters-io/optimistic-locking-bugfix
Formatting Bugfix: `DataTypeAssertion`
2 parents 8985e5c + 5c14378 commit 363c9f9

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ toolchain go1.24.2
77
require (
88
github.com/codecrafters-io/tester-utils v0.4.15
99
github.com/dustin/go-humanize v1.0.1
10+
github.com/fatih/color v1.18.0
1011
github.com/hdt3213/rdb v1.2.0
1112
github.com/stretchr/testify v1.10.0
1213
github.com/tidwall/pretty v1.2.1
@@ -16,7 +17,6 @@ require (
1617
require (
1718
github.com/creack/pty v1.1.24 // indirect
1819
github.com/davecgh/go-spew v1.1.1 // indirect
19-
github.com/fatih/color v1.18.0 // indirect
2020
github.com/mattn/go-colorable v0.1.14 // indirect
2121
github.com/mattn/go-isatty v0.0.20 // indirect
2222
github.com/mitchellh/go-testing-interface v1.14.1 // indirect

internal/resp_assertions/data_type_assertion.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,41 @@ type DataTypeAssertion struct {
1111
}
1212

1313
func (a DataTypeAssertion) Run(value resp_value.Value) error {
14-
dataTypeHint := ""
14+
if value.Type == a.ExpectedType {
15+
return nil
16+
}
1517

16-
switch a.ExpectedType {
18+
expectedDataTypeHint := a.getDataTypeHint(a.ExpectedType)
19+
receivedDataTypeHint := a.getDataTypeHint(value.Type)
20+
21+
// Spacing
22+
if expectedDataTypeHint != "" {
23+
expectedDataTypeHint = fmt.Sprintf(" (%s)", expectedDataTypeHint)
24+
}
25+
26+
if receivedDataTypeHint != "" {
27+
receivedDataTypeHint = fmt.Sprintf(" (%s)", receivedDataTypeHint)
28+
}
29+
30+
return fmt.Errorf(
31+
"Expected %s%s, found %s%s",
32+
a.ExpectedType, expectedDataTypeHint,
33+
value.Type, receivedDataTypeHint,
34+
)
35+
}
36+
37+
func (a DataTypeAssertion) getDataTypeHint(dataType string) string {
38+
dataTypeHint := ""
39+
switch dataType {
1740
case resp_value.NIL:
1841
dataTypeHint = "$-1\r\n"
1942
case resp_value.NIL_ARRAY:
2043
dataTypeHint = "*-1\r\n"
2144
}
2245

23-
// Spacing
2446
if dataTypeHint != "" {
25-
dataTypeHint = " " + dataTypeHint
47+
dataTypeHint = FormatWithoutQuotes(dataTypeHint)
2648
}
2749

28-
if value.Type != a.ExpectedType {
29-
return fmt.Errorf("Expected %s%s, found %s", a.ExpectedType, dataTypeHint, value.Type)
30-
}
31-
return nil
50+
return dataTypeHint
3251
}

internal/resp_assertions/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package resp_assertions
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/fatih/color"
58
)
69

@@ -17,3 +20,8 @@ func BuildExpectedVsReceivedErrorMessage(expectedValue string, receivedValue str
1720
errorMsg += " \"" + receivedValue + "\""
1821
return errorMsg
1922
}
23+
24+
func FormatWithoutQuotes(input string) string {
25+
formattedString := fmt.Sprintf("%q", input)
26+
return strings.Trim(formattedString, "\"")
27+
}

0 commit comments

Comments
 (0)