Skip to content

Commit

Permalink
use abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
adam.gloyne committed Aug 15, 2024
1 parent cdf9f8b commit df823d7
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 98 deletions.
21 changes: 5 additions & 16 deletions src/LEGO.AsyncAPI.Bindings/Sns/Principal.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Copyright (c) The LEGO Group. All rights reserved.

namespace LEGO.AsyncAPI.Bindings.Sns;

using System;
Expand All @@ -9,20 +7,11 @@ namespace LEGO.AsyncAPI.Bindings.Sns;
using System.Text.Json.Nodes;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Readers.ParseNodes;
using LEGO.AsyncAPI.Writers;

public class Principal : IAsyncApiElement
public abstract class Principal : IAsyncApiElement
{
public Principal(KeyValuePair<string, StringOrStringList> value)
{
this.Value = new PrincipalObject(value);
}

public Principal()
{
this.Value = new PrincipalStar();
}

public IPrincipalValue Value { get; private set; }
public abstract void Serialize(IAsyncApiWriter writer);

public static Principal Parse(ParseNode node)
{
Expand All @@ -36,7 +25,7 @@ public static Principal Parse(ParseNode node)
$"Principal value without a property name can only be a string value of '*'.");
}

return new Principal();
return new PrincipalStar();

case MapNode mapNode:
{
Expand All @@ -51,7 +40,7 @@ public static Principal Parse(ParseNode node)
propertyNode.Name,
StringOrStringList.Parse(propertyNode.Value));

return new Principal(principalValue);
return new PrincipalObject(principalValue);
}

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
// Copyright (c) The LEGO Group. All rights reserved.

namespace LEGO.AsyncAPI.Bindings.Sns;

using System;
using System.Collections.Generic;
using LEGO.AsyncAPI.Writers;

public interface IPrincipalValue
{
void Serialize(IAsyncApiWriter writer);
}

public struct PrincipalObject : IPrincipalValue
public class PrincipalObject : Principal
{
private KeyValuePair<string, StringOrStringList> PrincipalValue;

Expand All @@ -20,7 +13,7 @@ public PrincipalObject(KeyValuePair<string, StringOrStringList> principalValue)
this.PrincipalValue = principalValue;
}

public void Serialize(IAsyncApiWriter writer)
public override void Serialize(IAsyncApiWriter writer)
{
if (writer is null)
{
Expand All @@ -31,24 +24,4 @@ public void Serialize(IAsyncApiWriter writer)
writer.WriteRequiredObject(this.PrincipalValue.Key, this.PrincipalValue.Value, (w, t) => t.Value.Write(w));
writer.WriteEndObject();
}
}

public struct PrincipalStar : IPrincipalValue
{
private string PrincipalValue;

public PrincipalStar()
{
this.PrincipalValue = "*";
}

public void Serialize(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}

writer.WriteValue(this.PrincipalValue);
}
}
24 changes: 24 additions & 0 deletions src/LEGO.AsyncAPI.Bindings/Sns/PrincipalStar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace LEGO.AsyncAPI.Bindings.Sns;

using System;
using LEGO.AsyncAPI.Writers;

public class PrincipalStar : Principal
{
private string PrincipalValue;

public PrincipalStar()
{
this.PrincipalValue = "*";
}

public override void Serialize(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}

writer.WriteValue(this.PrincipalValue);
}
}
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Sns/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Serialize(IAsyncApiWriter writer)

writer.WriteStartObject();
writer.WriteRequiredProperty("effect", this.Effect.GetDisplayName());
writer.WriteRequiredObject("principal", this.Principal, (w, t) => t.Value.Serialize(w));
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));
Expand Down
19 changes: 5 additions & 14 deletions src/LEGO.AsyncAPI.Bindings/Sqs/Principal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@ namespace LEGO.AsyncAPI.Bindings.Sqs;
using System.Text.Json.Nodes;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Readers.ParseNodes;
using LEGO.AsyncAPI.Writers;

public class Principal : IAsyncApiElement
public abstract class Principal : IAsyncApiElement
{
public Principal(KeyValuePair<string, StringOrStringList> value)
{
this.Value = new PrincipalObject(value);
}

public Principal()
{
this.Value = new PrincipalStar();
}

public IPrincipalValue Value { get; private set; }
public abstract void Serialize(IAsyncApiWriter writer);

public static Principal Parse(ParseNode node)
{
Expand All @@ -36,7 +27,7 @@ public static Principal Parse(ParseNode node)
$"Principal value without a property name can only be a string value of '*'.");
}

return new Principal();
return new PrincipalStar();

case MapNode mapNode:
{
Expand All @@ -51,7 +42,7 @@ public static Principal Parse(ParseNode node)
propertyNode.Name,
StringOrStringList.Parse(propertyNode.Value));

return new Principal(principalValue);
return new PrincipalObject(principalValue);
}

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
// Copyright (c) The LEGO Group. All rights reserved.

namespace LEGO.AsyncAPI.Bindings.Sqs;

using System;
using System.Collections.Generic;
using LEGO.AsyncAPI.Writers;

public interface IPrincipalValue
{
void Serialize(IAsyncApiWriter writer);
}

public struct PrincipalObject : IPrincipalValue
public class PrincipalObject : Principal
{
private KeyValuePair<string, StringOrStringList> PrincipalValue;

Expand All @@ -20,7 +13,7 @@ public PrincipalObject(KeyValuePair<string, StringOrStringList> principalValue)
this.PrincipalValue = principalValue;
}

