Skip to content

Commit

Permalink
WIP on alex/avro
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed May 30, 2024
1 parent 87b64da commit f2053e9
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 176 deletions.
52 changes: 26 additions & 26 deletions src/LEGO.AsyncAPI.Readers/V2/AsyncApiAvroSchemaDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ namespace LEGO.AsyncAPI.Readers

public class AsyncApiAvroSchemaDeserializer
{
private static readonly FixedFieldMap<AvroSchema> schemaFixedFields = new()
{
{ "type", (a, n) => a.Type = n.GetScalarValue().GetEnumFromDisplayName<AvroSchemaType>() },
{ "name", (a, n) => a.Name = n.GetScalarValue() },
{ "namespace", (a, n) => a.Namespace = n.GetScalarValue() },
{ "doc", (a, n) => a.Doc = n.GetScalarValue() },
{ "fields", (a, n) => a.Fields = n.CreateList(LoadField) },
};

public static AvroSchema LoadSchema(ParseNode node)
{
var mapNode = node.CheckMapNode("schema");
var schema = new AvroSchema();

mapNode.ParseFields(ref schema, schemaFixedFields, null);

return schema;
}
// private static readonly FixedFieldMap<AvroSchema> schemaFixedFields = new()
// {
// { "type", (a, n) => a.Type = n.GetScalarValue() },
// { "name", (a, n) => a.Name = n.GetScalarValue() },
// { "namespace", (a, n) => a.Namespace = n.GetScalarValue() },
// { "doc", (a, n) => a.Doc = n.GetScalarValue() },
// { "fields", (a, n) => a.Fields = n.CreateList(LoadField) },
// };

// public static AvroSchema LoadSchema(ParseNode node)
// {
// var mapNode = node.CheckMapNode("schema");
// var schema = new AvroSchema();
//
// mapNode.ParseFields(ref schema, schemaFixedFields, null);
//
// return schema;
// }

private static AvroField LoadField(ParseNode node)
{
var mapNode = node.CheckMapNode("field");
var field = new AvroField();

mapNode.ParseFields(ref field, fieldFixedFields, null);

return field;
}

private static readonly FixedFieldMap<AvroField> fieldFixedFields = new()
{
{ "name", (a, n) => a.Name = n.GetScalarValue() },
{ "type", (a, n) => a.Type = LoadFieldType(n) },
{ "type", (a, n) => a.Type = LoadSchema(n) },
{ "doc", (a, n) => a.Doc = n.GetScalarValue() },
{ "default", (a, n) => a.Default = n.CreateAny() },
{ "order", (a, n) => a.Order = n.GetScalarValue() },
Expand Down Expand Up @@ -72,21 +72,21 @@ private static AvroField LoadField(ParseNode node)
private static readonly FixedFieldMap<AvroArray> arrayFixedFields = new()
{
{ "type", (a, n) => { } },
{ "items", (a, n) => a.Items = LoadFieldType(n) },
{ "items", (a, n) => a.Items = LoadSchema(n) },
};

private static readonly FixedFieldMap<AvroMap> mapFixedFields = new()
{
{ "type", (a, n) => { } },
{ "values", (a, n) => a.Values = LoadFieldType(n) },
{ "values", (a, n) => a.Values = LoadSchema(n) },
};

private static readonly FixedFieldMap<AvroUnion> unionFixedFields = new()
{
{ "types", (a, n) => a.Types = n.CreateList(LoadFieldType) },
{ "types", (a, n) => a.Types = n.CreateList(LoadSchema) },
};

private static AvroFieldType LoadFieldType(ParseNode node)
private static AvroSchema LoadSchema(ParseNode node)
{
if (node is ValueNode valueNode)
{
Expand All @@ -98,7 +98,7 @@ private static AvroFieldType LoadFieldType(ParseNode node)
var union = new AvroUnion();
foreach (var item in node as ListNode)
{
union.Types.Add(LoadFieldType(item));
union.Types.Add(LoadSchema(item));
}

return union;
Expand Down
4 changes: 2 additions & 2 deletions src/LEGO.AsyncAPI/Models/Avro/AvroArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace LEGO.AsyncAPI.Models
{
using LEGO.AsyncAPI.Writers;

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

public AvroFieldType Items { get; set; }
public AvroSchema Items { get; set; }

public override void SerializeV2(IAsyncApiWriter writer)
{
Expand Down
5 changes: 4 additions & 1 deletion src/LEGO.AsyncAPI/Models/Avro/AvroEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ namespace LEGO.AsyncAPI.Models
using System.Collections.Generic;
using LEGO.AsyncAPI.Writers;

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

public string Name { get; set; }

public string Namespace { get; set; }

public string Doc { get; set; }

Expand All @@ -24,6 +26,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WriteRequiredProperty("name", this.Name);
writer.WriteRequiredProperty("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));
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI/Models/Avro/AvroField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AvroField : IAsyncApiSerializable
/// <summary>
/// The type of the field. Can be a primitive type, a complex type, or a union.
/// </summary>
public AvroFieldType Type { get; set; }
public AvroSchema Type { get; set; }

/// <summary>
/// The documentation for the field.
Expand Down
4 changes: 2 additions & 2 deletions src/LEGO.AsyncAPI/Models/Avro/AvroFieldType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace LEGO.AsyncAPI.Models
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

public abstract class AvroFieldType : IAsyncApiSerializable
public abstract class AvroSchema : IAsyncApiSerializable
{
public static implicit operator AvroFieldType(AvroPrimitiveType type)
public static implicit operator AvroSchema(AvroPrimitiveType type)
{
return new AvroPrimitive(type);
}
Expand Down
4 changes: 3 additions & 1 deletion src/LEGO.AsyncAPI/Models/Avro/AvroFixed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ namespace LEGO.AsyncAPI.Models
using LEGO.AsyncAPI.Writers;
using System.Collections.Generic;

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

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

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

Expand All @@ -20,6 +21,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
writer.WriteStartObject();
writer.WriteOptionalProperty("type", this.Type);
writer.WriteRequiredProperty("name", this.Name);
writer.WriteRequiredProperty("name", this.Namespaace);
writer.WriteOptionalCollection("aliases", this.Aliases, (w, s) => w.WriteValue(s));
writer.WriteRequiredProperty("size", this.Size);
writer.WriteEndObject();
Expand Down
7 changes: 5 additions & 2 deletions src/LEGO.AsyncAPI/Models/Avro/AvroMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

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

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

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

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));
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI/Models/Avro/AvroPrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace LEGO.AsyncAPI.Models
{
using LEGO.AsyncAPI.Writers;

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

Expand Down
4 changes: 2 additions & 2 deletions src/LEGO.AsyncAPI/Models/Avro/AvroRecord.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) The LEGO Group. All rights reserved.
// Copyright (c) The LEGO Group. All rights reserved.

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

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

Expand Down
Loading

0 comments on commit f2053e9

Please sign in to comment.