Skip to content

Commit

Permalink
added avro models
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed May 23, 2024
1 parent 3d270e7 commit 56ff634
Show file tree
Hide file tree
Showing 22 changed files with 442 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public override void Visit(AsyncApiOperation operation)
public override void Visit(AsyncApiMessage message)
{
this.ResolveObject(message.Headers, r => message.Headers = r);
if (message.Payload is AsyncApiSchemaPayload)
if (message.Payload is AsyncApiJsonSchemaPayload)
{
this.ResolveObject(message.Payload as AsyncApiSchemaPayload, r => message.Payload = r);
this.ResolveObject(message.Payload as AsyncApiJsonSchemaPayload, r => message.Payload = r);
}
this.ResolveList(message.Traits);
this.ResolveObject(message.CorrelationId, r => message.CorrelationId = r);
Expand Down
10 changes: 7 additions & 3 deletions src/LEGO.AsyncAPI.Readers/V2/AsyncApiMessageDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ internal static partial class AsyncApiV2Deserializer
},
};

public static IAsyncApiMessagePayload LoadPayload(ParseNode n)
public static IAsyncApiMessagePayload LoadJsonSchemaPayload(ParseNode n)
{
// #ToFix figure out a way to get the format in a proper way.
return LoadPayload(n, null);
}

public static IAsyncApiMessagePayload LoadAvroPayload(ParseNode n)
{
return LoadPayload(n, "application/vnd.apache.avro");
}

private static IAsyncApiMessagePayload LoadPayload(ParseNode n, string format)
{
if (n == null)
Expand All @@ -91,7 +95,7 @@ private static IAsyncApiMessagePayload LoadPayload(ParseNode n, string format)
case null:
case "":
case var _ when SupportedJsonSchemaFormats.Where(s => format.StartsWith(s)).Any():
return new AsyncApiSchemaPayload(AsyncApiSchemaDeserializer.LoadSchema(n));
return new AsyncApiJsonSchemaPayload(AsyncApiSchemaDeserializer.LoadSchema(n));
case var _ when SupportedAvroSchemaFormats.Where(s => format.StartsWith(s)).Any():
return new AsyncApiAvroSchemaPayload();
default:
Expand Down
3 changes: 2 additions & 1 deletion src/LEGO.AsyncAPI.Readers/V2/AsyncApiV2VersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public AsyncApiV2VersionService(AsyncApiDiagnostic diagnostic)
[typeof(AsyncApiOperation)] = AsyncApiV2Deserializer.LoadOperation,
[typeof(AsyncApiParameter)] = AsyncApiV2Deserializer.LoadParameter,
[typeof(AsyncApiSchema)] = AsyncApiSchemaDeserializer.LoadSchema,
[typeof(AsyncApiSchemaPayload)] = AsyncApiV2Deserializer.LoadPayload, // #ToFix how do we get the schemaFormat?!
[typeof(AsyncApiJsonSchemaPayload)] = AsyncApiV2Deserializer.LoadJsonSchemaPayload, // #ToFix how do we get the schemaFormat?!
[typeof(AsyncApiAvroSchemaPayload)] = AsyncApiV2Deserializer.LoadAvroPayload,
[typeof(AsyncApiSecurityRequirement)] = AsyncApiV2Deserializer.LoadSecurityRequirement,
[typeof(AsyncApiSecurityScheme)] = AsyncApiV2Deserializer.LoadSecurityScheme,
[typeof(AsyncApiServer)] = AsyncApiV2Deserializer.LoadServer,
Expand Down
1 change: 1 addition & 0 deletions src/LEGO.AsyncAPI/LEGO.AsyncAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Apache.Avro" Version="1.11.3" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
10 changes: 5 additions & 5 deletions src/LEGO.AsyncAPI/Models/AsyncApiSchemaPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace LEGO.AsyncAPI.Models
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

public class AsyncApiSchemaPayload : IAsyncApiMessagePayload, IAsyncApiReferenceable
public class AsyncApiJsonSchemaPayload : IAsyncApiMessagePayload, IAsyncApiReferenceable
{
private readonly AsyncApiSchema schema;

public AsyncApiSchemaPayload()
public AsyncApiJsonSchemaPayload()
{
this.schema = new AsyncApiSchema();
}

public AsyncApiSchemaPayload(AsyncApiSchema schema)
public AsyncApiJsonSchemaPayload(AsyncApiSchema schema)
{
this.schema = schema;
}
Expand Down Expand Up @@ -111,9 +111,9 @@ public AsyncApiSchemaPayload(AsyncApiSchema schema)

public AsyncApiSchema AdditionalProperties { get => this.schema.AdditionalProperties; set => this.schema.AdditionalProperties = value; }

public static implicit operator AsyncApiSchema(AsyncApiSchemaPayload payload) => payload.schema;
public static implicit operator AsyncApiSchema(AsyncApiJsonSchemaPayload payload) => payload.schema;

public static implicit operator AsyncApiSchemaPayload(AsyncApiSchema schema) => new AsyncApiSchemaPayload(schema);
public static implicit operator AsyncApiJsonSchemaPayload(AsyncApiSchema schema) => new AsyncApiJsonSchemaPayload(schema);

public void SerializeV2(IAsyncApiWriter writer)
{
Expand Down
26 changes: 26 additions & 0 deletions src/LEGO.AsyncAPI/Models/Avro/AvroArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) The LEGO Group. All rights reserved.

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

public class AvroArray : AvroFieldType
{
public string Type { get; set; } = "array";

public AvroFieldType Items { get; set; }

public override void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WritePropertyName("items");
this.Items.SerializeV2(writer);
writer.WriteEndObject();
}
}
}
35 changes: 35 additions & 0 deletions src/LEGO.AsyncAPI/Models/Avro/AvroEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) The LEGO Group. All rights reserved.

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

