This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redis INFO parser was added to RedisDataInfoExtension
- Loading branch information
1 parent
3310e30
commit ba36e0e
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web.Script.Serialization; | ||
|
||
namespace ServiceStack.Redis | ||
{ | ||
public static class RedisDataInfoExtensions | ||
{ | ||
public static String ToJsonInfo(this RedisText redisText) | ||
{ | ||
var source = redisText.GetResult(); | ||
return Parse(source); | ||
} | ||
|
||
#region Private | ||
|
||
private static String Parse(String source) | ||
{ | ||
var result = new Dictionary<String, Dictionary<String, String>>(); | ||
var section = new Dictionary<String, String>(); | ||
|
||
var rows = SplitRows(source); | ||
|
||
foreach (var row in rows) | ||
{ | ||
if (row.IndexOf("#", StringComparison.Ordinal) == 0) | ||
{ | ||
var name = ParseSection(row); | ||
section = new Dictionary<String, String>(); | ||
result.Add(name, section); | ||
} | ||
else | ||
{ | ||
var pair = ParseKeyValue(row); | ||
if (pair.HasValue) | ||
{ | ||
section.Add(pair.Value.Key, pair.Value.Value); | ||
} | ||
} | ||
} | ||
|
||
return new JavaScriptSerializer().Serialize(result); | ||
} | ||
|
||
private static IEnumerable<String> SplitRows(String source) | ||
{ | ||
return source.Split(new[] { "\r\n" }, StringSplitOptions.None).Where(n => !String.IsNullOrWhiteSpace(n)); | ||
} | ||
|
||
private static String ParseSection(String source) | ||
{ | ||
return (source.IndexOf("#", StringComparison.Ordinal) == 0) | ||
? source.Trim('#').Trim() | ||
: String.Empty; | ||
} | ||
|
||
private static KeyValuePair<String, String>? ParseKeyValue(String source) | ||
{ | ||
KeyValuePair<String, String>? result = null; | ||
|
||
var devider = source.IndexOf(":", StringComparison.Ordinal); | ||
if (devider > 0) | ||
{ | ||
var name = source.Substring(0, devider); | ||
var value = source.Substring(devider + 1); | ||
result = new KeyValuePair<String, String>(name.Trim(), value.Trim()); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
#endregion Private | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters