Skip to content

Commit

Permalink
mas
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed May 30, 2024
1 parent f2053e9 commit 799cf93
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 51 deletions.
53 changes: 19 additions & 34 deletions src/LEGO.AsyncAPI/Models/AsyncApiAvroSchemaPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,40 @@ namespace LEGO.AsyncAPI.Models

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

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

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

/// <summary>
/// The type of the schema. See <a href="https://avro.apache.org/docs/1.9.0/spec.html#schema_primitive">Avro Schema Types</a>.
/// </summary>
public AvroSchemaType Type { get => this.schema.Type; set => this.schema.Type = value; }

/// <summary>
/// The name of the schema. Required for named types (e.g., record, enum, fixed). See <a href="https://avro.apache.org/docs/1.9.0/spec.html#names">Avro Names</a>.
/// </summary>
public string Name { get => this.schema.Name; set => this.schema.Name = value; }

/// <summary>
/// The namespace of the schema. Useful for named types to avoid name conflicts.
/// </summary>
public string? Namespace { get => this.schema.Namespace; set => this.schema.Namespace = value; }

/// <summary>
/// Documentation for the schema.
/// </summary>
public string? Doc { get => this.schema.Doc; set => this.schema.Doc = value; }

/// <summary>
/// The list of fields in the schema.
/// </summary>
public IList<AvroField> Fields { get => this.schema.Fields; set => this.schema.Fields = value; }

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

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

public void SerializeV2(IAsyncApiWriter writer)
{
this.schema.SerializeV2(writer);
var settings = writer.GetSettings();

if (this.Reference != null)
{
if (!settings.ShouldInlineReference(this.Reference))
{
this.Reference.SerializeV2(writer);
return;
}
}

this.SerializeV2WithoutReference(writer);
}

public void SerializeV2WithoutReference(IAsyncApiWriter writer)
{
this.schema.SerializeV2WithoutReference(writer);
this.Schema.SerializeV2(writer);
}
}
}
24 changes: 23 additions & 1 deletion src/LEGO.AsyncAPI/Models/Avro/AvroArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@

