Skip to content

Commit a970fce

Browse files
committed
more processors
1 parent c64f0d9 commit a970fce

File tree

2 files changed

+70
-31
lines changed

2 files changed

+70
-31
lines changed

src/StackExchange.Redis/ResultProcessor.cs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,50 +1120,46 @@ private static bool TryParseRole(string? val, out bool isReplica)
11201120

11211121
private sealed class BooleanProcessor : ResultProcessor<bool>
11221122
{
1123-
protected override bool SetResultCore(PhysicalConnection connection, Message message, RawResult result)
1123+
protected override bool SetResultCore(PhysicalConnection connection, Message message, ref RespReader reader)
11241124
{
1125-
if (result.IsNull)
1125+
if (reader.IsNull)
11261126
{
11271127
SetResult(message, false); // lots of ops return (nil) when they mean "no"
11281128
return true;
11291129
}
1130-
switch (result.Resp2TypeBulkString)
1130+
switch (reader.Resp2PrefixBulkString)
11311131
{
1132-
case ResultType.SimpleString:
1133-
if (result.IsEqual(CommonReplies.OK))
1132+
case RespPrefix.SimpleString:
1133+
if (reader.IsOK())
11341134
{
11351135
SetResult(message, true);
11361136
}
11371137
else
11381138
{
1139-
SetResult(message, result.GetBoolean());
1139+
SetResult(message, reader.ReadBoolean());
11401140
}
11411141
return true;
1142-
case ResultType.Integer:
1143-
case ResultType.BulkString:
1144-
SetResult(message, result.GetBoolean());
1142+
case RespPrefix.Integer:
1143+
case RespPrefix.BulkString:
1144+
SetResult(message, reader.ReadBoolean());
1145+
return true;
1146+
case RespPrefix.Array when reader.TryReadNext() && reader.IsScalar && reader.ReadBoolean() is var value && !reader.TryReadNext():
1147+
// treat an array of 1 like a single reply (for example, SCRIPT EXISTS)
1148+
SetResult(message, value);
11451149
return true;
1146-
case ResultType.Array:
1147-
var items = result.GetItems();
1148-
if (items.Length == 1)
1149-
{ // treat an array of 1 like a single reply (for example, SCRIPT EXISTS)
1150-
SetResult(message, items[0].GetBoolean());
1151-
return true;
1152-
}
1153-
break;
11541150
}
11551151
return false;
11561152
}
11571153
}
11581154

