-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathListNode.cs
More file actions
79 lines (67 loc) · 2.36 KB
/
ListNode.cs
File metadata and controls
79 lines (67 loc) · 2.36 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
namespace ByteBard.AsyncAPI.Readers.ParseNodes
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Readers.Exceptions;
public class ListNode : ParseNode, IEnumerable<ParseNode>
{
private readonly JsonArray nodeList;
public ListNode(ParsingContext context, JsonArray sequenceNode)
: base(
context)
{
this.nodeList = sequenceNode;
}
public override List<T> CreateList<T>(Func<MapNode, T> map)
{
if (this.nodeList == null)
{
throw new AsyncApiReaderException(
$"Expected list while parsing {typeof(T).Name}");
}
return this.nodeList.Select(n => map(new MapNode(this.Context, n as JsonObject)))
.Where(i => i != null)
.ToList();
}
public override List<AsyncApiAny> CreateListOfAny()
{
return this.nodeList.Select(n => ParseNode.Create(this.Context, n).CreateAny())
.Where(i => i != null)
.ToList();
}
public override List<T> CreateSimpleList<T>(Func<ValueNode, T> map)
{
if (this.nodeList == null)
{
throw new AsyncApiReaderException(
$"Expected list while parsing {typeof(T).Name}");
}
return this.nodeList.Select(n => map(new ValueNode(this.Context, n))).ToList();
}
public override HashSet<T> CreateSimpleSet<T>(Func<ValueNode, T> map)
{
if (this.nodeList == null)
{
throw new AsyncApiReaderException(
$"Expected list while parsing {typeof(T).Name}");
}
return this.nodeList.Select(n => map(new ValueNode(this.Context, n))).ToHashSet();
}
public IEnumerator<ParseNode> GetEnumerator()
{
return this.nodeList.Select(n => Create(this.Context, n)).ToList().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public override AsyncApiAny CreateAny()
{
return new AsyncApiAny(this.nodeList);
}
}
}