Skip to content

Commit a78aef2

Browse files
Merge pull request #125 from panoukos41/dev
Fix CouchViewOptions
2 parents af22327 + 90a1bf5 commit a78aef2

File tree

3 files changed

+77
-78
lines changed

3 files changed

+77
-78
lines changed

src/CouchDB.Driver/Helpers/CouchContractResolver.cs

-12
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ internal CouchContractResolver(PropertyCaseType propertyCaseType)
2424
if (property != null && !property.Ignored)
2525
{
2626
property.PropertyName = member.GetCouchPropertyName(_propertyCaseType);
27-
28-
DefaultValueAttribute? defaultValueAttribute = member.GetCustomAttribute<DefaultValueAttribute>();
29-
if (defaultValueAttribute != null && member is PropertyInfo propertyInfo)
30-
{
31-
property.ShouldSerialize =
32-
instance =>
33-
{
34-
object? value = propertyInfo.GetValue(instance);
35-
var shouldSerialize = !Equals(value, defaultValueAttribute.Value);
36-
return shouldSerialize;
37-
};
38-
}
3927
}
4028
return property;
4129
}
+47-66
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#nullable disable
2-
#pragma warning disable CA2227 // Collection properties should be read only
1+
#pragma warning disable CA2227 // Collection properties should be read only
32
using System.Collections.Generic;
43
using System.ComponentModel;
54
using Newtonsoft.Json;
@@ -16,162 +15,144 @@ public class CouchViewOptions<TKey>
1615
/// Include conflicts information in response.
1716
/// Ignored if <see cref="IncludeDocs"/> isn't <c>True</c>. Default is <c>False</c>.
1817
/// </summary>
19-
[JsonProperty("conflicts")]
20-
[DefaultValue(false)]
21-
public bool Conflicts { get; set; }
18+
[JsonProperty("conflicts", NullValueHandling = NullValueHandling.Ignore)]
19+
public bool? Conflicts { get; set; }
2220

2321
/// <summary>
2422
/// Return the documents in descending order by key. Default is <c>False</c>.
2523
/// </summary>
26-
[JsonProperty("descending")]
27-
[DefaultValue(false)]
28-
public bool Descending { get; set; }
24+
[JsonProperty("descending", NullValueHandling = NullValueHandling.Ignore)]
25+
public bool? Descending { get; set; }
2926

3027
/// <summary>
3128
/// Stop returning records when the specified key is reached.
3229
/// </summary>
33-
[JsonProperty("endkey")]
34-
[DefaultValue(null)]
35-
public TKey EndKey { get; set; }
30+
[JsonProperty("endkey", NullValueHandling = NullValueHandling.Ignore)]
31+
public TKey? EndKey { get; set; }
3632

3733
/// <summary>
3834
/// Stop returning records when the specified document ID is reached.
3935
/// Ignored if <see cref="EndKey"/> is not set.
4036
/// </summary>
41-
[JsonProperty("endkey_docid")]
42-
[DefaultValue(null)]
43-
public string EndKeyDocId { get; set; }
37+
[JsonProperty("endkey_docid", NullValueHandling = NullValueHandling.Ignore)]
38+
public string? EndKeyDocId { get; set; }
4439

4540
/// <summary>
4641
/// Group the results using the reduce function to a group or single row.
4742
/// Implies reduce is <c>True</c> and the maximum <see cref="GroupLevel"/>. Default is <c>False</c>.
4843
/// </summary>
49-
[JsonProperty("group")]
50-
[DefaultValue(false)]
51-
public bool Group { get; set; }
44+
[JsonProperty("group", NullValueHandling = NullValueHandling.Ignore)]
45+
public bool? Group { get; set; }
5246

5347
/// <summary>
5448
/// Specify the group level to be used. Implies group is <c>True</c>.
5549
/// </summary>
56-
[JsonProperty("group_level")]
57-
[DefaultValue(null)]
50+
[JsonProperty("group_level", NullValueHandling = NullValueHandling.Ignore)]
5851
public int? GroupLevel { get; set; }
5952

