Skip to content

Commit

Permalink
finished writing
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed May 23, 2024
1 parent 56ff634 commit c96dab3
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 123 deletions.
1 change: 0 additions & 1 deletion src/LEGO.AsyncAPI/LEGO.AsyncAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
</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
1 change: 0 additions & 1 deletion src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace LEGO.AsyncAPI.Models
{
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Nodes;
using LEGO.AsyncAPI.Models.Interfaces;
Expand Down
1 change: 0 additions & 1 deletion src/LEGO.AsyncAPI/Models/AsyncApiSchemaPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace LEGO.AsyncAPI.Models
{
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

Expand Down
9 changes: 2 additions & 7 deletions src/LEGO.AsyncAPI/Models/Avro/AvroArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@

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 string Type { get; } = "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.WriteRequiredObject("items", this.Items, (w, f) => f.SerializeV2(w));
writer.WriteEndObject();
}
}
Expand Down
14 changes: 2 additions & 12 deletions src/LEGO.AsyncAPI/Models/Avro/AvroEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

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 Type { get; } = "enum";

public string Name { get; set; }

Expand All @@ -21,14 +18,7 @@ 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.WriteRequiredCollection("symbols", this.Symbols, (w, s) => w.WriteValue(s));
writer.WriteEndObject();
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/LEGO.AsyncAPI/Models/Avro/AvroField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

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

Expand Down Expand Up @@ -46,11 +43,7 @@ public void SerializeV2(IAsyncApiWriter writer)
writer.WriteOptionalProperty("name", this.Name);

// type
if (this.Type != null)
{
writer.WritePropertyName("type");
this.Type.SerializeV2(writer);
}
writer.WriteOptionalObject("type", this.Type, (w, s) => s.SerializeV2(w));

// doc
writer.WriteOptionalProperty("doc", this.Doc);
Expand Down
8 changes: 5 additions & 3 deletions src/LEGO.AsyncAPI/Models/Avro/AvroFieldType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

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 static implicit operator AvroFieldType(AvroPrimitiveType type)
{
return new AvroPrimitive(type);
}

public abstract void SerializeV2(IAsyncApiWriter writer);
}
}
6 changes: 1 addition & 5 deletions src/LEGO.AsyncAPI/Models/Avro/AvroFixed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

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 Type { get; } = "fixed";

public string Name { get; set; }

Expand Down
9 changes: 2 additions & 7 deletions src/LEGO.AsyncAPI/Models/Avro/AvroMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@

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 string Type { get; } = "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.WriteRequiredObject("values", this.Values, (w, f) => f.SerializeV2(w));
writer.WriteEndObject();
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/LEGO.AsyncAPI/Models/Avro/AvroPrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@

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

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

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

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

namespace LEGO.AsyncAPI.Models
{
using LEGO.AsyncAPI.Attributes;

public enum AvroPrimitiveType
{
[Display("null")]
Null,

[Display("boolean")]
Boolean,

[Display("int")]
Int,

[Display("long")]
Long,

[Display("float")]
Float,

[Display("double")]
Double,

[Display("bytes")]
Bytes,

[Display("string")]
String,
}
}
12 changes: 1 addition & 11 deletions src/LEGO.AsyncAPI/Models/Avro/AvroRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

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
Expand All @@ -21,14 +18,7 @@ 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.WriteRequiredCollection("fields", this.Fields, (w, s) => s.SerializeV2(w));
writer.WriteEndObject();
}
}
Expand Down
23 changes: 11 additions & 12 deletions src/LEGO.AsyncAPI/Models/Avro/AvroSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace LEGO.AsyncAPI.Models
{
using System;
using System.Collections.Generic;
using System.Linq;
using LEGO.AsyncAPI.Models.Interfaces;
Expand Down Expand Up @@ -33,30 +32,30 @@ public class AvroSchema : IAsyncApiSerializable
/// </summary>
public string? Doc { get; set; }

Check warning on line 33 in src/LEGO.AsyncAPI/Models/Avro/AvroSchema.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

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

Check warning on line 33 in src/LEGO.AsyncAPI/Models/Avro/AvroSchema.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

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

Check warning on line 33 in src/LEGO.AsyncAPI/Models/Avro/AvroSchema.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.

/// <summary>
/// The list of fields in the schema.
/// </summary>
public IList<AvroField> Fields { get; set; } = new List<AvroField>();

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

// type
writer.WriteOptionalProperty(AsyncApiConstants.Type, this.Type.GetDisplayName());

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

// namespace
writer.WriteOptionalProperty("namespace", this.Namespace);

// type
var types = EnumExtensions.GetFlags<AvroSchemaType>(this.Type);
if (types.Count() == 1)
{
writer.WriteOptionalProperty(AsyncApiConstants.Type, types.First().GetDisplayName());
}
else
{
writer.WriteOptionalCollection(AsyncApiConstants.Type, types.Select(t => t.GetDisplayName()), (w, s) => w.WriteValue(s));
}

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

// fields
writer.WriteOptionalCollection("fields", this.Fields, (w, f) => f.SerializeV2(w));

writer.WriteEndObject();
}
}
Expand Down
40 changes: 3 additions & 37 deletions src/LEGO.AsyncAPI/Models/Avro/AvroSchemaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,15 @@ namespace LEGO.AsyncAPI.Models
/// <summary>
/// Enumeration of Avro schema types. See <a href="https://avro.apache.org/docs/1.9.0/spec.html#schemas">Avro Schemas</a>.
/// </summary>
[Flags]
public enum AvroSchemaType
{
[Display("null")]
Null = 1,

[Display("boolean")]
Boolean = 2,

[Display("int")]
Int = 4,

[Display("long")]
Long = 8,

[Display("float")]
Float = 16,

[Display("double")]
Double = 32,

[Display("bytes")]
Bytes = 64,

[Display("string")]
String = 128,

[Display("record")]
Record = 256,
Record,

[Display("enum")]
Enum = 512,

[Display("array")]
Array = 1024,

[Display("map")]
Map = 2048,
Enum,

[Display("fixed")]
Fixed = 4096,

[Display("logical")]
Logical = 8192,
Fixed
}
}
6 changes: 0 additions & 6 deletions src/LEGO.AsyncAPI/Models/Avro/AvroUnion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

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

public class AvroUnion : AvroFieldType
Expand All @@ -17,11 +14,8 @@ public override void SerializeV2(IAsyncApiWriter writer)
writer.WriteStartArray();
foreach (var type in this.Types)
{
writer.WriteStartObject();
type.SerializeV2(writer);
writer.WriteEndObject();
}

writer.WriteEndArray();
}
}
Expand Down
Loading

0 comments on commit c96dab3

Please sign in to comment.