Skip to content

Commit 030d603

Browse files
Gadam8adam.gloyne
andauthored
chore: make aws condition value explicit (#193)
Co-authored-by: adam.gloyne <[email protected]>
1 parent 57c0c33 commit 030d603

File tree

9 files changed

+161
-19
lines changed

9 files changed

+161
-19
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) The LEGO Group. All rights reserved.
2+
3+
namespace LEGO.AsyncAPI.Bindings.Sns;
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using LEGO.AsyncAPI.Models.Interfaces;
9+
using LEGO.AsyncAPI.Readers.ParseNodes;
10+
using LEGO.AsyncAPI.Writers;
11+
12+
public class Condition : IAsyncApiElement
13+
{
14+
public Dictionary<string, Dictionary<string, StringOrStringList>> Value { get; private set; }
15+
16+
public Condition(Dictionary<string, Dictionary<string, StringOrStringList>> value)
17+
{
18+
this.Value = value;
19+
}
20+
21+
public void Serialize(IAsyncApiWriter writer)
22+
{
23+
if (writer is null)
24+
{
25+
throw new ArgumentNullException(nameof(writer));
26+
}
27+
28+
writer.WriteStartObject();
29+
foreach (var conditionValue in this.Value)
30+
{
31+
writer.WriteRequiredMap(conditionValue.Key, conditionValue.Value, (w, t) => t.Value.Write(w));
32+
}
33+
34+
writer.WriteEndObject();
35+
}
36+
37+
public static Condition Parse(ParseNode node)
38+
{
39+
switch (node)
40+
{
41+
case MapNode mapNode:
42+
{
43+
var conditionValues = new Dictionary<string, Dictionary<string, StringOrStringList>>();
44+
foreach (var conditionNode in mapNode)
45+
{
46+
switch (conditionNode.Value)
47+
{
48+
case MapNode conditionValueNode:
49+
conditionValues.Add(conditionNode.Name, new Dictionary<string, StringOrStringList>(conditionValueNode.Select(x =>
50+
new KeyValuePair<string, StringOrStringList>(x.Name, StringOrStringList.Parse(x.Value)))
51+
.ToDictionary(x => x.Key, x => x.Value)));
52+
break;
53+
default:
54+
throw new ArgumentException($"An error occured while parsing a {nameof(Condition)} node. " +
55+
$"AWS condition values should be one or more key value pairs.");
56+
}
57+
}
58+
59+
return new Condition(conditionValues);
60+
}
61+
62+
default:
63+
throw new ArgumentException($"An error occured while parsing a {nameof(Condition)} node. " +
64+
$"Node should contain a collection of condition types.");
65+
}
66+
}
67+
}

src/LEGO.AsyncAPI.Bindings/Sns/SnsChannelBinding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SnsChannelBinding : ChannelBinding<SnsChannelBinding>
6060
{ "principal", (a, n) => { a.Principal = Principal.Parse(n); } },
6161
{ "action", (a, n) => { a.Action = StringOrStringList.Parse(n); } },
6262
{ "resource", (a, n) => { a.Resource = StringOrStringList.Parse(n); } },
63-
{ "condition", (a, n) => { a.Condition = n.CreateAny(); } },
63+
{ "condition", (a, n) => { a.Condition = Condition.Parse(n); } },
6464
};
6565

6666
/// <inheritdoc/>

src/LEGO.AsyncAPI.Bindings/Sns/Statement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class Statement : IAsyncApiExtensible
3333
/// <summary>
3434
/// Specific circumstances under which the policy grants permission.
3535
/// </summary>
36-
public AsyncApiAny? Condition { get; set; }
36+
public Condition Condition { get; set; }
3737

3838
public IDictionary<string, IAsyncApiExtension> Extensions { get; set; } = new Dictionary<string, IAsyncApiExtension>();
3939