6053
/// <summary>
6154
/// Include the associated document with each row. Default is <c>False</c>.
6255
/// </summary>
63-
[JsonProperty("include_docs")]
64-
[DefaultValue(false)]
65-
public bool IncludeDocs { get; set; }
56+
[JsonProperty("include_docs", NullValueHandling = NullValueHandling.Ignore)]
57+
public bool? IncludeDocs { get; set; }
6658

6759
/// <summary>
6860
/// Include the Base64-encoded content of attachments in the documents that are included if <see cref="IncludeDocs"/> is <c>True</c>.
6961
/// Ignored if <see cref="IncludeDocs"/> isn’t <c>True</c>. Default is <c>False</c>.
7062
/// </summary>
71-
[JsonProperty("attachments")]
72-
[DefaultValue(false)]
73-
public bool Attachments { get; set; }
63+
[JsonProperty("attachments", NullValueHandling = NullValueHandling.Ignore)]
64+
public bool? Attachments { get; set; }
7465

7566
/// <summary>
7667
/// Include encoding information in attachment stubs if <see cref="IncludeDocs"/> is <c>True</c> and the particular attachment is compressed.
7768
/// Ignored if <see cref="IncludeDocs"/> isn’t <c>True</c>. Default is <c>False</c>.
7869
/// </summary>
79-
[JsonProperty("att_encoding_info")]
80-
[DefaultValue(false)]
81-
public bool AttachEncodingInfo { get; set; }
70+
[JsonProperty("att_encoding_info", NullValueHandling = NullValueHandling.Ignore)]
71+
public bool? AttachEncodingInfo { get; set; }
8272

8373
/// <summary>
8474
/// Specifies whether the specified end key should be included in the result. Default is <c>True</c>.
8575
/// </summary>
86-
[JsonProperty("inclusive_end")]
87-
[DefaultValue(true)]
88-
public bool InclusiveEnd { get; set; } = true;
76+
[JsonProperty("inclusive_end", NullValueHandling = NullValueHandling.Ignore)]
77+
public bool? InclusiveEnd { get; set; }
8978

9079
/// <summary>
9180
/// Return only documents that match the specified key.
9281
/// </summary>
93-
[JsonProperty("key")]
94-
[DefaultValue(null)]
95-
public TKey Key { get; set; }
82+
[JsonProperty("key", NullValueHandling = NullValueHandling.Ignore)]
83+
public TKey? Key { get; set; }
9684

9785
/// <summary>
9886
/// Return only documents where the key matches one of the keys specified in the array.
9987
/// </summary>
100-
[JsonProperty("keys")]
101-
[DefaultValue(null)]
102-
public IList<TKey> Keys { get; set; }
88+
[JsonProperty("keys", NullValueHandling = NullValueHandling.Ignore)]
89+
public IList<TKey>? Keys { get; set; }
10390

10491
/// <summary>
10592
/// Limit the number of the returned documents to the specified number.
10693
/// </summary>
107-
[JsonProperty("limit")]
108-
[DefaultValue(null)]
94+
[JsonProperty("limit", NullValueHandling = NullValueHandling.Ignore)]
10995
public int? Limit { get; set; }
11096

11197
/// <summary>
11298
/// Use the reduction function. Default is <c>True</c> when a reduce function is defined.
11399
/// </summary>
114-
[JsonProperty("reduce")]
115-
[DefaultValue(false)]
116-
public bool Reduce { get; set; }
100+
[JsonProperty("reduce", NullValueHandling = NullValueHandling.Ignore)]
101+
public bool? Reduce { get; set; }
117102

118103
/// <summary>
119104
/// Skip this number of records before starting to return the results. Default is <code>0</code>.
120105
/// </summary>
121-
[JsonProperty("skip")]
122-
[DefaultValue(0)]
123-
public int Skip { get; set; }
106+
[JsonProperty("skip", NullValueHandling = NullValueHandling.Ignore)]
107+
public int? Skip { get; set; }
124108

