Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from Dec 6, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions LoveSeat.IntegrationTest/CouchClientTest.cs
Expand Up @@ -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
{
Expand Down
9 changes: 8 additions & 1 deletion LoveSeat/CouchDatabase.cs
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions LoveSeat/Interfaces/IViewResult.cs
Expand Up @@ -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>
Expand Down
52 changes: 33 additions & 19 deletions LoveSeat/ViewResult.cs
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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));
}
}
}
Expand All @@ -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
Expand All @@ -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>
Expand All @@ -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>
Expand Down