-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.cs
More file actions
81 lines (65 loc) · 3.03 KB
/
Copy pathResponse.cs
File metadata and controls
81 lines (65 loc) · 3.03 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
namespace JSONstat.Models;
/// <summary>
/// The parsed top-level JSON-stat response. Dispatches to the appropriate
/// class-specific object (<see cref="T:Dataset"/>, <see cref="T:Collection"/>, or
/// <see cref="T:Dimension"/>) and exposes the Toolkit-parity navigation entry
/// points (<c>Dataset</c>, <c>Item</c>).
/// </summary>
public sealed class Response
{
/// <summary>The response class.</summary>
[JsonIgnore]
public ResponseClass Class { get; internal set; }
/// <summary>JSON-stat version.</summary>
[JsonIgnore]
public string? Version { get; internal set; }
/// <summary>Short descriptive text.</summary>
[JsonIgnore]
public string? Label { get; internal set; }
/// <summary>The single dataset for a dataset response.</summary>
internal Dataset? DatasetValue { get; set; }
/// <summary>The collection for a collection response.</summary>
internal Collection? CollectionValue { get; set; }
/// <summary>The dimension for a dimension response.</summary>
internal Dimension? DimensionValue { get; set; }
/// <summary>The parsed collection (collection responses), or <c>null</c>.</summary>
[JsonIgnore]
public Collection? Collection => CollectionValue;
/// <summary>The parsed dimension (dimension responses), or <c>null</c>.</summary>
[JsonIgnore]
public Dimension? Dimension => DimensionValue;
/// <summary>All datasets reachable from this response.</summary>
[JsonIgnore]
public IReadOnlyList<Dataset> Datasets
{
get
{
if (Class == ResponseClass.Dataset && DatasetValue != null) return new[] { DatasetValue };
if (Class == ResponseClass.Collection && CollectionValue != null) return CollectionValue.EmbeddedDatasets;
return Array.Empty<Dataset>();
}
}
/// <summary>Gets the first dataset, or <c>null</c>.</summary>
public Dataset? Dataset() => DatasetValue;
/// <summary>Gets the dataset at <paramref name="index"/>, or <c>null</c>.</summary>
public Dataset? Dataset(int index)
{
var list = Datasets;
return index >= 0 && index < list.Count ? list[index] : null;
}
/// <summary>Gets the dataset whose label matches <paramref name="id"/>, or <c>null</c>.</summary>
public Dataset? Dataset(string id) =>
Datasets.FirstOrDefault(d => string.Equals(d.Label, id, StringComparison.Ordinal));
/// <summary>The collection items (collection responses).</summary>
[JsonIgnore]
public IReadOnlyList<LinkItem> Items => CollectionValue?.Items ?? Array.Empty<LinkItem>();
/// <summary>Gets the collection item at <paramref name="index"/>, or <c>null</c>.</summary>
public LinkItem? Item(int index) => CollectionValue?.Item(index);
/// <summary>Gets the collection items of a given class.</summary>
public IReadOnlyList<LinkItem> ItemsOf(ResponseClass filter) =>
CollectionValue?.ItemsOf(filter) ?? Array.Empty<LinkItem>();
}