125109
/// <summary>
126110
/// Sort returned rows (see Sorting <see href="https://docs.couchdb.org/en/stable/api/ddoc/views.html#api-ddoc-view-sorting"></see> Returned Rows).
127111
/// Setting this to false offers a performance boost.
128112
/// The <see cref="CouchViewResult{TKey, TRow}.TotalRows"/> and <see cref="CouchViewResult{TKey, TRow}.Offset"/> fields are not available when this is set to <c>False</c>.
129113
/// Default is <c>True</c>.
130114
/// </summary>
131-
[JsonProperty("sorted")]
132-
[DefaultValue(true)]
133-
public bool Sorted { get; set; } = true;
115+
[JsonProperty("sorted", NullValueHandling = NullValueHandling.Ignore)]
116+
public bool? Sorted { get; set; }
134117

135118
/// <summary>
136-
/// Whether or not the view results should be returned from a stable set of shards. Default is <c>False</c>.
119+
/// Whether or not the view results should be returned from a stable set of shards.
120+
/// Supported values <see cref="StableStyle.True"/>, <see cref="StableStyle.False"/>. Default is <see cref="StableStyle.False"/>
137121
/// </summary>
138-
[JsonProperty("stable")]
139-
[DefaultValue(false)]
140-
public bool Stable { get; set; }
122+
[JsonIgnore]
123+
public StableStyle? Stable { get; set; }
124+
125+
[JsonProperty("stable", NullValueHandling = NullValueHandling.Ignore)]
126+
internal string? StableString => Stable?.ToString();
141127

142128
/// <summary>
143129
/// Return records starting with the specified key.
144130
/// </summary>
145-
[JsonProperty("startkey")]
146-
[DefaultValue(null)]
147-
public TKey StartKey { get; set; }
131+
[JsonProperty("startkey", NullValueHandling = NullValueHandling.Ignore)]
132+
public TKey? StartKey { get; set; }
148133

149134
/// <summary>
150135
/// Return records starting with the specified document ID. Ignored if <see cref="StartKey"/> is not set.
151136
/// </summary>
152-
[JsonProperty("startkey_docid")]
153-
[DefaultValue(null)]
154-
public string StartKeyDocId { get; set; }
137+
[JsonProperty("startkey_docid", NullValueHandling = NullValueHandling.Ignore)]
138+
public string? StartKeyDocId { get; set; }
155139

156140
/// <summary>
157141
/// Whether or not the view in question should be updated prior to responding to the user.
158142
/// Supported values: <see cref="UpdateStyle.True"/>, <see cref="UpdateStyle.False"/>, <see cref="UpdateStyle.Lazy"/>. Default is <see cref="UpdateStyle.True"/>.
159143
/// </summary>
160144
[JsonIgnore]
161-
public UpdateStyle Update { get; set; } = UpdateStyle.True;
145+
public UpdateStyle? Update { get; set; }
162146

163-
[JsonProperty("update")]
164-
[DefaultValue("true")]
165-
internal string UpdateString => Update.ToString();
147+
[JsonProperty("update", NullValueHandling = NullValueHandling.Ignore)]
148+
internal string? UpdateString => Update?.ToString();
166149

167150
/// <summary>
168151
/// Whether to include in the response an <see cref="UpdateSeq"/> value indicating the sequence id of the database the view reflects.
169152
/// Default is <c>False</c>.
170153
/// </summary>
171-
[JsonProperty("update_seq")]
172-
[DefaultValue(false)]
173-
public bool UpdateSeq { get; set; }
154+
[JsonProperty("update_seq", NullValueHandling = NullValueHandling.Ignore)]
155+
public bool? UpdateSeq { get; set; }
174156
}
175157
}
176158
#pragma warning restore CA2227 // Collection properties should be read only
177-
#nullable restore
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace CouchDB.Driver.Views
2+
{
3+
/// <summary>
4+
/// Whether or not the view results should be returned from a stable set of shards.
5+
/// </summary>
6+
public class StableStyle
7+
{
8+
private readonly string _value;
9+
10+
/// <summary>
11+
/// The view results will be returned from a stable set of shards.
12+
/// </summary>
13+
public static StableStyle True => new("true");
14+
15+
/// <summary>
16+
/// The view results will be returned from an unstable set of shards.
17+
/// </summary>
18+
public static StableStyle False => new("false");
19+
20+
private StableStyle(string value)
21+
{
22+
_value = value;
23+
}
24+
25+
public override string ToString()
26+
{
27+
return _value;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)