public void Serialize(IAsyncApiWriter writer)
public override void Serialize(IAsyncApiWriter writer)
{
if (writer is null)
{
Expand All @@ -31,24 +24,4 @@ public void Serialize(IAsyncApiWriter writer)
writer.WriteRequiredObject(this.PrincipalValue.Key, this.PrincipalValue.Value, (w, t) => t.Value.Write(w));
writer.WriteEndObject();
}
}

struct PrincipalStar : IPrincipalValue
{
private string PrincipalValue;

public PrincipalStar()
{
this.PrincipalValue = "*";
}

public void Serialize(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}

writer.WriteValue(this.PrincipalValue);
}
}
24 changes: 24 additions & 0 deletions src/LEGO.AsyncAPI.Bindings/Sqs/PrincipalStar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace LEGO.AsyncAPI.Bindings.Sqs;

using System;
using LEGO.AsyncAPI.Writers;

public class PrincipalStar : Principal
{
private string PrincipalValue;

public PrincipalStar()
{
this.PrincipalValue = "*";
}

public override void Serialize(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}

writer.WriteValue(this.PrincipalValue);
}
}
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Bindings/Sqs/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void Serialize(IAsyncApiWriter writer)

writer.WriteStartObject();
writer.WriteRequiredProperty("effect", this.Effect.GetDisplayName());
writer.WriteRequiredObject("principal", this.Principal, (w, t) => t.Value.Serialize(w));
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));
Expand Down
4 changes: 2 additions & 2 deletions test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void SnsChannelBinding_WithFilledObject_SerializesAndDeserializes()
new Statement()
{
Effect = Effect.Deny,
Principal = new Principal(),
Principal = new PrincipalStar(),
Action = new StringOrStringList(new AsyncApiAny(new List<string>()
{
"sns:Publish",
Expand All @@ -105,7 +105,7 @@ public void SnsChannelBinding_WithFilledObject_SerializesAndDeserializes()
new Statement()
{
Effect = Effect.Allow,
Principal = new Principal(new KeyValuePair<string, StringOrStringList>(
Principal = new PrincipalObject(new KeyValuePair<string, StringOrStringList>(
"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")),
Expand Down
12 changes: 6 additions & 6 deletions test/LEGO.AsyncAPI.Tests/Bindings/Sqs/SqsBindings_should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void SqsChannelBinding_WithFilledObject_SerializesAndDeserializes()
new Statement()
{
Effect = Effect.Deny,
Principal = new Principal(new KeyValuePair<string, StringOrStringList>(
Principal = new PrincipalObject(new KeyValuePair<string, StringOrStringList>(
"AWS", new StringOrStringList(new AsyncApiAny("arn:aws:iam::123456789012:user/alex.wichmann")))),
Action = new StringOrStringList(new AsyncApiAny(new List<string>
{
Expand Down Expand Up @@ -166,7 +166,7 @@ public void SqsChannelBinding_WithFilledObject_SerializesAndDeserializes()
new Statement()
{
Effect = Effect.Allow,
Principal = new Principal(new KeyValuePair<string, StringOrStringList>(
Principal = new PrincipalObject(new KeyValuePair<string, StringOrStringList>(
"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")),
Expand Down Expand Up @@ -223,7 +223,7 @@ public void SqsChannelBinding_WithFilledObject_SerializesAndDeserializes()
new Statement()
{
Effect = Effect.Allow,
Principal = new Principal(new KeyValuePair<string, StringOrStringList>(
Principal = new PrincipalObject(new KeyValuePair<string, StringOrStringList>(
"Service", new StringOrStringList(new AsyncApiAny("s3.amazonaws.com")))),
Action = new StringOrStringList(new AsyncApiAny(new List<string>
{
Expand Down Expand Up @@ -377,7 +377,7 @@ public void SqsOperationBinding_WithFilledObject_SerializesAndDeserializes()
new Statement()
{
Effect = Effect.Deny,
Principal = new Principal(new KeyValuePair<string, StringOrStringList>(
Principal = new PrincipalObject(new KeyValuePair<string, StringOrStringList>(
"AWS", new StringOrStringList(new AsyncApiAny("arn:aws:iam::123456789012:user/alex.wichmann")))),
Action = new StringOrStringList(new AsyncApiAny(new List<string>()
{
Expand All @@ -398,7 +398,7 @@ public void SqsOperationBinding_WithFilledObject_SerializesAndDeserializes()
new Statement()
{
Effect = Effect.Allow,
Principal = new Principal(new KeyValuePair<string, StringOrStringList>(
Principal = new PrincipalObject(new KeyValuePair<string, StringOrStringList>(
"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")),
Expand Down Expand Up @@ -446,7 +446,7 @@ public void SqsOperationBinding_WithFilledObject_SerializesAndDeserializes()
new Statement()
{
Effect = Effect.Allow,
Principal = new Principal(new KeyValuePair<string, StringOrStringList>(
Principal = new PrincipalObject(new KeyValuePair<string, StringOrStringList>(
"AWS", new StringOrStringList(new AsyncApiAny("arn:aws:iam::123456789012:user/alex.wichmann")))),
Action = new StringOrStringList(new AsyncApiAny(new List<string>
{
Expand Down

0 comments on commit df823d7

Please sign in to comment.