Skip to content

Commit

Permalink
Updated AsyncAPi to use the base settings calls to pass around values
Browse files Browse the repository at this point in the history
  • Loading branch information
ByronMayne committed Mar 26, 2024
1 parent c756ba3 commit 74fff81
Show file tree
Hide file tree
Showing 24 changed files with 433 additions and 465 deletions.
10 changes: 5 additions & 5 deletions src/LEGO.AsyncAPI.Readers/AsyncApiJsonDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ internal class AsyncApiJsonDocumentReader : IAsyncApiReader<JsonNode, AsyncApiDi
private readonly AsyncApiReaderSettings settings;

/// <summary>
/// Create stream reader with custom settings if desired.
/// Initializes a new instance of the <see cref="AsyncApiJsonDocumentReader"/> class.
/// </summary>
/// <param name="settings"></param>
/// <param name="settings">The settings used to read json.</param>
public AsyncApiJsonDocumentReader(AsyncApiReaderSettings settings = null)
{
this.settings = settings ?? new AsyncApiReaderSettings();
Expand All @@ -39,7 +39,7 @@ public AsyncApiJsonDocumentReader(AsyncApiReaderSettings settings = null)
public AsyncApiDocument Read(JsonNode input, out AsyncApiDiagnostic diagnostic)
{
diagnostic = new AsyncApiDiagnostic();
var context = new ParsingContext(diagnostic)
var context = new ParsingContext(diagnostic, this.settings)
{
ExtensionParsers = this.settings.ExtensionParsers,
ServerBindingParsers = this.settings.Bindings.OfType<IBindingParser<IServerBinding>>().ToDictionary(b => b.BindingKey, b => b),
Expand Down Expand Up @@ -80,7 +80,7 @@ public AsyncApiDocument Read(JsonNode input, out AsyncApiDiagnostic diagnostic)
public async Task<ReadResult> ReadAsync(JsonNode input, CancellationToken cancellationToken = default)
{
var diagnostic = new AsyncApiDiagnostic();
var context = new ParsingContext(diagnostic)
var context = new ParsingContext(diagnostic, this.settings)
{
ExtensionParsers = this.settings.ExtensionParsers,
};
Expand Down Expand Up @@ -150,7 +150,7 @@ public T ReadFragment<T>(JsonNode input, AsyncApiVersion version, out AsyncApiDi
where T : IAsyncApiElement
{
diagnostic = new AsyncApiDiagnostic();
var context = new ParsingContext(diagnostic)
var context = new ParsingContext(diagnostic, this.settings)
{
ExtensionParsers = this.settings.ExtensionParsers,
ServerBindingParsers = this.settings.Bindings.OfType<IBindingParser<IServerBinding>>().ToDictionary(b => b.BindingKey, b => b),
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI.Readers/AsyncApiReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum ReferenceResolutionSetting
/// <summary>
/// Configuration settings to control how AsyncApi documents are parsed.
/// </summary>
public class AsyncApiReaderSettings
public class AsyncApiReaderSettings : AsyncApiSettings
{
/// <summary>
/// Indicates how references in the source document should be handled.
Expand Down
11 changes: 6 additions & 5 deletions src/LEGO.AsyncAPI.Readers/AsyncApiTextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace LEGO.AsyncAPI.Readers
{
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
Expand Down Expand Up @@ -42,7 +43,7 @@ public AsyncApiDocument Read(TextReader input, out AsyncApiDiagnostic diagnostic
// Parse the YAML/JSON text in the TextReader into the YamlDocument
try
{
jsonNode = LoadYamlDocument(input);
jsonNode = LoadYamlDocument(input, this.settings);
}
catch (JsonException ex)
{
Expand All @@ -69,7 +70,7 @@ public async Task<ReadResult> ReadAsync(TextReader input, CancellationToken canc
// Parse the YAML/JSON text in the TextReader into the YamlDocument
try
{
jsonNode = LoadYamlDocument(input);
jsonNode = LoadYamlDocument(input, this.settings);
}
catch (JsonException ex)
{
Expand Down Expand Up @@ -100,7 +101,7 @@ public T ReadFragment<T>(TextReader input, AsyncApiVersion version, out AsyncApi
// Parse the YAML/JSON
try
{
jsonNode = LoadYamlDocument(input);
jsonNode = LoadYamlDocument(input, this.settings);
}
catch (JsonException ex)
{
Expand All @@ -118,11 +119,11 @@ public T ReadFragment<T>(TextReader input, AsyncApiVersion version, out AsyncApi
/// </summary>
/// <param name="input">Stream containing YAML formatted text.</param>
/// <returns>Instance of a YamlDocument.</returns>
static JsonNode LoadYamlDocument(TextReader input)
static JsonNode LoadYamlDocument(TextReader input, AsyncApiReaderSettings settings)
{
var yamlStream = new YamlStream();
yamlStream.Load(input);
return yamlStream.Documents.First().ToJsonNode();
return yamlStream.Documents.First().ToJsonNode(settings);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,28 @@ namespace LEGO.AsyncAPI.Readers.Exceptions
[Serializable]
public class AsyncApiUnsupportedSpecVersionException : Exception
{
const string MessagePattern = "AsyncApi specification version '{0}' is not supported.";
private const string MessagePattern = "AsyncApi specification version '{0}' is not supported.";

/// <summary>
/// Initializes the <see cref="AsyncApiUnsupportedSpecVersionException"/> class with a specification version.
/// Initializes a new instance of the <see cref="AsyncApiUnsupportedSpecVersionException"/> class.
/// </summary>
/// <param name="specificationVersion">Version that caused this exception to be thrown.</param>
public AsyncApiUnsupportedSpecVersionException(string specificationVersion)
: base(string.Format(Configuration.CultureInfo, MessagePattern, specificationVersion))
/// <param name="settings">The settings used for reading and writing.</param>
public AsyncApiUnsupportedSpecVersionException(string specificationVersion, AsyncApiSettings settings)
: base(string.Format(settings.CultureInfo, MessagePattern, specificationVersion))
{
this.SpecificationVersion = specificationVersion;
}

/// <summary>
/// Initializes the <see cref="AsyncApiUnsupportedSpecVersionException"/> class with a specification version and
/// Initializes a new instance of the <see cref="AsyncApiUnsupportedSpecVersionException"/> class.
/// inner exception.
/// </summary>
/// <param name="specificationVersion">Version that caused this exception to be thrown.</param>
/// <param name="settings">The setting used for reading and writing</param>
/// <param name="innerException">Inner exception that caused this exception to be thrown.</param>
public AsyncApiUnsupportedSpecVersionException(string specificationVersion, Exception innerException)
: base(string.Format(Configuration.CultureInfo, MessagePattern, specificationVersion), innerException)
public AsyncApiUnsupportedSpecVersionException(string specificationVersion, AsyncApiSettings settings, Exception innerException)
: base(string.Format(settings.CultureInfo, MessagePattern, specificationVersion), innerException)
{
this.SpecificationVersion = specificationVersion;
}
Expand Down
23 changes: 0 additions & 23 deletions src/LEGO.AsyncAPI.Readers/JsonHelper.cs

This file was deleted.

13 changes: 10 additions & 3 deletions src/LEGO.AsyncAPI.Readers/ParseNodes/MapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace LEGO.AsyncAPI.Readers.ParseNodes
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using LEGO.AsyncAPI.Exceptions;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Readers.Exceptions;
Expand All @@ -18,7 +19,7 @@ public class MapNode : ParseNode, IEnumerable<PropertyNode>
private readonly List<PropertyNode> nodes;

public MapNode(ParsingContext context, string jsonString)
: this(context, JsonHelper.ParseJsonString(jsonString))
: this(context, JsonNode.Parse(jsonString))
{
}

Expand Down Expand Up @@ -196,7 +197,7 @@ public string GetReferencePointer()
return null;
}

return refNode.GetScalarValue();
return this.ToScalarValue(refNode);
}

public string GetScalarValue(ValueNode key)
Expand All @@ -205,12 +206,18 @@ public string GetScalarValue(ValueNode key)
? jsonValue
: throw new AsyncApiReaderException($"Expected scalar value while parsing {key.GetScalarValue()}", this.Context);

return scalarNode.GetScalarValue();
return this.ToScalarValue(scalarNode);
}

public override AsyncApiAny CreateAny()
{
return new AsyncApiAny(this.node);
}

private string ToScalarValue(JsonNode node)
{
var scalarNode = node is JsonValue value ? value : throw new AsyncApiException($"Expected scalar value");
return Convert.ToString(scalarNode.GetValue<object>(), this.Context.Settings.CultureInfo);
}
}
}
6 changes: 5 additions & 1 deletion src/LEGO.AsyncAPI.Readers/ParseNodes/ValueNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace LEGO.AsyncAPI.Readers.ParseNodes
{
using LEGO.AsyncAPI.Exceptions;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Readers.Exceptions;
using System;
using System.Text.Json.Nodes;

public class ValueNode : ParseNode
Expand All @@ -26,7 +28,9 @@ public override string GetScalarValue()
{
if (this.cachedScalarValue == null)
{
this.cachedScalarValue = this.node.GetScalarValue();
// TODO: Update this property to use the .ToString() or JsonReader.
var scalarNode = this.node is JsonValue value ? value : throw new AsyncApiException($"Expected scalar value");
this.cachedScalarValue = Convert.ToString(scalarNode.GetValue<object>(), this.Context.Settings.CultureInfo);
}

return this.cachedScalarValue;
Expand Down
23 changes: 22 additions & 1 deletion src/LEGO.AsyncAPI.Readers/ParsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,30 @@ internal Dictionary<string, Func<AsyncApiAny, IAsyncApiExtension>> ExtensionPars

public AsyncApiDiagnostic Diagnostic { get; }

/// <summary>
/// Gets the settings used fore reading json.
/// </summary>
public AsyncApiReaderSettings Settings { get; }

///// <summary>
///// Initializes a new instance of the <see cref="ParsingContext"/> class.
///// </summary>
/// <param name="diagnostic">The diagnostics.</param>
[Obsolete($"Please use the overloaded version that takes in an instance of {nameof(AsyncApiReaderSettings)} isntead.")]
public ParsingContext(AsyncApiDiagnostic diagnostic)
: this(diagnostic, new AsyncApiReaderSettings())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ParsingContext"/> class.
/// </summary>
/// <param name="diagnostic">The diagnostics.</param>
/// <param name="settings">The settings used to read json.</param>
public ParsingContext(AsyncApiDiagnostic diagnostic, AsyncApiReaderSettings settings)
{
this.Diagnostic = diagnostic;
this.Settings = settings;
}

internal AsyncApiDocument Parse(JsonNode jsonNode)
Expand All @@ -61,7 +82,7 @@ internal AsyncApiDocument Parse(JsonNode jsonNode)
break;

default:
throw new AsyncApiUnsupportedSpecVersionException(inputVersion);
throw new AsyncApiUnsupportedSpecVersionException(inputVersion, this.Settings);
}

return doc;
Expand Down
18 changes: 9 additions & 9 deletions src/LEGO.AsyncAPI.Readers/V2/AsyncApiSchemaDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public class JsonSchemaDeserializer
"multipleOf",
(a, n) =>
{
a.MultipleOf = double.Parse(n.GetScalarValue(), NumberStyles.Float, Configuration.CultureInfo);
a.MultipleOf = double.Parse(n.GetScalarValue(), NumberStyles.Float, n.Context.Settings.CultureInfo);
}
},
{
"maximum",
(a, n) =>
{
a.Maximum = double.Parse(n.GetScalarValue(), NumberStyles.Float, Configuration.CultureInfo);
a.Maximum = double.Parse(n.GetScalarValue(), NumberStyles.Float, n.Context.Settings.CultureInfo);
}
},
{
Expand All @@ -66,37 +66,37 @@ public class JsonSchemaDeserializer
"minimum",
(a, n) =>
{
a.Minimum = double.Parse(n.GetScalarValue(), NumberStyles.Float, Configuration.CultureInfo);
a.Minimum = double.Parse(n.GetScalarValue(), NumberStyles.Float, n.Context.Settings.CultureInfo);
}
},
{
"exclusiveMinimum", (a, n) => { a.ExclusiveMinimum = bool.Parse(n.GetScalarValue()); }
},
{
"maxLength", (a, n) => { a.MaxLength = int.Parse(n.GetScalarValue(), Configuration.CultureInfo); }
"maxLength", (a, n) => { a.MaxLength = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
},
{
"minLength", (a, n) => { a.MinLength = int.Parse(n.GetScalarValue(), Configuration.CultureInfo); }
"minLength", (a, n) => { a.MinLength = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
},
{
"pattern", (a, n) => { a.Pattern = n.GetScalarValue(); }
},
{
"maxItems", (a, n) => { a.MaxItems = int.Parse(n.GetScalarValue(), Configuration.CultureInfo); }
"maxItems", (a, n) => { a.MaxItems = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
},
{
"minItems", (a, n) => { a.MinItems = int.Parse(n.GetScalarValue(), Configuration.CultureInfo); }
"minItems", (a, n) => { a.MinItems = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
},
{
"uniqueItems", (a, n) => { a.UniqueItems = bool.Parse(n.GetScalarValue()); }
},
{
"maxProperties",
(a, n) => { a.MaxProperties = int.Parse(n.GetScalarValue(), Configuration.CultureInfo); }
(a, n) => { a.MaxProperties = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
},
{
"minProperties",
(a, n) => { a.MinProperties = int.Parse(n.GetScalarValue(), Configuration.CultureInfo); }
(a, n) => { a.MinProperties = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
},
{
"enum", (a, n) => { a.Enum = n.CreateListOfAny(); }
Expand Down
24 changes: 12 additions & 12 deletions src/LEGO.AsyncAPI.Readers/YamlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,51 @@

internal static class YamlConverter
{
public static JsonNode ToJsonNode(this YamlDocument yamlDocument)
public static JsonNode ToJsonNode(this YamlDocument yamlDocument, AsyncApiReaderSettings settings)
{
return yamlDocument.RootNode.ToJsonNode();
return yamlDocument.RootNode.ToJsonNode(settings);
}

public static JsonObject ToJsonObject(this YamlMappingNode yamlMappingNode)
public static JsonObject ToJsonObject(this YamlMappingNode yamlMappingNode, AsyncApiReaderSettings settings)
{
var node = new JsonObject();
foreach (var keyValuePair in yamlMappingNode)
{
var key = ((YamlScalarNode)keyValuePair.Key).Value!;
node[key] = keyValuePair.Value.ToJsonNode();
node[key] = keyValuePair.Value.ToJsonNode(settings);
}

return node;
}

public static JsonArray ToJsonArray(this YamlSequenceNode yaml)
public static JsonArray ToJsonArray(this YamlSequenceNode yaml, AsyncApiReaderSettings settings)
{
var node = new JsonArray();
foreach (var value in yaml)
{
node.Add(value.ToJsonNode());
node.Add(value.ToJsonNode(settings));
}

return node;
}

public static JsonNode ToJsonNode(this YamlNode yaml)
public static JsonNode ToJsonNode(this YamlNode yaml, AsyncApiReaderSettings settings)
{
return yaml switch
{
YamlMappingNode map => map.ToJsonObject(),
YamlSequenceNode seq => seq.ToJsonArray(),
YamlScalarNode scalar => scalar.ToJsonValue(),
YamlMappingNode map => map.ToJsonObject(settings),
YamlSequenceNode seq => seq.ToJsonArray(settings),
YamlScalarNode scalar => scalar.ToJsonValue(settings),
_ => throw new NotSupportedException("This yaml isn't convertible to JSON")
};
}

public static JsonValue ToJsonValue(this YamlScalarNode yaml)
private static JsonValue ToJsonValue(this YamlScalarNode yaml, AsyncApiReaderSettings settings)
{
switch (yaml.Style)
{
case ScalarStyle.Plain:
return decimal.TryParse(yaml.Value, NumberStyles.Float, Configuration.CultureInfo, out var d)
return decimal.TryParse(yaml.Value, NumberStyles.Float, settings.CultureInfo, out var d)
? JsonValue.Create(d)
: bool.TryParse(yaml.Value, out var b)
? JsonValue.Create(b)
Expand Down
Loading

0 comments on commit 74fff81

Please sign in to comment.