Skip to content

Commit

Permalink
refactor: privatize the avro schema property and add helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed Aug 12, 2024
1 parent a9d501d commit 8598935
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
25 changes: 19 additions & 6 deletions src/LEGO.AsyncAPI/Models/AsyncApiAvroSchemaPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,43 @@ namespace LEGO.AsyncAPI.Models
{
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;
using System;

Check warning on line 7 in src/LEGO.AsyncAPI/Models/AsyncApiAvroSchemaPayload.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Using directive for 'System' should appear before directive for 'LEGO.AsyncAPI.Writers' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1208.md)

Check warning on line 7 in src/LEGO.AsyncAPI/Models/AsyncApiAvroSchemaPayload.cs

View workflow job for this annotation

GitHub Actions / Publish NuGet packages (LEGO.AsyncAPI.Readers)

Using directive for 'System' should appear before directive for 'LEGO.AsyncAPI.Writers' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1208.md)

Check warning on line 7 in src/LEGO.AsyncAPI/Models/AsyncApiAvroSchemaPayload.cs

View workflow job for this annotation

GitHub Actions / Publish NuGet packages (LEGO.AsyncAPI)

Using directive for 'System' should appear before directive for 'LEGO.AsyncAPI.Writers' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1208.md)

Check warning on line 7 in src/LEGO.AsyncAPI/Models/AsyncApiAvroSchemaPayload.cs

View workflow job for this annotation

GitHub Actions / Publish NuGet packages (LEGO.AsyncAPI.Bindings)

Using directive for 'System' should appear before directive for 'LEGO.AsyncAPI.Writers' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1208.md)

Check warning on line 7 in src/LEGO.AsyncAPI/Models/AsyncApiAvroSchemaPayload.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Using directive for 'System' should appear before directive for 'LEGO.AsyncAPI.Writers' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1208.md)

public class AsyncApiAvroSchemaPayload : IAsyncApiMessagePayload
{
public AvroSchema Schema { get; set; }
private readonly AvroSchema schema;

public AsyncApiAvroSchemaPayload(AvroSchema schema)
{
this.Schema = schema;
this.schema = schema;
}

public AsyncApiAvroSchemaPayload()
{
this.schema = new AvroRecord();
}

public bool TryGetAs<T>(out T schema)
where T : AvroSchema
{
schema = this.Schema as T;
schema = this.schema as T;
return schema != null;
}

public bool UnresolvedReference { get => this.Schema.UnresolvedReference; set => this.Schema.UnresolvedReference = value; }
public bool Is<T>()
where T : AvroSchema
{
return this.schema is T;
}

public Type GetSchemaType()
{
return this.schema.GetType();
}

public bool UnresolvedReference { get => this.schema.UnresolvedReference; set => this.schema.UnresolvedReference = value; }

public AsyncApiReference Reference { get => this.Schema.Reference; set => this.Schema.Reference = value; }
public AsyncApiReference Reference { get => this.schema.Reference; set => this.schema.Reference = value; }

public void SerializeV2(IAsyncApiWriter writer)
{
Expand All @@ -47,7 +60,7 @@ public void SerializeV2(IAsyncApiWriter writer)

public void SerializeV2WithoutReference(IAsyncApiWriter writer)
{
this.Schema.SerializeV2(writer);
this.schema.SerializeV2(writer);
}
}
}
24 changes: 11 additions & 13 deletions test/LEGO.AsyncAPI.Tests/Models/AsyncApiMessage_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,25 +189,23 @@ public void AsyncApiMessage_WithAvroSchemaFormat_Serializes()

var message = new AsyncApiMessage();
message.SchemaFormat = "application/vnd.apache.avro";
message.Payload = new AsyncApiAvroSchemaPayload()
var schema = new AvroRecord()
{
Schema = new AvroRecord()
Name = "User",
Namespace = "com.example",
Fields = new List<AvroField>
{
Name = "User",
Namespace = "com.example",
Fields = new List<AvroField>
new AvroField()
{
new AvroField()
{
Name = "username",
Type = AvroPrimitiveType.String,
Doc = "The username of the user.",
Default = new AsyncApiAny("guest"),
Order = AvroFieldOrder.Ascending,
},
Name = "username",
Type = AvroPrimitiveType.String,
Doc = "The username of the user.",
Default = new AsyncApiAny("guest"),
Order = AvroFieldOrder.Ascending,
},
},
};
message.Payload = new AsyncApiAvroSchemaPayload(schema);

// Act
var actual = message.SerializeAsYaml(AsyncApiVersion.AsyncApi2_0);
Expand Down

0 comments on commit 8598935

Please sign in to comment.