Repository navigation
Expand file tree
/
Copy pathCondition.cs
More file actions
67 lines (57 loc) · 2.36 KB
/
Copy pathCondition.cs
File metadata and controls
67 lines (57 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
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI.Bindings.Sns;
using System;
using System.Collections.Generic;
using System.Linq;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Readers.ParseNodes;
using LEGO.AsyncAPI.Writers;
public class Condition : IAsyncApiElement
{
public Dictionary<string, Dictionary<string, StringOrStringList>> Value { get; private set; }
public Condition(Dictionary<string, Dictionary<string, StringOrStringList>> value)
{
this.Value = value;
}
public void Serialize(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
writer.WriteStartObject();
foreach (var conditionValue in this.Value)
{
writer.WriteRequiredMap(conditionValue.Key, conditionValue.Value, (w, t) => t.Value.Write(w));
}
writer.WriteEndObject();
}
public static Condition Parse(ParseNode node)
{
switch (node)
{
case MapNode mapNode:
{
var conditionValues = new Dictionary<string, Dictionary<string, StringOrStringList>>();
foreach (var conditionNode in mapNode)
{
switch (conditionNode.Value)
{
case MapNode conditionValueNode:
conditionValues.Add(conditionNode.Name, new Dictionary<string, StringOrStringList>(conditionValueNode.Select(x =>
new KeyValuePair<string, StringOrStringList>(x.Name, StringOrStringList.Parse(x.Value)))
.ToDictionary(x => x.Key, x => x.Value)));
break;
default:
throw new ArgumentException($"An error occured while parsing a {nameof(Condition)} node. " +
$"AWS condition values should be one or more key value pairs.");
}
}
return new Condition(conditionValues);
}
default:
throw new ArgumentException($"An error occured while parsing a {nameof(Condition)} node. " +
$"Node should contain a collection of condition types.");
}
}
}