Permalink
Browse files

Improved view option syntax.

  • Loading branch information...
1 parent 9246c11 commit 4e1e7fb4ebac97a7ae778240c4f389d616746ac3 @soitgoes committed Dec 15, 2010
@@ -35,5 +35,26 @@ public void KeyOptions_Should_Produce_A_Complex_Array_For_Multiple_Values()
var result = arry.ToString();
Assert.AreEqual("[1,\"2011-01-01T00:00:00\"]", result);
}
+
+ [Test]
+ public void KeyOptions_Should_Produce_Squirley_Brackets_for_CouchValueEmpty()
+ {
+ var arry = new KeyOptions();
+ arry.Add(CouchValue.Empty);
+ arry.Add(1);
+ var result = arry.ToString();
+ Assert.AreEqual("[{},1]", result);
+ }
+
+ [Test]
+ public void KeyOptions_Should_Produce_IsoTime()
+ {
+ var arry = new KeyOptions();
+ arry.Add(CouchValue.Empty);
+ arry.Add(new DateTime(2011,1,1));
+ var result = arry.ToString();
+ Assert.AreEqual("[{},\"2011-01-01T00:00:00\"]", result);
+
+ }
}
}
@@ -4,9 +4,9 @@ namespace LoveSeat.Interfaces
{
public interface IViewOptions
{
- KeyOptions Key { get; set; }
- KeyOptions StartKey { get; set; }
- KeyOptions EndKey { get; set; }
+ IKeyOptions Key { get; set; }
+ IKeyOptions StartKey { get; set; }
+ IKeyOptions EndKey { get; set; }
int? Limit { get; set; }
int? Skip { get; set; }
bool? Reduce { get; set; }
@@ -8,21 +8,99 @@
namespace LoveSeat
{
- public class KeyOptions : JArray
+ public interface IKeyOptions
{
-
+ string ToString();
+ void Insert(int index, JToken item);
+ void RemoveAt(int index);
+ void Add(JToken item);
+ bool Remove(JToken item);
+ int Count { get; }
+ bool HasValues { get; }
+ void Add(object content);
+ }
+
+ public class KeyOptions : IKeyOptions
+ {
+ private JArray objects;
+ public KeyOptions(params object[] objects)
+ {
+ this.objects = new JArray(objects);
+ }
+ public KeyOptions(JArray jArray)
+ {
+ this.objects = jArray;
+ }
public override string ToString()
{
- if (Count == 1)
+ if (objects.Count ==0 ) return "";
+ if (objects.Count ==1 ) return objects[0].ToString(Formatting.None, new IsoDateTimeConverter());
+ string result = "[";
+ bool first = true;
+ foreach (var item in objects)
{
- return this[0].ToString(Formatting.None, new IsoDateTimeConverter());
+ if (!first)
+ result += ",";
+ first = false;
+ result += item.ToString(Formatting.None, new IsoDateTimeConverter());
}
- if (Count > 1)
- {
- return base.ToString(Formatting.None, new IsoDateTimeConverter());
- }
- return "";
+ result += "]";
+ return result;
}
-
+
+ public void Insert(int index, JToken item)
+ {
+ objects.Insert(index, item);
+ }
+
+ public void RemoveAt(int index)
+ {
+ objects.RemoveAt(index);
+ }
+
+ public void Add(JToken item)
+ {
+ objects.Add(item);
+ }
+
+ public bool Remove(JToken item)
+ {
+ return objects.Remove(item);
+ }
+
+ public int Count
+ {
+ get { return objects.Count; }
+ }
+
+ public bool HasValues
+ {
+ get { return objects.Count > 0; }
+ }
+ public void Add(params object[] items)
+ {
+ foreach (var item in items)
+ Add(item);
+ }
+ public void Add(object item)
+ {
+ if (item == CouchValue.Empty)
+ {
+ objects.Add(new JRaw("{}"));
+ return;
+ }
+ objects.Add(item);
+ }
+ }
+ public static class CouchValue
+ {
+ static object value = new object();
+ public static object Empty
+ {
+ get
+ {
+ return value;
+ }
+ }
}
}
@@ -15,19 +15,19 @@ public ViewOptions()
StartKey = new KeyOptions();
EndKey = new KeyOptions();
}
- /// <summary>
+ /// <summary>
/// If you have a complex object as a string set this using a JRaw object()
/// </summary>
- public KeyOptions Key { get; set; }
+ public IKeyOptions Key { get; set; }
/// <summary>
/// If you have a complex object as a string set this using a JRaw object()
/// </summary>
- public KeyOptions StartKey { get; set; }
+ public IKeyOptions StartKey { get; set; }
public string StartKeyDocId { get; set; }
/// <summary>
/// If you have a complex object as a string set this using a JRaw object()
/// </summary>
- public KeyOptions EndKey { get; set; }
+ public IKeyOptions EndKey { get; set; }
public string EndKeyDocId { get; set; }
public int? Limit { get; set; }
public int? Skip { get; set; }
@@ -40,12 +40,6 @@ public ViewOptions()
public bool? Stale { get; set; }
public string Etag { get; set; }
- public ViewOptions()
- {
- Key = new KeyOptions();
- StartKey = new KeyOptions();
- EndKey = new KeyOptions();
- }
public override string ToString()
{
View
@@ -62,8 +62,14 @@ Assuming that your view keys have complex structure, for example:
...
var options = new ViewOptions();
+
+ options.StartKey.Add("1234", CouchValue.Empty); // passes "[1234, {}]"
+ options.EndKey.Add("1234", DateTime.Now.AddYears(1)); //Dates are properly converted to ISO8601
+
+ // or if you would like to do all the work and create the json yourself...
options.StartKey.Add(new JRaw("[\"johny\",[\"work\"]"));
- options.EndKey.Add(new JRaw("[\"johny\",[\"work\",{}]]"));
+ options.EndKey.Add(new JRaw("[\"johny\",[\"work\",{}]]"));
+
results = db.View<MyObject>("view_name", options);
// loop through your strongly typed results

0 comments on commit 4e1e7fb

Please sign in to comment.