Skip to content

Commit df57c6c

Browse files
committed
Add quotes for fields containing spaces in CmdSerializer
1 parent 7bf9e18 commit df57c6c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

redisdump/redisdump.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,22 @@ func RESPSerializer(cmd []string) string {
6666

6767
// RedisCmdSerializer will serialize cmd to a string with redis commands
6868
func RedisCmdSerializer(cmd []string) string {
69-
return strings.Join(cmd, " ")
69+
if len(cmd) == 0 {
70+
return ""
71+
}
72+
res := ""
73+
for i, s := range cmd {
74+
if i>0 {
75+
res += " "
76+
}
77+
if strings.Contains(s, " ") {
78+
res += "\"" + s + "\""
79+
} else {
80+
res += s
81+
}
82+
}
83+
84+
return res
7085
}
7186

7287
func dumpKeys(client radix.Client, keys []string, withTTL bool, logger *log.Logger, serializer func([]string) string) error {

redisdump/redisdump_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestHashToRedisCmd(t *testing.T) {
7373
}
7474

7575
testCases := []testCase{
76-
{key: "Paris", value: map[string]string{"country": "France", "weather": "sunny"}, expected: []string{"HSET", "Paris", "country", "France", "weather", "sunny"}},
76+
{key: "Paris", value: map[string]string{"country": "France", "weather": "sunny", "poi": "Tour Eiffel"}, expected: []string{"HSET", "Paris", "country", "France", "weather", "sunny", "poi", "Tour Eiffel"}},
7777
}
7878

7979
for _, test := range testCases {
@@ -125,6 +125,26 @@ func TestRESPSerializer(t *testing.T) {
125125
}
126126
}
127127

128+
129+
func TestRedisCmdSerializer(t *testing.T) {
130+
type testCase struct {
131+
command []string
132+
expected string
133+
}
134+
135+
testCases := []testCase{
136+
{command: []string{"SET", "key name 1", "key value 1"}, expected: "SET \"key name 1\" \"key value 1\""},
137+
{command: []string{"HSET", "key1", "key value 1"}, expected: "HSET key1 \"key value 1\""},
138+
}
139+
140+
for _, test := range testCases {
141+
s := RedisCmdSerializer(test.command)
142+
if s != test.expected {
143+
t.Errorf("Failed serializing command to redis protocol: expected %s, got %s", test.expected, s)
144+
}
145+
}
146+
}
147+
128148
func TestParseKeyspaceInfo(t *testing.T) {
129149
keyspaceInfo := `# Keyspace
130150
db0:keys=2,expires=1,avg_ttl=1009946407050

0 commit comments

Comments
 (0)