public class AvroEnum : AvroFieldType
{
public string Type { get; set; } = "enum";

public string Name { get; set; }

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

public override void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WriteRequiredProperty("name", this.Name);
writer.WritePropertyName("symbols");
writer.WriteStartArray();
foreach (var symbol in this.Symbols)
{
writer.WriteValue(symbol);
}

writer.WriteEndArray();
writer.WriteEndObject();
}
}
}
67 changes: 67 additions & 0 deletions src/LEGO.AsyncAPI/Models/Avro/AvroField.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.Models
{
using System;
using System.Collections.Generic;
using System.Linq;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

/// <summary>
/// Represents a field within an Avro record schema.
/// </summary>
public class AvroField : IAsyncApiSerializable
{
/// <summary>
/// The name of the field.
/// </summary>
public string Name { get; set; }

/// <summary>
/// The type of the field. Can be a primitive type, a complex type, or a union.
/// </summary>
public AvroFieldType Type { get; set; }

/// <summary>
/// The documentation for the field.
/// </summary>
public string Doc { get; set; }

/// <summary>
/// The default value for the field.
/// </summary>
public AsyncApiAny Default { get; set; }

/// <summary>
/// The order of the field, can be 'ascending', 'descending', or 'ignore'.
/// </summary>
public string Order { get; set; }

public void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteStartObject();

// name
writer.WriteOptionalProperty("name", this.Name);

// type
if (this.Type != null)
{
writer.WritePropertyName("type");
this.Type.SerializeV2(writer);
}

// doc
writer.WriteOptionalProperty("doc", this.Doc);

// default
writer.WriteOptionalObject("default", this.Default, (w, s) => w.WriteAny(s));

// order
writer.WriteOptionalProperty("order", this.Order);

writer.WriteEndObject();
}
}
}
15 changes: 15 additions & 0 deletions src/LEGO.AsyncAPI/Models/Avro/AvroFieldType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) The LEGO Group. All rights reserved.

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

public abstract class AvroFieldType : IAsyncApiSerializable
{
public abstract void SerializeV2(IAsyncApiWriter writer);
}
}
28 changes: 28 additions & 0 deletions src/LEGO.AsyncAPI/Models/Avro/AvroFixed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) The LEGO Group. All rights reserved.

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

public class AvroFixed : AvroFieldType
{
public string Type { get; set; } = "fixed";

public string Name { get; set; }

public int Size { get; set; }

public override void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WriteRequiredProperty("name", this.Name);
writer.WriteRequiredProperty("size", this.Size);
writer.WriteEndObject();
}
}
}
26 changes: 26 additions & 0 deletions src/LEGO.AsyncAPI/Models/Avro/AvroMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) The LEGO Group. All rights reserved.

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

public class AvroMap : AvroFieldType
{
public string Type { get; set; } = "map";

public AvroFieldType Values { get; set; }

public override void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WritePropertyName("values");
this.Values.SerializeV2(writer);
writer.WriteEndObject();
}
}
}
25 changes: 25 additions & 0 deletions src/LEGO.AsyncAPI/Models/Avro/AvroPrimitive.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) The LEGO Group. All rights reserved.

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

public class AvroPrimitive : AvroFieldType
{
public string Type { get; set; }

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

public override void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteValue(this.Type);
}
}
}
35 changes: 35 additions & 0 deletions src/LEGO.AsyncAPI/Models/Avro/AvroRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) The LEGO Group. All rights reserved.

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

public class AvroRecord : AvroFieldType
{
public string Type { get; set; } = "record";

public string Name { get; set; }

public IList<AvroField> Fields { get; set; } = new List<AvroField>();

public override void SerializeV2(IAsyncApiWriter writer)
{
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WriteRequiredProperty("name", this.Name);
writer.WritePropertyName("fields");
writer.WriteStartArray();
foreach (var field in this.Fields)
{
field.SerializeV2(writer);
}

writer.WriteEndArray();
writer.WriteEndObject();
}
}
}
Loading

0 comments on commit 56ff634

Please sign in to comment.