Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ViewResult.Items uses RawDocs instead of RawValues when IncludeDocs is true #41

Merged
merged 3 commits into from

2 participants

@pagebrooks

When you have have IncludeDocs set to true and you are querying a view like:

function(doc) {
  if(doc.type == 'MyType') {
    emit(doc._id, null);
  }
}

The ViewResult.Items collection does not return anything. With this pull request, the ViewResult.Items collection will be populated from the RawDocs when IncludeDocs is true.

@soitgoes soitgoes merged commit b9c3a58 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
39 LoveSeat.IntegrationTest/CouchClientTest.cs
@@ -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
{
View
9 LoveSeat/CouchDatabase.cs
@@ -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
View
2  LoveSeat/Interfaces/IViewResult.cs
@@ -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>
View
52 LoveSeat/ViewResult.cs
@@ -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>
Something went wrong with that request. Please try again.