-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement principal parse logic
- Loading branch information
adam.gloyne
committed
Jul 23, 2024
1 parent
e8f7826
commit c20ad8d
Showing
10 changed files
with
186 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
namespace LEGO.AsyncAPI.Bindings.Sns; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using LEGO.AsyncAPI.Models; | ||
using LEGO.AsyncAPI.Models.Interfaces; | ||
using LEGO.AsyncAPI.Readers.ParseNodes; | ||
|
||
public class Principal : IAsyncApiElement | ||
{ | ||
public Principal(AsyncApiAny value) | ||
{ | ||
this.Value = value; | ||
} | ||
|
||
public AsyncApiAny Value { get; } | ||
|
||
public static Principal Parse(ParseNode node) | ||
{ | ||
switch (node) | ||
{ | ||
case ValueNode: | ||
var nodeValue = node.GetScalarValue(); | ||
if (!IsStarString(nodeValue)) | ||
{ | ||
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " + | ||
$"Principal value without a property name can only be a string value of '*'."); | ||
} | ||
|
||
return new Principal(new AsyncApiAny(nodeValue)); | ||
case MapNode mapNode: | ||
{ | ||
var propertyNode = mapNode.First(); | ||
if (!IsValidPrincipalProperty(propertyNode.Name)) | ||
{ | ||
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " + | ||
$"Node should contain a valid AWS principal property name."); | ||
} | ||
|
||
var parsedObject = new Dictionary<string, AsyncApiAny>() | ||
{ { propertyNode.Name, StringOrStringList.Parse(propertyNode.Value).Value } }; | ||
|
||
return new Principal(new AsyncApiAny(parsedObject)); | ||
} | ||
|
||
default: | ||
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " + | ||
$"Node should contain a string value of '*' or a valid AWS principal property."); | ||
} | ||
} | ||
|
||
private static bool IsStarString(JsonNode value) | ||
{ | ||
var element = JsonDocument.Parse(value.ToJsonString()).RootElement; | ||
|
||
return element.ValueKind == JsonValueKind.String && element.ValueEquals("*"); | ||
} | ||
|
||
private static bool IsValidPrincipalProperty(string property) | ||
{ | ||
return new[] { "AWS", "Service" }.Contains(property); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
namespace LEGO.AsyncAPI.Bindings.Sqs; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using LEGO.AsyncAPI.Models; | ||
using LEGO.AsyncAPI.Models.Interfaces; | ||
using LEGO.AsyncAPI.Readers.ParseNodes; | ||
|
||
public class Principal : IAsyncApiElement | ||
{ | ||
public Principal(AsyncApiAny value) | ||
{ | ||
this.Value = value; | ||
} | ||
|
||
public AsyncApiAny Value { get; } | ||
|
||
public static Principal Parse(ParseNode node) | ||
{ | ||
switch (node) | ||
{ | ||
case ValueNode: | ||
var nodeValue = node.GetScalarValue(); | ||
if (!IsStarString(nodeValue)) | ||
{ | ||
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " + | ||
$"Principal value without a property name can only be a string value of '*'."); | ||
} | ||
|
||
return new Principal(new AsyncApiAny(nodeValue)); | ||
case MapNode mapNode: | ||
{ | ||
var propertyNode = mapNode.First(); | ||
if (!IsValidPrincipalProperty(propertyNode.Name)) | ||
{ | ||
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " + | ||
$"Node should contain a valid AWS principal property name."); | ||
} | ||
|
||
var parsedObject = new Dictionary<string, AsyncApiAny>() | ||
{ { propertyNode.Name, StringOrStringList.Parse(propertyNode.Value).Value } }; | ||
|
||
return new Principal(new AsyncApiAny(parsedObject)); | ||
} | ||
|
||
default: | ||
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " + | ||
$"Node should contain a string value of '*' or a valid AWS principal property."); | ||
} | ||
} | ||
|
||
private static bool IsStarString(JsonNode value) | ||
{ | ||
var element = JsonDocument.Parse(value.ToJsonString()).RootElement; | ||
|
||
return element.ValueKind == JsonValueKind.String && element.ValueEquals("*"); | ||
} | ||
|
||
private static bool IsValidPrincipalProperty(string property) | ||
{ | ||
return new[] { "AWS", "Service" }.Contains(property); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.