@@ -49,7 +49,7 @@ public void Serialize(IAsyncApiWriter writer)
4949
writer.WriteRequiredObject("principal", this.Principal, (w, t) => t.Serialize(w));
5050
writer.WriteRequiredObject("action", this.Action, (w, t) => t.Value.Write(w));
5151
writer.WriteOptionalObject("resource", this.Resource, (w, t) => t?.Value.Write(w));
52-
writer.WriteOptionalObject("condition", this.Condition, (w, t) => t?.Write(w));
52+
writer.WriteOptionalObject("condition", this.Condition, (w, t) => t.Serialize(w));
5353
writer.WriteExtensions(this.Extensions);
5454
writer.WriteEndObject();
5555
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) The LEGO Group. All rights reserved.
2+
3+
namespace LEGO.AsyncAPI.Bindings.Sqs;
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using LEGO.AsyncAPI.Models.Interfaces;
9+
using LEGO.AsyncAPI.Readers.ParseNodes;
10+
using LEGO.AsyncAPI.Writers;
11+
12+
public class Condition : IAsyncApiElement
13+
{
14+
public Dictionary<string, Dictionary<string, StringOrStringList>> Value { get; private set; }
15+
16+
public Condition(Dictionary<string, Dictionary<string, StringOrStringList>> value)
17+
{
18+
this.Value = value;
19+
}
20+
21+
public void Serialize(IAsyncApiWriter writer)
22+
{
23+
if (writer is null)
24+
{
25+
throw new ArgumentNullException(nameof(writer));
26+
}
27+
28+
writer.WriteStartObject();
29+
foreach (var conditionValue in this.Value)
30+
{
31+
writer.WriteRequiredMap(conditionValue.Key, conditionValue.Value, (w, t) => t.Value.Write(w));
32+
}
33+
34+
writer.WriteEndObject();
35+
}
36+
37+
public static Condition Parse(ParseNode node)
38+
{
39+
switch (node)
40+
{
41+
case MapNode mapNode:
42+
{
43+
var conditionValues = new Dictionary<string, Dictionary<string, StringOrStringList>>();
44+
foreach (var conditionNode in mapNode)
45+
{
46+
switch (conditionNode.Value)
47+
{
48+
case MapNode conditionValueNode:
49+
conditionValues.Add(conditionNode.Name, new Dictionary<string, StringOrStringList>(conditionValueNode.Select(x =>
50+
new KeyValuePair<string, StringOrStringList>(x.Name, StringOrStringList.Parse(x.Value)))
51+
.ToDictionary(x => x.Key, x => x.Value)));
52+
break;
53+
default:
54+
throw new ArgumentException($"An error occured while parsing a {nameof(Condition)} node. " +
55+
$"AWS condition values should be one or more key value pairs.");
56+
}
57+
}
58+
59+
return new Condition(conditionValues);
60+
}
61+
62+
default:
63+
throw new ArgumentException($"An error occured while parsing a {nameof(Condition)} node. " +
64+
$"Node should contain a collection of AWS condition types.");
65+
}
66+
}
67+
}

src/LEGO.AsyncAPI.Bindings/Sqs/SqsChannelBinding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class SqsChannelBinding : ChannelBinding<SqsChannelBinding>
6767
{ "principal", (a, n) => { a.Principal = Principal.Parse(n); } },
6868
{ "action", (a, n) => { a.Action = StringOrStringList.Parse(n); } },
6969
{ "resource", (a, n) => { a.Resource = StringOrStringList.Parse(n); } },
70-
{ "condition", (a, n) => { a.Condition = n.CreateAny(); } },
70+
{ "condition", (a, n) => { a.Condition = Condition.Parse(n); } },
7171
};
7272

7373
public override void SerializeProperties(IAsyncApiWriter writer)

src/LEGO.AsyncAPI.Bindings/Sqs/SqsOperationBinding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class SqsOperationBinding : OperationBinding<SqsOperationBinding>
5959
{ "principal", (a, n) => { a.Principal = Principal.Parse(n); } },
6060
{ "action", (a, n) => { a.Action = StringOrStringList.Parse(n); } },
6161
{ "resource", (a, n) => { a.Resource = StringOrStringList.Parse(n); } },
62-
{ "condition", (a, n) => { a.Condition = n.CreateAny(); } },
62+
{ "condition", (a, n) => { a.Condition = Condition.Parse(n); } },
6363
};
6464

6565
public override void SerializeProperties(IAsyncApiWriter writer)

src/LEGO.AsyncAPI.Bindings/Sqs/Statement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class Statement : IAsyncApiExtensible
3434
/// <summary>
3535
/// Specific circumstances under which the policy grants permission.
3636
/// </summary>
37-
public AsyncApiAny? Condition { get; set; }
37+
public Condition Condition { get; set; }
3838

3939
public IDictionary<string, IAsyncApiExtension> Extensions { get; set; } = new Dictionary<string, IAsyncApiExtension>();
4040

