|
3 | 3 | namespace LEGO.AsyncAPI.Readers
|
4 | 4 | {
|
5 | 5 | using System;
|
| 6 | + using System.Threading; |
6 | 7 | using LEGO.AsyncAPI.Exceptions;
|
7 | 8 | using LEGO.AsyncAPI.Models;
|
| 9 | + using LEGO.AsyncAPI.Models.Avro.LogicalTypes; |
8 | 10 | using LEGO.AsyncAPI.Readers.Exceptions;
|
9 | 11 | using LEGO.AsyncAPI.Readers.ParseNodes;
|
10 | 12 | using LEGO.AsyncAPI.Writers;
|
@@ -68,6 +70,60 @@ public class AsyncApiAvroSchemaDeserializer
|
68 | 70 | { "types", (a, n) => a.Types = n.CreateList(LoadSchema) },
|
69 | 71 | };
|
70 | 72 |
|
| 73 | + private static readonly FixedFieldMap<AvroDecimal> DecimalFixedFields = new() |
| 74 | + { |
| 75 | + { "type", (a, n) => { } }, |
| 76 | + { "logicalType", (a, n) => { } }, |
| 77 | + { "precision", (a, n) => a.Precision = int.Parse(n.GetScalarValue()) }, |
| 78 | + { "scale", (a, n) => a.Scale = int.Parse(n.GetScalarValue()) }, |
| 79 | + }; |
| 80 | + |
| 81 | + private static readonly FixedFieldMap<AvroUUID> UUIDFixedFields = new() |
| 82 | + { |
| 83 | + { "type", (a, n) => { } }, |
| 84 | + { "logicalType", (a, n) => { } }, |
| 85 | + }; |
| 86 | + |
| 87 | + private static readonly FixedFieldMap<AvroDate> DateFixedFields = new() |
| 88 | + { |
| 89 | + { "type", (a, n) => { } }, |
| 90 | + { "logicalType", (a, n) => { } }, |
| 91 | + }; |
| 92 | + |
| 93 | + private static readonly FixedFieldMap<AvroTimeMillis> TimeMillisFixedFields = new() |
| 94 | + { |
| 95 | + { "type", (a, n) => { } }, |
| 96 | + { "logicalType", (a, n) => { } }, |
| 97 | + }; |
| 98 | + |
| 99 | + private static readonly FixedFieldMap<AvroTimeMicros> TimeMicrosFixedFields = new() |
| 100 | + { |
| 101 | + { "type", (a, n) => { } }, |
| 102 | + { "logicalType", (a, n) => { } }, |
| 103 | + }; |
| 104 | + |
| 105 | + private static readonly FixedFieldMap<AvroTimestampMillis> TimestampMillisFixedFields = new() |
| 106 | + { |
| 107 | + { "type", (a, n) => { } }, |
| 108 | + { "logicalType", (a, n) => { } }, |
| 109 | + }; |
| 110 | + |
| 111 | + private static readonly FixedFieldMap<AvroTimestampMicros> TimestampMicrosFixedFields = new() |
| 112 | + { |
| 113 | + { "type", (a, n) => { } }, |
| 114 | + { "logicalType", (a, n) => { } }, |
| 115 | + }; |
| 116 | + |
| 117 | + private static readonly FixedFieldMap<AvroDuration> DurationFixedFields = new() |
| 118 | + { |
| 119 | + { "type", (a, n) => { } }, |
| 120 | + { "logicalType", (a, n) => { } }, |
| 121 | + { "name", (a, n) => a.Name = n.GetScalarValue() }, |
| 122 | + { "namespace", (a, n) => a.Namespace = n.GetScalarValue() }, |
| 123 | + { "aliases", (a, n) => a.Aliases = n.CreateSimpleList(n2 => n2.GetScalarValue()) }, |
| 124 | + { "size", (a, n) => { } }, |
| 125 | + }; |
| 126 | + |
71 | 127 | private static readonly PatternFieldMap<AvroRecord> RecordMetadataPatternFields =
|
72 | 128 | new()
|
73 | 129 | {
|
@@ -110,6 +166,54 @@ public class AsyncApiAvroSchemaDeserializer
|
110 | 166 | { s => s.StartsWith(string.Empty), (a, p, n) => a.Metadata[p] = n.CreateAny() },
|
111 | 167 | };
|
112 | 168 |
|
| 169 | + private static readonly PatternFieldMap<AvroDecimal> DecimalMetadataPatternFields = |
| 170 | + new() |
| 171 | + { |
| 172 | + { s => s.StartsWith(string.Empty), (a, p, n) => a.Metadata[p] = n.CreateAny() }, |
| 173 | + }; |
| 174 | + |
| 175 | + private static readonly PatternFieldMap<AvroUUID> UUIDMetadataPatternFields = |
| 176 | + new() |
| 177 | + { |
| 178 | + { s => s.StartsWith(string.Empty), (a, p, n) => a.Metadata[p] = n.CreateAny() }, |
| 179 | + }; |
| 180 | + |
| 181 | + private static readonly PatternFieldMap<AvroDate> DateMetadataPatternFields = |
| 182 | + new() |
| 183 | + { |
| 184 | + { s => s.StartsWith(string.Empty), (a, p, n) => a.Metadata[p] = n.CreateAny() }, |
| 185 | + }; |
| 186 | + |
| 187 | + private static readonly PatternFieldMap<AvroTimeMillis> TimeMillisMetadataPatternFields = |
| 188 | + new() |
| 189 | + { |
| 190 | + { s => s.StartsWith(string.Empty), (a, p, n) => a.Metadata[p] = n.CreateAny() }, |
| 191 | + }; |
| 192 | + |
| 193 | + private static readonly PatternFieldMap<AvroTimeMicros> TimeMicrosMetadataPatternFields = |
| 194 | + new() |
| 195 | + { |
| 196 | + { s => s.StartsWith(string.Empty), (a, p, n) => a.Metadata[p] = n.CreateAny() }, |
| 197 | + }; |
| 198 | + |
| 199 | + private static readonly PatternFieldMap<AvroTimestampMillis> TimestampMillisMetadataPatternFields = |
| 200 | + new() |
| 201 | + { |
| 202 | + { s => s.StartsWith(string.Empty), (a, p, n) => a.Metadata[p] = n.CreateAny() }, |
| 203 | + }; |
| 204 | + |
| 205 | + private static readonly PatternFieldMap<AvroTimestampMicros> TimestampMicrosMetadataPatternFields = |
| 206 | + new() |
| 207 | + { |
| 208 | + { s => s.StartsWith(string.Empty), (a, p, n) => a.Metadata[p] = n.CreateAny() }, |
| 209 | + }; |
| 210 | + |
| 211 | + private static readonly PatternFieldMap<AvroDuration> DurationMetadataPatternFields = |
| 212 | + new() |
| 213 | + { |
| 214 | + { s => s.StartsWith(string.Empty), (a, p, n) => a.Metadata[p] = n.CreateAny() }, |
| 215 | + }; |
| 216 | + |
113 | 217 | public static AvroSchema LoadSchema(ParseNode node)
|
114 | 218 | {
|
115 | 219 | if (node is ValueNode valueNode)
|
@@ -141,8 +245,13 @@ public static AvroSchema LoadSchema(ParseNode node)
|
141 | 245 | };
|
142 | 246 | }
|
143 | 247 |
|
144 |
| - var type = mapNode["type"]?.Value.GetScalarValue(); |
| 248 | + var isLogicalType = mapNode["logicalType"] != null; |
| 249 | + if (isLogicalType) |
| 250 | + { |
| 251 | + return LoadLogicalType(mapNode); |
| 252 | + } |
145 | 253 |
|
| 254 | + var type = mapNode["type"]?.Value.GetScalarValue(); |
146 | 255 | switch (type)
|
147 | 256 | {
|
148 | 257 | case "record":
|
@@ -177,6 +286,48 @@ public static AvroSchema LoadSchema(ParseNode node)
|
177 | 286 | throw new AsyncApiReaderException("Invalid node type");
|
178 | 287 | }
|
179 | 288 |
|
| 289 | + private static AvroSchema LoadLogicalType(MapNode mapNode) |
| 290 | + { |
| 291 | + var type = mapNode["logicalType"]?.Value.GetScalarValue(); |
| 292 | + switch (type) |
| 293 | + { |
| 294 | + case "decimal": |
| 295 | + var @decimal = new AvroDecimal(); |
| 296 | + mapNode.ParseFields(ref @decimal, DecimalFixedFields, DecimalMetadataPatternFields); |
| 297 | + return @decimal; |
| 298 | + case "uuid": |
| 299 | + var uuid = new AvroUUID(); |
| 300 | + mapNode.ParseFields(ref uuid, UUIDFixedFields, UUIDMetadataPatternFields); |
| 301 | + return uuid; |
| 302 | + case "date": |
| 303 | + var date = new AvroDate(); |
| 304 | + mapNode.ParseFields(ref date, DateFixedFields, DateMetadataPatternFields); |
| 305 | + return date; |
| 306 | + case "time-millis": |
| 307 | + var timeMillis = new AvroTimeMillis(); |
| 308 | + mapNode.ParseFields(ref timeMillis, TimeMillisFixedFields, TimeMillisMetadataPatternFields); |
| 309 | + return timeMillis; |
| 310 | + case "time-micros": |
| 311 | + var timeMicros = new AvroTimeMicros(); |
| 312 | + mapNode.ParseFields(ref timeMicros, TimeMicrosFixedFields, TimeMicrosMetadataPatternFields); |
| 313 | + return timeMicros; |
| 314 | + case "timestamp-millis": |
| 315 | + var timestampMillis = new AvroTimestampMillis(); |
| 316 | + mapNode.ParseFields(ref timestampMillis, TimestampMillisFixedFields, TimestampMillisMetadataPatternFields); |
| 317 | + return timestampMillis; |
| 318 | + case "timestamp-micros": |
| 319 | + var timestampMicros = new AvroTimestampMicros(); |
| 320 | + mapNode.ParseFields(ref timestampMicros, TimestampMicrosFixedFields, TimestampMicrosMetadataPatternFields); |
| 321 | + return timestampMicros; |
| 322 | + case "duration": |
| 323 | + var duration = new AvroDuration(); |
| 324 | + mapNode.ParseFields(ref duration, DurationFixedFields, DurationMetadataPatternFields); |
| 325 | + return duration; |
| 326 | + default: |
| 327 | + throw new AsyncApiException($"Unsupported type: {type}"); |
| 328 | + } |
| 329 | + } |
| 330 | + |
180 | 331 | private static AvroField LoadField(ParseNode node)
|
181 | 332 | {
|
182 | 333 | var mapNode = node.CheckMapNode("field");
|
|
0 commit comments