Permalink
Browse files

Merge pull request #41 from pagebrooks/master

ViewResult.Items uses RawDocs instead of RawValues when IncludeDocs is true
  • Loading branch information...
2 parents 2b9d7cc + d576655 commit b9c3a583fe2901d6eb00ace64c133716f5c54bb6 @soitgoes committed Dec 6, 2012
@@ -219,6 +219,45 @@ public void Should_Get_Id_From_Existing_Document()
Document doc= db.GetDocument(id);
Assert.AreEqual(id, doc.Id);
}
+
+ [Test]
+ public void Should_Populate_Items_When_IncludeDocs_Set_In_ViewOptions()
+ {
+ string designDoc = "test";
+ string viewName = "testView";
+ var settings = new JsonSerializerSettings();
+ var converters = new List<JsonConverter> { new IsoDateTimeConverter() };
+ settings.Converters = converters;
+ settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
+ settings.NullValueHandling = NullValueHandling.Ignore;
+
+ var doc = new
+ {
+ _id = "_design/" + designDoc,
+ Language = "javascript",
+ Views = new
+ {
+ TestView = new
+ {
+ Map = "function(doc) {\n if(doc.type == 'company') {\n emit(doc._id, null);\n }\n}"
+ }
+ }
+ };
+
+ var db = client.GetDatabase(baseDatabase);
+ db.CreateDocument(doc._id, JsonConvert.SerializeObject(doc, Formatting.Indented, settings));
+
+ var company = new Company();
+ company.Name = "foo";
+ db.CreateDocument(company);
+
+ // Without IncludeDocs
+ Assert.IsNull(db.View<Company>(viewName, designDoc).Items.ToList()[0]);
+
+ // With IncludeDocs
+ ViewOptions options = new ViewOptions { IncludeDocs = true };
+ Assert.AreEqual("foo", db.View<Company>(viewName, options, designDoc).Items.ToList()[0].Name);
+ }
}
public class Company : IBaseObject
{
@@ -362,7 +362,14 @@ public void SetDefaultDesignDoc(string designDoc)
if (resp.StatusCode == HttpStatusCode.BadRequest) {
throw new CouchException(req.GetRequest(), resp, resp.GetResponseString() + "\n" + req.GetRequest().RequestUri);
}
- return new ViewResult<T>(resp, req.GetRequest(), ObjectSerializer);
+
+ bool includeDocs = false;
+ if (options != null)
+ {
+ includeDocs = options.IncludeDocs ?? false;
+ }
+
+ return new ViewResult<T>(resp, req.GetRequest(), ObjectSerializer, includeDocs);
}
/// <summary>
/// Gets the results of the view using any and all parameters
@@ -29,6 +29,8 @@ public interface IViewResult : System.IEquatable<IListResult>
/// </summary>
IEnumerable<JToken> Docs { get; }
+ bool IncludeDocs { get; }
+
/// <summary>
/// An IEnumerable of strings insteda of the IEnumerable of JTokens
/// </summary>
@@ -14,11 +14,11 @@ public class ViewResult<T> : ViewResult
{
private readonly IObjectSerializer objectSerializer = null;
private CouchDictionary<T> dict = null;
- public ViewResult(HttpWebResponse response, HttpWebRequest request, IObjectSerializer objectSerializer)
- : base(response, request)
+ public ViewResult(HttpWebResponse response, HttpWebRequest request, IObjectSerializer objectSerializer, bool includeDocs = false)
+ : base(response, request, includeDocs)
{
this.objectSerializer = objectSerializer;
-
+
}
public CouchDictionary<T> Dictionary
@@ -28,7 +28,7 @@ public CouchDictionary<T> Dictionary
if (dict != null) return dict;
dict = new CouchDictionary<T>();
foreach (var row in this.Rows)
- {
+ {
dict.Add(row.Value<JToken>("key").ToString(Formatting.None), objectSerializer.Deserialize<T>(row.Value<string>("value")));
}
return dict;
@@ -43,7 +43,9 @@ public IEnumerable<T> Items
{
throw new InvalidOperationException("ObjectSerializer must be set in order to use the generic view.");
}
- return this.RawValues.Select(item => objectSerializer.Deserialize<T>(item));
+
+ var values = this.IncludeDocs ? this.RawDocs : this.RawValues;
+ return values.Select(item => objectSerializer.Deserialize<T>(item));
}
}
}
@@ -56,11 +58,12 @@ public class ViewResult : IViewResult
private readonly string responseString;
public JObject Json { get { return json ?? (json = JObject.Parse(responseString)); } }
- public ViewResult(HttpWebResponse response, HttpWebRequest request)
+ public ViewResult(HttpWebResponse response, HttpWebRequest request, bool includeDocs = false)
{
this.response = response;
this.request = request;
this.responseString = response.GetResponseString();
+ this.IncludeDocs = includeDocs;
}
/// <summary>
/// Typically won't be needed. Provided for debuging assistance
@@ -73,21 +76,30 @@ public ViewResult(HttpWebResponse response, HttpWebRequest request)
public HttpStatusCode StatusCode { get { return response.StatusCode; } }
public string Etag { get { return response.Headers["ETag"]; } }
- public int TotalRows { get
+ public int TotalRows
{
- if (Json["total_rows"] == null) throw new CouchException(request, response, Json["reason"].Value<string>());
- return Json["total_rows"].Value<int>();
- } }
- public int OffSet { get
+ get
+ {
+ if (Json["total_rows"] == null) throw new CouchException(request, response, Json["reason"].Value<string>());
+ return Json["total_rows"].Value<int>();
+ }
+ }
+ public int OffSet
{
- if (Json["offset"] == null) throw new CouchException(request, response, Json["reason"].Value<string>());
- return Json["offset"].Value<int>();
- } }
- public IEnumerable<JToken> Rows { get
+ get
+ {
+ if (Json["offset"] == null) throw new CouchException(request, response, Json["reason"].Value<string>());
+ return Json["offset"].Value<int>();
+ }
+ }
+ public IEnumerable<JToken> Rows
{
- if (Json["rows"] == null) throw new CouchException(request, response, Json["reason"].Value<string>());
- return (JArray)Json["rows"];
- } }
+ get
+ {
+ if (Json["rows"] == null) throw new CouchException(request, response, Json["reason"].Value<string>());
+ return (JArray)Json["rows"];
+ }
+ }
/// <summary>
/// Only populated when IncludeDocs is true
/// </summary>
@@ -99,12 +111,14 @@ public IEnumerable<JToken> Docs
}
}
+ public bool IncludeDocs { get; private set; }
+
public JToken[] Keys
{
get
{
var arry = (JArray)Json["rows"];
- return arry.Select(item => item["key"]).ToArray();
+ return arry.Select(item => item["key"]).ToArray();
}
}
/// <summary>

0 comments on commit b9c3a58

Please sign in to comment.