11591155
private sealed class ByteArrayProcessor : ResultProcessor<byte[]?>
11601156
{
1161-
protected override bool SetResultCore(PhysicalConnection connection, Message message, RawResult result)
1157+
protected override bool SetResultCore(PhysicalConnection connection, Message message, ref RespReader reader)
11621158
{
1163-
switch (result.Resp2TypeBulkString)
1159+
switch (reader.Resp2PrefixBulkString)
11641160
{
1165-
case ResultType.BulkString:
1166-
SetResult(message, result.GetBlob());
1161+
case RespPrefix.BulkString:
1162+
SetResult(message, reader.ReadByteArray());
11671163
return true;
11681164
}
11691165
return false;
@@ -2838,20 +2834,21 @@ protected override KeyValuePair<string, string> Parse(in RawResult first, in Raw
28382834

28392835
private sealed class StringProcessor : ResultProcessor<string?>
28402836
{
2841-
protected override bool SetResultCore(PhysicalConnection connection, Message message, RawResult result)
2837+
protected override bool SetResultCore(PhysicalConnection connection, Message message, ref RespReader reader)
28422838
{
2843-
switch (result.Resp2TypeBulkString)
2839+
switch (reader.Resp2PrefixBulkString)
28442840
{
2845-
case ResultType.Integer:
2846-
case ResultType.SimpleString:
2847-
case ResultType.BulkString:
2848-
SetResult(message, result.GetString());
2841+
case RespPrefix.Integer:
2842+
case RespPrefix.SimpleString:
2843+
case RespPrefix.BulkString:
2844+
SetResult(message, reader.ReadString());
28492845
return true;
2850-
case ResultType.Array:
2851-
var arr = result.GetItems();
2852-
if (arr.Length == 1)
2846+
case RespPrefix.Array when reader.TryReadNext() && reader.IsScalar:
2847+
// treat an array of 1 like a single reply
2848+
var value = reader.ReadString();
2849+
if (!reader.TryReadNext())
28532850
{
2854-
SetResult(message, arr[0].GetString());
2851+
SetResult(message, value);
28552852
return true;
28562853
}
28572854
break;

tests/StackExchange.Redis.Tests/ResultProcessorUnitTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,48 @@ public class ResultProcessorUnitTests(ITestOutputHelper log)
9999
[InlineData(ATTRIB_FOO_BAR + ",3.14\r\n", 3.14)]
100100
public void NullableDouble(string resp, double? value) => Assert.Equal(value, Execute(resp, ResultProcessor.NullableDouble));
101101

102+
[Theory]
103+
[InlineData("_\r\n", false)] // null = false
104+
[InlineData(":0\r\n", false)]
105+
[InlineData(":1\r\n", true)]
106+
[InlineData("#f\r\n", false)]
107+
[InlineData("#t\r\n", true)]
108+
[InlineData("+OK\r\n", true)]
109+
[InlineData(ATTRIB_FOO_BAR + ":1\r\n", true)]
110+
public void Boolean(string resp, bool value) => Assert.Equal(value, Execute(resp, ResultProcessor.Boolean));
111+
112+
[Theory]
113+
[InlineData("*1\r\n:1\r\n", true)] // SCRIPT EXISTS returns array
114+
[InlineData("*1\r\n:0\r\n", false)]
115+
[InlineData(ATTRIB_FOO_BAR + "*1\r\n:1\r\n", true)]
116+
public void BooleanArrayOfOne(string resp, bool value) => Assert.Equal(value, Execute(resp, ResultProcessor.Boolean));
117+
118+
[Theory]
119+
[InlineData("*0\r\n")] // empty array
120+
[InlineData("*2\r\n:1\r\n:0\r\n")] // two elements
121+
[InlineData("*1\r\n*1\r\n:1\r\n")] // nested array (not scalar)
122+
public void FailingBooleanArrayOfNonOne(string resp) => ExecuteUnexpected(resp, ResultProcessor.Boolean);
123+
124+
[Theory]
125+
[InlineData("$5\r\nhello\r\n", "hello")]
126+
[InlineData("+world\r\n", "world")]
127+
[InlineData(":42\r\n", "42")]
128+
[InlineData("$-1\r\n", null)]
129+
[InlineData(ATTRIB_FOO_BAR + "$3\r\nfoo\r\n", "foo")]
130+
public void String(string resp, string? value) => Assert.Equal(value, Execute(resp, ResultProcessor.String));
131+
132+
[Theory]
133+
[InlineData("*1\r\n$3\r\nbar\r\n", "bar")]
134+
[InlineData(ATTRIB_FOO_BAR + "*1\r\n$3\r\nbar\r\n", "bar")]
135+
public void StringArrayOfOne(string resp, string? value) => Assert.Equal(value, Execute(resp, ResultProcessor.String));
136+
137+
[Theory]
138+
[InlineData("*-1\r\n")] // null array
139+
[InlineData("*0\r\n")] // empty array
140+
[InlineData("*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n")] // two elements
141+
[InlineData("*1\r\n*1\r\n$3\r\nfoo\r\n")] // nested array (not scalar)
142+
public void FailingStringArrayOfNonOne(string resp) => ExecuteUnexpected(resp, ResultProcessor.String);
143+
102144
[return: NotNullIfNotNull(nameof(array))]
103145
protected static string? Join<T>(T[]? array, string separator = ",")
104146
{

0 commit comments

Comments
 (0)