namespace LEGO.AsyncAPI.Models
{
using System.Collections.Generic;
using System.Linq;
using LEGO.AsyncAPI.Writers;

public class AvroArray : AvroSchema
{
public string Type { get; } = "array";
public override string Type { get; } = "array";

public AvroSchema Items { get; set; }

/// <summary>
/// A map of properties not in the schema, but added as additional metadata.
/// </summary>
public IDictionary<string, AvroSchema> Metadata { get; set; } = new Dictionary<string, AvroSchema>();

public override void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WriteRequiredObject("items", this.Items, (w, f) => f.SerializeV2(w));
if (this.Metadata.Any())
{
foreach (var item in this.Metadata)
{
writer.WritePropertyName(item.Key);
if (item.Value == null)
{
writer.WriteNull();
}
else
{
item.Value.SerializeV2(writer);
}
}
}
writer.WriteEndObject();
}
}
Expand Down
27 changes: 24 additions & 3 deletions src/LEGO.AsyncAPI/Models/Avro/AvroEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace LEGO.AsyncAPI.Models
{
using System.Collections.Generic;
using System.Linq;
using LEGO.AsyncAPI.Writers;

public class AvroEnum : AvroSchema
{
public string Type { get; } = "enum";
public override string Type { get; } = "enum";

public string Name { get; set; }

public string Namespace { get; set; }

public string Doc { get; set; }
Expand All @@ -21,16 +22,36 @@ public class AvroEnum : AvroSchema

public string Default { get; set; }

/// <summary>
/// A map of properties not in the schema, but added as additional metadata.
/// </summary>
public IDictionary<string, AvroSchema> Metadata { get; set; } = new Dictionary<string, AvroSchema>();

public override void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WriteRequiredProperty("name", this.Name);
writer.WriteRequiredProperty("namespace", this.Namespace);
writer.WriteOptionalProperty("namespace", this.Namespace);
writer.WriteOptionalCollection("aliases", this.Aliases, (w, s) => w.WriteValue(s));
writer.WriteOptionalProperty("doc", this.Doc);
writer.WriteRequiredCollection("symbols", this.Symbols, (w, s) => w.WriteValue(s));
writer.WriteRequiredProperty("default", this.Default);
if (this.Metadata.Any())
{
foreach (var item in this.Metadata)
{
writer.WritePropertyName(item.Key);
if (item.Value == null)
{
writer.WriteNull();
}
else
{
item.Value.SerializeV2(writer);
}
}
}
writer.WriteEndObject();
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/LEGO.AsyncAPI/Models/Avro/AvroField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace LEGO.AsyncAPI.Models
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;
using System.Collections.Generic;

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

View workflow job for this annotation

GitHub Actions / build (macos-latest)

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

/// <summary>
/// Represents a field within an Avro record schema.
Expand Down Expand Up @@ -41,6 +42,11 @@ public class AvroField : IAsyncApiSerializable
/// </summary>
public IList<string> Aliases { get; set; } = new List<string>();

/// <summary>
/// A map of properties not in the schema, but added as additional metadata.
/// </summary>
public IDictionary<string, AvroSchema> Metadata { get; set; } = new Dictionary<string, AvroSchema>();

public void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteStartObject();
Expand All @@ -50,6 +56,21 @@ public void SerializeV2(IAsyncApiWriter writer)
writer.WriteOptionalObject("default", this.Default, (w, s) => w.WriteAny(s));
writer.WriteOptionalProperty("order", this.Order);
writer.WriteOptionalCollection("aliases", this.Aliases, (w, s) => w.WriteValue(s));
if (this.Metadata.Any())
{
foreach (var item in this.Metadata)
{
writer.WritePropertyName(item.Key);
if (item.Value == null)
{
writer.WriteNull();
}
else
{
item.Value.SerializeV2(writer);
}
}
}
writer.WriteEndObject();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/LEGO.AsyncAPI/Models/Avro/AvroFieldType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace LEGO.AsyncAPI.Models

public abstract class AvroSchema : IAsyncApiSerializable
{
public abstract string Type { get; }

public static implicit operator AvroSchema(AvroPrimitiveType type)
{
return new AvroPrimitive(type);
Expand Down
28 changes: 25 additions & 3 deletions src/LEGO.AsyncAPI/Models/Avro/AvroFixed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,48 @@ namespace LEGO.AsyncAPI.Models
{
using LEGO.AsyncAPI.Writers;
using System.Collections.Generic;
using System.Linq;

public class AvroFixed : AvroSchema
{
public string Type { get; } = "fixed";
public override string Type { get; } = "fixed";

public string Name { get; set; }
public string Namespaace { get; set; }

public string? Namespaace { get; set; }

Check warning on line 15 in src/LEGO.AsyncAPI/Models/Avro/AvroFixed.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

public IList<string> Aliases { get; set; } = new List<string>();

public int Size { get; set; }

/// <summary>
/// A map of properties not in the schema, but added as additional metadata.
/// </summary>
public IDictionary<string, AvroSchema> Metadata { get; set; } = new Dictionary<string, AvroSchema>();

public override void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WriteRequiredProperty("name", this.Name);
writer.WriteRequiredProperty("name", this.Namespaace);
writer.WriteOptionalProperty("namespace", this.Namespaace);
writer.WriteOptionalCollection("aliases", this.Aliases, (w, s) => w.WriteValue(s));
writer.WriteRequiredProperty("size", this.Size);
if (this.Metadata.Any())
{
foreach (var item in this.Metadata)
{
writer.WritePropertyName(item.Key);
if (item.Value == null)
{
writer.WriteNull();
}
else
{
item.Value.SerializeV2(writer);
}
}
}
writer.WriteEndObject();
}
}
Expand Down
28 changes: 24 additions & 4 deletions src/LEGO.AsyncAPI/Models/Avro/AvroMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@ namespace LEGO.AsyncAPI.Models
{
using System;
using System.Collections.Generic;
using System.Linq;
using LEGO.AsyncAPI.Writers;

public class AvroMap : AvroSchema
{
public string Type { get; } = "map";
public override string Type { get; } = "map";

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

/// <summary>
/// A map of properties not in the schema, but added as additional metadata.
/// </summary>
public IDictionary<string, AvroSchema> Metadata { get; set; } = new Dictionary<string, AvroSchema>();

public override void SerializeV2(IAsyncApiWriter writer)
{
Convert.ToBoolean()
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WriteRequiredObject("values", this.Values, (w, f) => f.SerializeV2(w));
writer.WriteRequiredProperty("values", this.Values.GetDisplayName());
if (this.Metadata.Any())
{
foreach (var item in this.Metadata)
{
writer.WritePropertyName(item.Key);
if (item.Value == null)
{
writer.WriteNull();
}
else
{
item.Value.SerializeV2(writer);
}
}
}
writer.WriteEndObject();
}
}
Expand Down
32 changes: 29 additions & 3 deletions src/LEGO.AsyncAPI/Models/Avro/AvroPrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,46 @@

namespace LEGO.AsyncAPI.Models
{
using System.Collections.Generic;
using System.Linq;
using LEGO.AsyncAPI.Writers;

public class AvroPrimitive : AvroSchema
{
public AvroPrimitiveType Type { get; set; }
public override string Type { get; }

/// <summary>
/// A map of properties not in the schema, but added as additional metadata.
/// </summary>
public IDictionary<string, AvroSchema> Metadata { get; set; } = new Dictionary<string, AvroSchema>();

public AvroPrimitive(AvroPrimitiveType type)
{
this.Type = type;
this.Type = type.GetDisplayName();
}

public AvroPrimitive()
{
}

public override void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteValue(this.Type.GetDisplayName());
writer.WriteValue(this.Type);
if (this.Metadata.Any())
{
foreach (var item in this.Metadata)
{
writer.WritePropertyName(item.Key);
if (item.Value == null)
{
writer.WriteNull();
}
else
{
item.Value.SerializeV2(writer);
}
}
}
}
}
}
Loading

0 comments on commit 799cf93

Please sign in to comment.