Skip to content

Commit e14e6b4

Browse files
Refactor code ♻️
- Remove custom converter and user JsonSubTypes conveter - Add Null enum to a handle in case if API returns Unknown string - Add missing StringEnumConverter on enum properties
1 parent 1403c2c commit e14e6b4

File tree

5 files changed

+147
-86
lines changed

5 files changed

+147
-86
lines changed

Src/Notion.Client/Models/Database.cs

Lines changed: 128 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using JsonSubTypes;
55
using Newtonsoft.Json;
66
using Newtonsoft.Json.Converters;
7-
using Newtonsoft.Json.Linq;
87

98
namespace Notion.Client
109
{
@@ -119,6 +118,9 @@ public class Equation
119118

120119
public enum RichTextType
121120
{
121+
[EnumMember(Value = null)]
122+
Unknown,
123+
122124
[EnumMember(Value = "text")]
123125
Text,
124126

@@ -150,55 +152,90 @@ public class Annotations
150152
public string Color { get; set; }
151153
}
152154

153-
public abstract class Property
155+
[JsonConverter(typeof(JsonSubtypes), "type")]
156+
[JsonSubtypes.KnownSubType(typeof(CheckboxProperty), PropertyType.Checkbox)]
157+
[JsonSubtypes.KnownSubType(typeof(CreatedByProperty), PropertyType.CreatedBy)]
158+
[JsonSubtypes.KnownSubType(typeof(CreatedTimeProperty), PropertyType.CreatedTime)]
159+
[JsonSubtypes.KnownSubType(typeof(DateProperty), PropertyType.Date)]
160+
[JsonSubtypes.KnownSubType(typeof(EmailProperty), PropertyType.Email)]
161+
[JsonSubtypes.KnownSubType(typeof(FileProperty), PropertyType.File)]
162+
[JsonSubtypes.KnownSubType(typeof(FormulaProperty), PropertyType.Formula)]
163+
[JsonSubtypes.KnownSubType(typeof(LastEditedByProperty), PropertyType.LastEditedBy)]
164+
[JsonSubtypes.KnownSubType(typeof(LastEditedTimeProperty), PropertyType.LastEditedTime)]
165+
[JsonSubtypes.KnownSubType(typeof(MultiSelectProperty), PropertyType.MultiSelect)]
166+
[JsonSubtypes.KnownSubType(typeof(NumberProperty), PropertyType.Number)]
167+
[JsonSubtypes.KnownSubType(typeof(PeopleProperty), PropertyType.People)]
168+
[JsonSubtypes.KnownSubType(typeof(PhoneNumberProperty), PropertyType.PhoneNumber)]
169+
[JsonSubtypes.KnownSubType(typeof(RelationProperty), PropertyType.Relation)]
170+
[JsonSubtypes.KnownSubType(typeof(RichTextProperty), PropertyType.RichText)]
171+
[JsonSubtypes.KnownSubType(typeof(RollupProperty), PropertyType.Rollup)]
172+
[JsonSubtypes.KnownSubType(typeof(SelectProperty), PropertyType.Select)]
173+
[JsonSubtypes.KnownSubType(typeof(TitleProperty), PropertyType.Title)]
174+
[JsonSubtypes.KnownSubType(typeof(UrlProperty), PropertyType.Url)]
175+
public class Property
154176
{
155177
public string Id { get; set; }
156-
public virtual string Type { get; set; }
178+
179+
[JsonConverter(typeof(StringEnumConverter))]
180+
public virtual PropertyType Type { get; set; }
157181
}
158182

159183
public class TitleProperty : Property
160184
{
161-
public override string Type => "title";
185+
public override PropertyType Type => PropertyType.Title;
162186
public Dictionary<string, object> Title { get; set; }
163187
}
164188

165189
public class RichTextProperty : Property
166190
{
167-
public override string Type => "rich_text";
191+
public override PropertyType Type => PropertyType.RichText;
168192

169193
[JsonProperty("rich_text")]
170194
public Dictionary<string, object> RichText { get; set; }
171195
}
172196

173197
public class NumberProperty : Property
174198
{
175-
public override string Type => "number";
199+
public override PropertyType Type => PropertyType.Number;
176200
public Number Number { get; set; }
177201

178202
}
179203

180204
public enum NumberFormat
181205
{
206+
[EnumMember(Value = null)]
207+
Unknown,
208+
182209
[EnumMember(Value = "number")]
183210
Number,
211+
184212
[EnumMember(Value = "number_with_commas")]
185213
NumberWithCommas,
214+
186215
[EnumMember(Value = "percent")]
187216
Percent,
217+
188218
[EnumMember(Value = "dollar")]
189219
Dollar,
220+
190221
[EnumMember(Value = "euro")]
191222
Euro,
223+
192224
[EnumMember(Value = "pound")]
193225
Pound,
226+
194227
[EnumMember(Value = "yen")]
195228
Yen,
229+
196230
[EnumMember(Value = "ruble")]
197231
Ruble,
232+
198233
[EnumMember(Value = "rupee")]
199234
Rupee,
235+
200236
[EnumMember(Value = "won")]
201237
Won,
238+
202239
[EnumMember(Value = "yuan")]
203240
Yuan
204241
}
@@ -211,7 +248,7 @@ public class Number
211248

212249
public class SelectProperty : Property
213250
{
214-
public override string Type => "select";
251+
public override PropertyType Type => PropertyType.Select;
215252
public OptionWrapper<SelectOption> Select { get; set; }
216253
}
217254

@@ -231,6 +268,9 @@ public class SelectOption
231268

232269
public enum Color
233270
{
271+
[EnumMember(Value = null)]
272+
Unknown,
273+
234274
[EnumMember(Value = "default")]
235275
Default,
236276

@@ -262,61 +302,61 @@ public enum Color
262302
Red
263303
}
264304

265-
public class MultiSelctProperty : Property
305+
public class MultiSelectProperty : Property
266306
{
267-
public override string Type => "multi_select";
307+
public override PropertyType Type => PropertyType.MultiSelect;
268308

269309
[JsonProperty("multi_select")]
270310
public OptionWrapper<SelectOption> MultiSelect { get; set; }
271311
}
272312

273313
public class DateProperty : Property
274314
{
275-
public override string Type => "date";
315+
public override PropertyType Type => PropertyType.MultiSelect;
276316
public Dictionary<string, object> Date { get; set; }
277317
}
278318

279319
public class PeopleProperty : Property
280320
{
281-
public override string Type => "people";
321+
public override PropertyType Type => PropertyType.People;
282322
public Dictionary<string, object> People { get; set; }
283323
}
284324

285325
public class FileProperty : Property
286326
{
287-
public override string Type => "file";
327+
public override PropertyType Type => PropertyType.File;
288328
public Dictionary<string, object> File { get; set; }
289329
}
290330

291331
public class CheckboxProperty : Property
292332
{
293-
public override string Type => "checkbox";
333+
public override PropertyType Type => PropertyType.Checkbox;
294334
public Dictionary<string, object> Checkbox { get; set; }
295335
}
296336

297337
public class UrlProperty : Property
298338
{
299-
public override string Type => "url";
339+
public override PropertyType Type => PropertyType.Url;
300340
public Dictionary<string, object> Url { get; set; }
301341
}
302342

303343
public class EmailProperty : Property
304344
{
305-
public override string Type => "email";
345+
public override PropertyType Type => PropertyType.Email;
306346
public Dictionary<string, object> Email { get; set; }
307347
}
308348

309349
public class PhoneNumberProperty : Property
310350
{
311-
public override string Type => "phone_number";
351+
public override PropertyType Type => PropertyType.PhoneNumber;
312352

313353
[JsonProperty("phone_number")]
314354
public Dictionary<string, object> PhoneNumber { get; set; }
315355
}
316356

317357
public class FormulaProperty : Property
318358
{
319-
public override string Type => "formula";
359+
public override PropertyType Type => PropertyType.Formula;
320360

321361
public Formula Formula { get; set; }
322362
}
@@ -328,7 +368,7 @@ public class Formula
328368

329369
public class RelationProperty : Property
330370
{
331-
public override string Type => "relation";
371+
public override PropertyType Type => PropertyType.Relation;
332372

333373
public Relation Relation { get; set; }
334374
}
@@ -347,7 +387,7 @@ public class Relation
347387

348388
public class RollupProperty : Property
349389
{
350-
public override string Type => "rollup";
390+
public override PropertyType Type => PropertyType.Rollup;
351391

352392
public Rollup Rollup { get; set; }
353393
}
@@ -372,6 +412,9 @@ public class Rollup
372412

373413
public enum Function
374414
{
415+
[EnumMember(Value = null)]
416+
Unknown,
417+
375418
[EnumMember(Value = "count_all")]
376419
CountAll,
377420
[EnumMember(Value = "count_values")]
@@ -413,93 +456,97 @@ public enum Function
413456

414457
public class CreatedTimeProperty : Property
415458
{
416-
public override string Type => "created_time";
459+
public override PropertyType Type => PropertyType.CreatedTime;
417460

418461
[JsonProperty("created_time")]
419462
public Dictionary<string, object> CreatedTime { get; set; }
420463
}
421464

422465
public class CreatedByProperty : Property
423466
{
424-
public override string Type => "created_by";
467+
public override PropertyType Type => PropertyType.CreatedBy;
425468

426469
[JsonProperty("created_by")]
427470
public Dictionary<string, object> CreatedBy { get; set; }
428471
}
429472

430473
public class LastEditedTimeProperty : Property
431474
{
432-
public override string Type => "last_edited_time";
475+
public override PropertyType Type => PropertyType.LastEditedTime;
433476

434477
[JsonProperty("last_edited_time")]
435478
public Dictionary<string, object> LastEditedTime { get; set; }
436479
}
437480

438481
public class LastEditedByProperty : Property
439482
{
440-
public override string Type => "last_edited_by";
483+
public override PropertyType Type => PropertyType.LastEditedBy;
441484

442485
[JsonProperty("last_edited_by")]
443486
public Dictionary<string, object> LastEditedBy { get; set; }
444487
}
445488

446-
// Todo: Is there a better way?
447-
public class PropertyConverter : JsonConverter<Property>
448-
{
449-
public override Property ReadJson(JsonReader reader, Type objectType, Property existingValue, bool hasExistingValue, JsonSerializer serializer)
450-
{
451-
var jsonObject = JObject.Load(reader);
452-
var type = jsonObject["type"].Value<string>();
453-
454-
switch (type)
455-
{
456-
case "title":
457-
return jsonObject.ToObject<TitleProperty>();
458-
case "rich_text":
459-
return jsonObject.ToObject<RichTextProperty>();
460-
case "number":
461-
return jsonObject.ToObject<NumberProperty>();
462-
case "select":
463-
return jsonObject.ToObject<SelectProperty>();
464-
case "multi_select":
465-
return jsonObject.ToObject<MultiSelctProperty>();
466-
case "date":
467-
return jsonObject.ToObject<DateProperty>();
468-
case "people":
469-
return jsonObject.ToObject<PeopleProperty>();
470-
case "file":
471-
return jsonObject.ToObject<FileProperty>();
472-
case "checkbox":
473-
return jsonObject.ToObject<CheckboxProperty>();
474-
case "url":
475-
return jsonObject.ToObject<UrlProperty>();
476-
case "email":
477-
return jsonObject.ToObject<EmailProperty>();
478-
case "phone_number":
479-
return jsonObject.ToObject<PhoneNumberProperty>();
480-
case "formula":
481-
return jsonObject.ToObject<FormulaProperty>();
482-
case "relation":
483-
return jsonObject.ToObject<RelationProperty>();
484-
case "rollup":
485-
return jsonObject.ToObject<RollupProperty>();
486-
case "created_time":
487-
return jsonObject.ToObject<CreatedTimeProperty>();
488-
case "created_by":
489-
return jsonObject.ToObject<CreatedByProperty>();
490-
case "last_edited_by":
491-
return jsonObject.ToObject<LastEditedByProperty>();
492-
case "last_edited_time":
493-
return jsonObject.ToObject<LastEditedTimeProperty>();
494-
495-
default:
496-
throw new InvalidOperationException();
497-
}
498-
}
499-
500-
public override void WriteJson(JsonWriter writer, Property value, JsonSerializer serializer)
501-
{
502-
throw new NotImplementedException();
503-
}
489+
490+
public enum PropertyType
491+
{
492+
[EnumMember(Value = null)]
493+
Unknown,
494+
495+
[EnumMember(Value = "title")]
496+
Title,
497+
498+
[EnumMember(Value = "rich_text")]
499+
RichText,
500+
501+
[EnumMember(Value = "number")]
502+
Number,
503+
504+
[EnumMember(Value = "select")]
505+
Select,
506+
507+
[EnumMember(Value = "multi_select")]
508+
MultiSelect,
509+
510+
[EnumMember(Value = "date")]
511+
Date,
512+
513+
[EnumMember(Value = "people")]
514+
People,
515+
516+
[EnumMember(Value = "file")]
517+
File,
518+
519+
[EnumMember(Value = "checkbox")]
520+
Checkbox,
521+
522+
[EnumMember(Value = "url")]
523+
Url,
524+
525+
[EnumMember(Value = "email")]
526+
Email,
527+
528+
[EnumMember(Value = "phone_number")]
529+
PhoneNumber,
530+
531+
[EnumMember(Value = "formula")]
532+
Formula,
533+
534+
[EnumMember(Value = "relation")]
535+
Relation,
536+
537+
[EnumMember(Value = "rollup")]
538+
Rollup,
539+
540+
[EnumMember(Value = "created_time")]
541+
CreatedTime,
542+
543+
[EnumMember(Value = "created_by")]
544+
CreatedBy,
545+
546+
[EnumMember(Value = "last_edited_by")]
547+
LastEditedBy,
548+
549+
[EnumMember(Value = "last_edited_time")]
550+
LastEditedTime
504551
}
505552
}

0 commit comments

Comments
 (0)