Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 4c0678e

Browse files
committed
Add new API to read unstructured complex type responses
1 parent cad3f7b commit 4c0678e

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
3+
namespace ServiceStack.Redis
4+
{
5+
public static class RedisDataExtensions
6+
{
7+
public static RedisText ToRedisText(this RedisData data)
8+
{
9+
var to = new RedisText();
10+
11+
if (data.Data != null)
12+
to.Text = data.Data.FromUtf8Bytes();
13+
14+
if (data.Children != null)
15+
to.Children = data.Children.ConvertAll(x => x.ToRedisText());
16+
17+
return to;
18+
}
19+
}
20+
}

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111

1212
using System;
13+
using System.Collections.Generic;
1314
using System.Diagnostics;
1415
using System.Globalization;
1516
using System.IO;
@@ -114,6 +115,9 @@ private void Connect()
114115
if (db != 0)
115116
SendExpectSuccess(Commands.Select, db.ToUtf8Bytes());
116117

118+
if (Client != null)
119+
SendExpectSuccess(Commands.Client, Commands.SetName, Client.ToUtf8Bytes());
120+
117121
try
118122
{
119123
if (ServerVersionNumber == 0)
@@ -527,6 +531,19 @@ protected object[] SendExpectDeeplyNestedMultiData(params byte[][] cmdWithBinary
527531
return ReadDeeplyNestedMultiData();
528532
}
529533

534+
protected RedisData SendExpectComplexResponse(params byte[][] cmdWithBinaryArgs)
535+
{
536+
if (!SendCommand(cmdWithBinaryArgs))
537+
throw CreateConnectionError();
538+
539+
if (Pipeline != null)
540+
{
541+
throw new NotSupportedException("Pipeline is not supported.");
542+
}
543+
544+
return ReadComplexResponse();
545+
}
546+
530547
protected void Log(string fmt, params object[] args)
531548
{
532549
log.DebugFormat("{0}", string.Format(fmt, args).Trim());
@@ -802,6 +819,47 @@ private object ReadDeeplyNestedMultiDataItem()
802819
throw CreateResponseError("Unknown reply on multi-request: " + c + s);
803820
}
804821

822+
internal RedisData ReadComplexResponse()
823+
{
824+
int c = SafeReadByte();
825+
if (c == -1)
826+
throw CreateResponseError("No more data");
827+
828+
var s = ReadLine();
829+
if (log.IsDebugEnabled)
830+
Log("R: {0}", s);
831+
832+
switch (c)
833+
{
834+
case '$':
835+
return new RedisData {
836+
Data = ParseSingleLine(string.Concat(char.ToString((char) c), s))
837+
};
838+
839+
case '-':
840+
throw CreateResponseError(s.StartsWith("ERR") ? s.Substring(4) : s);
841+
842+
case '*':
843+
int count;
844+
if (int.TryParse(s, out count))
845+
{
846+
var ret = new RedisData { Children = new List<RedisData>() };
847+
for (var i = 0; i < count; i++)
848+
{
849+
ret.Children.Add(ReadComplexResponse());
850+
}
851+
852+
return ret;
853+
}
854+
break;
855+
856+
default:
857+
return new RedisData { Data = s.ToUtf8Bytes() };
858+
}
859+
860+
throw CreateResponseError("Unknown reply on multi-request: " + c + s);
861+
}
862+
805863
internal int ReadMultiDataResultCount()
806864
{
807865
int c = SafeReadByte();

0 commit comments

Comments
 (0)