-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilders.cs
More file actions
211 lines (183 loc) · 6.82 KB
/
Copy pathBuilders.cs
File metadata and controls
211 lines (183 loc) · 6.82 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System.Collections.Generic;
using System.Linq;
using JSONstat.IO;
using JSONstat.Models;
namespace JSONstat.Builders;
/// <summary>Builds a <see cref="Category"/> fluently.</summary>
public sealed class CategoryBuilder
{
private readonly List<string> _index = new List<string>();
private readonly Dictionary<string, string> _label = new Dictionary<string, string>();
private Dictionary<string, string[]>? _child;
private Dictionary<string, double[]>? _coordinates;
private Dictionary<string, Unit>? _units;
/// <summary>Adds category ids in order.</summary>
public CategoryBuilder Ids(params string[] ids)
{
foreach (var id in ids)
{
_index.Add(id);
if (!_label.ContainsKey(id)) _label[id] = id;
}
return this;
}
/// <summary>Sets the label of a category id (adding it to the order if new).</summary>
public CategoryBuilder Label(string id, string label)
{
_label[id] = label;
if (!_index.Contains(id)) _index.Add(id);
return this;
}
/// <summary>Adds a parent -> children hierarchy entry.</summary>
public CategoryBuilder Child(string parent, params string[] children)
{
(_child ??= new Dictionary<string, string[]>())[parent] = children;
return this;
}
/// <summary>Sets geographic coordinates for a category id.</summary>
public CategoryBuilder Coordinates(string id, double longitude, double latitude)
{
(_coordinates ??= new Dictionary<string, double[]>())[id] = new[] { longitude, latitude };
return this;
}
/// <summary>Configures the unit for a metric category id.</summary>
public CategoryBuilder Unit(string id, Unit unit)
{
(_units ??= new Dictionary<string, Unit>())[id] = unit;
return this;
}
internal Category Build()
{
var category = new Category();
if (_index.Count > 0)
{
category.Index = _index.Select((id, i) => (id, i)).ToDictionary(x => x.id, x => x.i);
}
if (_label.Count > 0) category.Label = new Dictionary<string, string>(_label);
if (_child != null) category.Child = new Dictionary<string, string[]>(_child);
if (_coordinates != null) category.Coordinates = new Dictionary<string, double[]>(_coordinates);
if (_units != null) category.Unit = new Dictionary<string, Unit>(_units);
return category;
}
}
/// <summary>Builds a <see cref="Dimension"/> fluently.</summary>
public sealed class DimensionBuilder
{
private string? _label;
private readonly CategoryBuilder _category = new CategoryBuilder();
/// <summary>Sets the dimension label.</summary>
public DimensionBuilder Label(string label)
{
_label = label;
return this;
}
/// <summary>Configures the dimension's categories.</summary>
public DimensionBuilder Category(System.Action<CategoryBuilder> configure)
{
configure(_category);
return this;
}
internal Dimension Build(string id)
{
return new Dimension
{
Label = _label ?? id,
Id = id,
CategoryInfo = _category.Build()
};
}
}
/// <summary>Builds a <see cref="Dataset"/> fluently.</summary>
public sealed class DatasetBuilder
{
private string? _label;
private string? _source;
private string? _updated;
private readonly List<string> _ids = new List<string>();
private readonly Dictionary<string, DimensionBuilder> _dims = new Dictionary<string, DimensionBuilder>();
private Role? _role;
private double?[]? _values;
private Dictionary<string, double?>? _sparse;
private StatusCollection? _status;
/// <summary>Sets the dataset label.</summary>
public DatasetBuilder Label(string label) { _label = label; return this; }
/// <summary>Sets the source.</summary>
public DatasetBuilder Source(string source) { _source = source; return this; }
/// <summary>Sets the update time.</summary>
public DatasetBuilder Updated(string updated) { _updated = updated; return this; }
/// <summary>Adds a dimension by id and configures it.</summary>
public DatasetBuilder Dimension(string id, System.Action<DimensionBuilder> configure)
{
var builder = new DimensionBuilder();
configure(builder);
_ids.Add(id);
_dims[id] = builder;
return this;
}
/// <summary>Configures the dimension roles.</summary>
public DatasetBuilder Role(System.Action<Role> configure)
{
_role ??= new Role();
configure(_role);
return this;
}
/// <summary>Sets the dense values.</summary>
public DatasetBuilder Values(params double?[] values)
{
_values = values;
_sparse = null;
return this;
}
/// <summary>Sets the sparse values (object form).</summary>
public DatasetBuilder SparseValues(IDictionary<string, double?> values)
{
_sparse = new Dictionary<string, double?>(values);
_values = null;
return this;
}
/// <summary>Sets a uniform string status.</summary>
public DatasetBuilder Status(string uniform) { _status = StatusCollection.StringOf(uniform); return this; }
/// <summary>Sets an array status.</summary>
public DatasetBuilder StatusArray(params string?[] values) { _status = StatusCollection.ArrayOf(values); return this; }
/// <summary>Sets an object status.</summary>
public DatasetBuilder StatusObject(IDictionary<string, string> values)
{
_status = StatusCollection.ObjectOf(new Dictionary<string, string>(values));
return this;
}
/// <summary>Builds the dataset (already linked and ready to serialize).</summary>
public Dataset Build()
{
var dataset = new Dataset
{
Version = "2.0",
Class = ResponseClass.Dataset,
Label = _label,
Source = _source,
Updated = _updated
};
dataset.Id = _ids.ToArray();
dataset.DimensionMap = new Dictionary<string, Dimension>();
foreach (var id in _ids)
{
dataset.DimensionMap[id] = _dims[id].Build(id);
}
dataset.Size = _ids
.Select(id => dataset.DimensionMap[id].CategoryInfo.Index?.Count ?? 1)
.ToArray();
dataset.Role = _role;
if (_sparse != null) dataset.Value = ValueCollection.SparseOf(_sparse);
else dataset.Value = _values != null ? ValueCollection.DenseOf(_values) : ValueCollection.Empty();
dataset.Status = _status;
dataset.Wire();
return dataset;
}
/// <summary>Builds a <see cref="Response"/> wrapping the dataset.</summary>
public Response BuildResponse() => new Response
{
Class = ResponseClass.Dataset,
Version = "2.0",
Label = _label,
DatasetValue = Build()
};
}