Skip to content

Commit

Permalink
Merge branch 'main' into vnext
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed Sep 4, 2024
2 parents 25875cf + 030d603 commit 37ee652
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 19 deletions.
67 changes: 67 additions & 0 deletions src/LEGO.AsyncAPI.Bindings/Sns/Condition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.");
}
}
}
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Sns/SnsChannelBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class SnsChannelBinding : ChannelBinding<SnsChannelBinding>
{ "principal", (a, n) => { a.Principal = Principal.Parse(n); } },
{ "action", (a, n) => { a.Action = StringOrStringList.Parse(n); } },
{ "resource", (a, n) => { a.Resource = StringOrStringList.Parse(n); } },
{ "condition", (a, n) => { a.Condition = n.CreateAny(); } },
{ "condition", (a, n) => { a.Condition = Condition.Parse(n); } },
};

/// <inheritdoc/>
Expand Down
4 changes: 2 additions & 2 deletions src/LEGO.AsyncAPI.Bindings/Sns/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Statement : IAsyncApiExtensible
/// <summary>
/// Specific circumstances under which the policy grants permission.
/// </summary>
public AsyncApiAny? Condition { get; set; }
public Condition Condition { get; set; }

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

Expand All @@ -49,7 +49,7 @@ public void Serialize(IAsyncApiWriter writer)
writer.WriteRequiredObject("principal", this.Principal, (w, t) => t.Serialize(w));
writer.WriteRequiredObject("action", this.Action, (w, t) => t.Value.Write(w));
writer.WriteOptionalObject("resource", this.Resource, (w, t) => t?.Value.Write(w));
writer.WriteOptionalObject("condition", this.Condition, (w, t) => t?.Write(w));
writer.WriteOptionalObject("condition", this.Condition, (w, t) => t.Serialize(w));
writer.WriteExtensions(this.Extensions);
writer.WriteEndObject();
}
Expand Down
67 changes: 67 additions & 0 deletions src/LEGO.AsyncAPI.Bindings/Sqs/Condition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) The LEGO Group. All rights reserved.

namespace LEGO.AsyncAPI.Bindings.Sqs;

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 AWS condition types.");
}
}
}
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Sqs/SqsChannelBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class SqsChannelBinding : ChannelBinding<SqsChannelBinding>
{ "principal", (a, n) => { a.Principal = Principal.Parse(n); } },
{ "action", (a, n) => { a.Action = StringOrStringList.Parse(n); } },
{ "resource", (a, n) => { a.Resource = StringOrStringList.Parse(n); } },
{ "condition", (a, n) => { a.Condition = n.CreateAny(); } },
{ "condition", (a, n) => { a.Condition = Condition.Parse(n); } },
};

public override void SerializeProperties(IAsyncApiWriter writer)
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Sqs/SqsOperationBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class SqsOperationBinding : OperationBinding<SqsOperationBinding>
{ "principal", (a, n) => { a.Principal = Principal.Parse(n); } },
{ "action", (a, n) => { a.Action = StringOrStringList.Parse(n); } },
{ "resource", (a, n) => { a.Resource = StringOrStringList.Parse(n); } },
{ "condition", (a, n) => { a.Condition = n.CreateAny(); } },
{ "condition", (a, n) => { a.Condition = Condition.Parse(n); } },
};

public override void SerializeProperties(IAsyncApiWriter writer)
Expand Down
4 changes: 2 additions & 2 deletions src/LEGO.AsyncAPI.Bindings/Sqs/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Statement : IAsyncApiExtensible
/// <summary>
/// Specific circumstances under which the policy grants permission.
/// </summary>
public AsyncApiAny? Condition { get; set; }
public Condition Condition { get; set; }

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

