-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataset.cs
More file actions
87 lines (79 loc) · 3.12 KB
/
Dataset.cs
File metadata and controls
87 lines (79 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Data;
using Newtonsoft.Json.Linq;
abstract class Dataset
{
protected Dataset(IDbConnection connection, LibInsightClient libInsightClient) {
Connection = connection;
LibInsightClient = libInsightClient;
}
protected IDbConnection Connection { get; }
protected LibInsightClient LibInsightClient { get; }
public abstract int DatasetId { get; }
public abstract int RequestId { get; }
public abstract Task ProcessDateRange(DateTime fromDate, DateTime toDate);
/// <summary>
/// Converts a Json element to a string and normalizes apostrophes.
/// </summary>
/// <param name="input">Any Json element.</param>
/// <returns>The string representation of the element, or null if it is null or a blank string.</returns>
protected static string CleanString(JToken input)
{
var s = input?.ToString();
if (string.IsNullOrWhiteSpace(s))
return null;
return s
.Trim()
.Replace("'Äô", "'")
.Replace("’", "'");
}
/// <summary>
/// Converts a Json array to a sequence of strings using <see cref="CleanString"/>.
/// </summary>
/// <param name="input">Any Json element.</param>
/// <returns>
/// The cleaned, non-null strings in the given array, or an empty sequence if the input is not an array.
/// </returns>
protected static IEnumerable<string> JsonArrayToStrings(JToken input) =>
(input as JArray)?
.Select(CleanString)
.Where(value => value is not null) ?? Enumerable.Empty<string>();
/// <summary>
/// Converts a Json element to a number and boxes it.
/// </summary>
/// <param name="input">Any Json element.</param>
/// <returns>
/// If the Json element is a number, returns that number as a boxed int or double, otherwise returns null.
/// </returns>
protected static object NumberOrNull(JToken input) {
if (input?.Type == JTokenType.Integer) {
return (int?) input;
} else if (input?.Type == JTokenType.Float) {
return (double?) input;
} else if (input?.Type == JTokenType.String) {
return (int.TryParse(input.ToString(), out var value)) ? value : null;
} else return null;
}
/// <summary>
/// Uses DateTime.TryParse to attempt to parse the input, returning null on a failure.
/// </summary>
protected static DateTime? DateTimeOrNull(JToken input)
{
return DateTime.TryParse(input.ToString(), out var result) ? result : null;
}
/// <summary>
/// Returns the single element in a Json array, cleaned with <see cref="CleanString"/>.
/// </summary>
/// <param name="input">Any Json element.</param>
/// <returns>
/// If the input is an array, returns the cleaned single element of the array.
/// Returns null if the input is not an array or if the single element is null or blank.
/// </returns>
protected static string ArraySingleElement(JToken input)
{
if (input is JArray array)
{
return CleanString(array.SingleOrDefault());
}
else return null;
}
}