@@ -50,7 +50,7 @@ public void Serialize(IAsyncApiWriter writer)
5050
writer.WriteRequiredObject("principal", this.Principal, (w, t) => t.Serialize(w));
5151
writer.WriteRequiredObject("action", this.Action, (w, t) => t.Value.Write(w));
5252
writer.WriteOptionalObject("resource", this.Resource, (w, t) => t?.Value.Write(w));
53-
writer.WriteOptionalObject("condition", this.Condition, (w, t) => t?.Write(w));
53+
writer.WriteOptionalObject("condition", this.Condition, (w, t) => t.Serialize(w));
5454
writer.WriteExtensions(this.Extensions);
5555
writer.WriteEndObject();
5656
}

test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ public void SnsChannelBinding_WithFilledObject_SerializesAndDeserializes()
9292
"sns:Publish",
9393
"sns:Delete",
9494
})),
95-
Condition = new AsyncApiAny(new Dictionary<string, object>()
95+
Condition = new Condition(new Dictionary<string, Dictionary<string, StringOrStringList>>
9696
{
9797
{
98-
"StringEquals", new Dictionary<string, List<string>>()
98+
"StringEquals", new Dictionary<string, StringOrStringList>
9999
{
100-
{ "aws:username", new List<string>() { "johndoe", "mrsmith" } },
100+
{
101+
"aws:username", new StringOrStringList(new AsyncApiAny(new List<string>() { "johndoe", "mrsmith" }))
102+
},
101103
}
102104
},
103105
}),
@@ -109,12 +111,14 @@ public void SnsChannelBinding_WithFilledObject_SerializesAndDeserializes()
109111
"AWS", new StringOrStringList(new AsyncApiAny(new List<string>
110112
{ "arn:aws:iam::123456789012:user/alex.wichmann", "arn:aws:iam::123456789012:user/dec.kolakowski" })))),
111113
Action = new StringOrStringList(new AsyncApiAny("sns:Create")),
112-
Condition = new AsyncApiAny(new Dictionary<string, object>()
114+
Condition = new Condition(new Dictionary<string, Dictionary<string, StringOrStringList>>
113115
{
114116
{
115-
"NumericLessThanEquals", new Dictionary<string, string>()
117+
"NumericLessThanEquals", new Dictionary<string, StringOrStringList>
116118
{
117-
{ "aws:MultiFactorAuthAge", "3600" },
119+
{
120+
"aws:MultiFactorAuthAge", new StringOrStringList(new AsyncApiAny("3600"))
121+
},
118122
}
119123
},
120124
}),

test/LEGO.AsyncAPI.Tests/Bindings/Sqs/SqsBindings_should.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ public void SqsChannelBinding_WithFilledObject_SerializesAndDeserializes()
143143
"sqs:SendMessage",
144144
"sqs:ReceiveMessage",
145145
})),
146-
Condition = new AsyncApiAny(new Dictionary<string, object>()
146+
Condition = new Condition(new Dictionary<string, Dictionary<string, StringOrStringList>>
147147
{
148148
{
149-
"StringEquals", new Dictionary<string, List<string>>()
149+
"StringEquals", new Dictionary<string, StringOrStringList>
150150
{
151-
{ "aws:username", new List<string>() { "johndoe", "mrsmith" } },
151+
{
152+
"aws:username", new StringOrStringList(new AsyncApiAny(new List<string> { "johndoe", "mrsmith" }))
153+
},
152154
}
153155
},
154156
}),
@@ -170,12 +172,14 @@ public void SqsChannelBinding_WithFilledObject_SerializesAndDeserializes()
170172
"AWS", new StringOrStringList(new AsyncApiAny(new List<string>
171173
{ "arn:aws:iam::123456789012:user/alex.wichmann", "arn:aws:iam::123456789012:user/dec.kolakowski" })))),
172174
Action = new StringOrStringList(new AsyncApiAny("sqs:CreateQueue")),
173-
Condition = new AsyncApiAny(new Dictionary<string, object>()
175+
Condition = new Condition(new Dictionary<string, Dictionary<string, StringOrStringList>>
174176
{
175177
{
176-
"NumericLessThanEquals", new Dictionary<string, string>()
178+
"NumericLessThanEquals", new Dictionary<string, StringOrStringList>
177179
{
178-
{ "aws:MultiFactorAuthAge", "3600" },
180+
{
181+
"aws:MultiFactorAuthAge", new StringOrStringList(new AsyncApiAny("3600"))
182+
},
179183
}
180184
},
181185
}),

0 commit comments

Comments
 (0)