Expand All @@ -50,7 +50,7 @@ public void Serialize(IAsyncApiWriter writer)
writer.WriteRequiredObject("principal", this.Principal, (w, t) => t.Serialize(w));
writer.WriteRequiredObject("action", this.Action, (w, t) => t.Value.Write(w));
writer.WriteOptionalObject("resource", this.Resource, (w, t) => t?.Value.Write(w));
writer.WriteOptionalObject("condition", this.Condition, (w, t) => t?.Write(w));
writer.WriteOptionalObject("condition", this.Condition, (w, t) => t.Serialize(w));
writer.WriteExtensions(this.Extensions);
writer.WriteEndObject();
}
Expand Down
16 changes: 10 additions & 6 deletions test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ public void SnsChannelBinding_WithFilledObject_SerializesAndDeserializes()
"sns:Publish",
"sns:Delete",
})),
Condition = new AsyncApiAny(new Dictionary<string, object>()
Condition = new Condition(new Dictionary<string, Dictionary<string, StringOrStringList>>
{
{
"StringEquals", new Dictionary<string, List<string>>()
"StringEquals", new Dictionary<string, StringOrStringList>
{
{ "aws:username", new List<string>() { "johndoe", "mrsmith" } },
{
"aws:username", new StringOrStringList(new AsyncApiAny(new List<string>() { "johndoe", "mrsmith" }))
},
}
},
}),
Expand All @@ -109,12 +111,14 @@ public void SnsChannelBinding_WithFilledObject_SerializesAndDeserializes()
"AWS", new StringOrStringList(new AsyncApiAny(new List<string>
{ "arn:aws:iam::123456789012:user/alex.wichmann", "arn:aws:iam::123456789012:user/dec.kolakowski" })))),
Action = new StringOrStringList(new AsyncApiAny("sns:Create")),
Condition = new AsyncApiAny(new Dictionary<string, object>()
Condition = new Condition(new Dictionary<string, Dictionary<string, StringOrStringList>>
{
{
"NumericLessThanEquals", new Dictionary<string, string>()
"NumericLessThanEquals", new Dictionary<string, StringOrStringList>
{
{ "aws:MultiFactorAuthAge", "3600" },
{
"aws:MultiFactorAuthAge", new StringOrStringList(new AsyncApiAny("3600"))
},
}
},
}),
Expand Down
16 changes: 10 additions & 6 deletions test/LEGO.AsyncAPI.Tests/Bindings/Sqs/SqsBindings_should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,14 @@ public void SqsChannelBinding_WithFilledObject_SerializesAndDeserializes()
"sqs:SendMessage",
"sqs:ReceiveMessage",
})),
Condition = new AsyncApiAny(new Dictionary<string, object>()
Condition = new Condition(new Dictionary<string, Dictionary<string, StringOrStringList>>
{
{
"StringEquals", new Dictionary<string, List<string>>()
"StringEquals", new Dictionary<string, StringOrStringList>
{
{ "aws:username", new List<string>() { "johndoe", "mrsmith" } },
{
"aws:username", new StringOrStringList(new AsyncApiAny(new List<string> { "johndoe", "mrsmith" }))
},
}
},
}),
Expand All @@ -170,12 +172,14 @@ public void SqsChannelBinding_WithFilledObject_SerializesAndDeserializes()
"AWS", new StringOrStringList(new AsyncApiAny(new List<string>
{ "arn:aws:iam::123456789012:user/alex.wichmann", "arn:aws:iam::123456789012:user/dec.kolakowski" })))),
Action = new StringOrStringList(new AsyncApiAny("sqs:CreateQueue")),
Condition = new AsyncApiAny(new Dictionary<string, object>()
Condition = new Condition(new Dictionary<string, Dictionary<string, StringOrStringList>>
{
{
"NumericLessThanEquals", new Dictionary<string, string>()
"NumericLessThanEquals", new Dictionary<string, StringOrStringList>
{
{ "aws:MultiFactorAuthAge", "3600" },
{
"aws:MultiFactorAuthAge", new StringOrStringList(new AsyncApiAny("3600"))
},
}
},
}),
Expand Down

0 comments on commit 37ee652

Please sign in to comment.