Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cultureinfo to reader/writer settings. #152

Merged
merged 15 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -130,7 +130,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,23 +11,25 @@ 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>
/// <param name="settings">The settings used for reading and writing.</param>
public AsyncApiUnsupportedSpecVersionException(string specificationVersion)
: base(string.Format(CultureInfo.InvariantCulture, 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(CultureInfo.InvariantCulture, MessagePattern, specificationVersion), innerException)
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
21 changes: 21 additions & 0 deletions 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 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 @@ -50,14 +50,14 @@ public class JsonSchemaDeserializer
"multipleOf",
(a, n) =>
{
a.MultipleOf = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
a.MultipleOf = double.Parse(n.GetScalarValue(), NumberStyles.Float, n.Context.Settings.CultureInfo);
}
},
{
"maximum",
(a, n) =>
{
a.Maximum = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
a.Maximum = double.Parse(n.GetScalarValue(), NumberStyles.Float, n.Context.Settings.CultureInfo);
}
},
{
Expand All @@ -67,37 +67,37 @@ public class JsonSchemaDeserializer
"minimum",
(a, n) =>
{
a.Minimum = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
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(), CultureInfo.InvariantCulture); }
"maxLength", (a, n) => { a.MaxLength = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
},
{
"minLength", (a, n) => { a.MinLength = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture); }
"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(), CultureInfo.InvariantCulture); }
"maxItems", (a, n) => { a.MaxItems = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
},
{
"minItems", (a, n) => { a.MinItems = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture); }
"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(), CultureInfo.InvariantCulture); }
(a, n) => { a.MaxProperties = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
},
{
"minProperties",
(a, n) => { a.MinProperties = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture); }
(a, n) => { a.MinProperties = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
},
{
"enum", (a, n) => { a.Enum = n.CreateListOfAny(); }
Expand Down
26 changes: 13 additions & 13 deletions src/LEGO.AsyncAPI.Readers/YamlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,55 @@ namespace LEGO.AsyncAPI.Readers

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"),
};
}

private 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, CultureInfo.InvariantCulture, 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)
: JsonValue.Create(yaml.Value)!;
: JsonValue.Create(yaml.Value) !;
case ScalarStyle.SingleQuoted:
case ScalarStyle.DoubleQuoted:
case ScalarStyle.Literal:
Expand Down
Loading
Loading