-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetInputNodeById.cs
56 lines (48 loc) · 1.75 KB
/
GetInputNodeById.cs
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
using System.Text.Json;
using System.Text.Json.Nodes;
namespace SmallsOnline.VSCode.Configurator.External;
public static partial class VSCodeOperations
{
/// <summary>
/// Gets an input node, in the 'inputs' property, based off of the 'id' property.
/// </summary>
/// <param name="inputsNode">The <see cref="JsonNode"/> representing the 'inputs' property.</param>
/// <param name="id">The value of the 'id' property to search for.</param>
/// <returns>The <see cref="JsonNode"/> representing the input.</returns>
/// <exception cref="InvalidOperationException">An issue occurred while searching for the input.</exception>
private static JsonNode GetInputNodeById(this JsonNode? inputsNode, string id)
{
if (inputsNode is null)
{
throw new InvalidOperationException("The inputs property is missing.");
}
if (inputsNode.GetValueKind() != JsonValueKind.Array)
{
throw new InvalidOperationException("The inputs property is not an array.");
}
try
{
foreach (var inputElement in inputsNode.AsArray())
{
JsonNode? inputIdProperty = inputElement!["id"];
if (inputIdProperty is null)
{
continue;
}
if (inputIdProperty.GetValueKind() != JsonValueKind.String)
{
continue;
}
if (inputIdProperty.GetValue<string>() == id)
{
return inputElement;
}
}
throw new InvalidOperationException("The projectItem input is missing.");
}
catch (Exception)
{
throw;
}
}
}