Skip to content

Commit 0199420

Browse files
feat: add cultureinfo to reader/writer settings. (LEGO#152)
Co-authored-by: Alex Wichmann <[email protected]>
1 parent efcf8f0 commit 0199420

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1061
-787
lines changed

src/LEGO.AsyncAPI.Readers/AsyncApiJsonDocumentReader.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ internal class AsyncApiJsonDocumentReader : IAsyncApiReader<JsonNode, AsyncApiDi
2222
private readonly AsyncApiReaderSettings settings;
2323

2424
/// <summary>
25-
/// Create stream reader with custom settings if desired.
25+
/// Initializes a new instance of the <see cref="AsyncApiJsonDocumentReader"/> class.
2626
/// </summary>
27-
/// <param name="settings"></param>
27+
/// <param name="settings">The settings used to read json.</param>
2828
public AsyncApiJsonDocumentReader(AsyncApiReaderSettings settings = null)
2929
{
3030
this.settings = settings ?? new AsyncApiReaderSettings();
@@ -39,7 +39,7 @@ public AsyncApiJsonDocumentReader(AsyncApiReaderSettings settings = null)
3939
public AsyncApiDocument Read(JsonNode input, out AsyncApiDiagnostic diagnostic)
4040
{
4141
diagnostic = new AsyncApiDiagnostic();
42-
var context = new ParsingContext(diagnostic)
42+
var context = new ParsingContext(diagnostic, this.settings)
4343
{
4444
ExtensionParsers = this.settings.ExtensionParsers,
4545
ServerBindingParsers = this.settings.Bindings.OfType<IBindingParser<IServerBinding>>().ToDictionary(b => b.BindingKey, b => b),
@@ -80,7 +80,7 @@ public AsyncApiDocument Read(JsonNode input, out AsyncApiDiagnostic diagnostic)
8080
public async Task<ReadResult> ReadAsync(JsonNode input, CancellationToken cancellationToken = default)
8181
{
8282
var diagnostic = new AsyncApiDiagnostic();
83-
var context = new ParsingContext(diagnostic)
83+
var context = new ParsingContext(diagnostic, this.settings)
8484
{
8585
ExtensionParsers = this.settings.ExtensionParsers,
8686
};
@@ -130,7 +130,7 @@ public T ReadFragment<T>(JsonNode input, AsyncApiVersion version, out AsyncApiDi
130130
where T : IAsyncApiElement
131131
{
132132
diagnostic = new AsyncApiDiagnostic();
133-
var context = new ParsingContext(diagnostic)
133+
var context = new ParsingContext(diagnostic, this.settings)
134134
{
135135
ExtensionParsers = this.settings.ExtensionParsers,
136136
ServerBindingParsers = this.settings.Bindings.OfType<IBindingParser<IServerBinding>>().ToDictionary(b => b.BindingKey, b => b),

src/LEGO.AsyncAPI.Readers/AsyncApiReaderSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public enum ReferenceResolutionSetting
2626
/// <summary>
2727
/// Configuration settings to control how AsyncApi documents are parsed.
2828
/// </summary>
29-
public class AsyncApiReaderSettings
29+
public class AsyncApiReaderSettings : AsyncApiSettings
3030
{
3131
/// <summary>
3232
/// Indicates how references in the source document should be handled.

src/LEGO.AsyncAPI.Readers/AsyncApiTextReader.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace LEGO.AsyncAPI.Readers
44
{
55
using System.IO;
66
using System.Linq;
7+
using System.Runtime.CompilerServices;
78
using System.Text.Json;
89
using System.Text.Json.Nodes;
910
using System.Threading;
@@ -42,7 +43,7 @@ public AsyncApiDocument Read(TextReader input, out AsyncApiDiagnostic diagnostic
4243
// Parse the YAML/JSON text in the TextReader into the YamlDocument
4344
try
4445
{
45-
jsonNode = LoadYamlDocument(input);
46+
jsonNode = LoadYamlDocument(input, this.settings);
4647
}
4748
catch (JsonException ex)
4849
{
@@ -69,7 +70,7 @@ public async Task<ReadResult> ReadAsync(TextReader input, CancellationToken canc
6970
// Parse the YAML/JSON text in the TextReader into the YamlDocument
7071
try
7172
{
72-
jsonNode = LoadYamlDocument(input);
73+
jsonNode = LoadYamlDocument(input, this.settings);
7374
}
7475
catch (JsonException ex)
7576
{
@@ -100,7 +101,7 @@ public T ReadFragment<T>(TextReader input, AsyncApiVersion version, out AsyncApi
100101
// Parse the YAML/JSON
101102
try
102103
{
103-
jsonNode = LoadYamlDocument(input);
104+
jsonNode = LoadYamlDocument(input, this.settings);
104105
}
105106
catch (JsonException ex)
106107
{
@@ -118,11 +119,11 @@ public T ReadFragment<T>(TextReader input, AsyncApiVersion version, out AsyncApi
118119
/// </summary>
119120
/// <param name="input">Stream containing YAML formatted text.</param>
120121
/// <returns>Instance of a YamlDocument.</returns>
121-
static JsonNode LoadYamlDocument(TextReader input)
122+
static JsonNode LoadYamlDocument(TextReader input, AsyncApiReaderSettings settings)
122123
{
123124
var yamlStream = new YamlStream();
124125
yamlStream.Load(input);
125-
return yamlStream.Documents.First().ToJsonNode();
126+
return yamlStream.Documents.First().ToJsonNode(settings);
126127
}
127128
}
128129
}

src/LEGO.AsyncAPI.Readers/Exceptions/AsyncApiUnsupportedSpecVersionException.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,25 @@ namespace LEGO.AsyncAPI.Readers.Exceptions
1111
[Serializable]
1212
public class AsyncApiUnsupportedSpecVersionException : Exception
1313
{
14-
const string MessagePattern = "AsyncApi specification version '{0}' is not supported.";
14+
private const string MessagePattern = "AsyncApi specification version '{0}' is not supported.";
1515

1616
/// <summary>
17-
/// Initializes the <see cref="AsyncApiUnsupportedSpecVersionException"/> class with a specification version.
17+
/// Initializes a new instance of the <see cref="AsyncApiUnsupportedSpecVersionException"/> class.
1818
/// </summary>
1919
/// <param name="specificationVersion">Version that caused this exception to be thrown.</param>
20+
/// <param name="settings">The settings used for reading and writing.</param>
2021
public AsyncApiUnsupportedSpecVersionException(string specificationVersion)
2122
: base(string.Format(CultureInfo.InvariantCulture, MessagePattern, specificationVersion))
2223
{
2324
this.SpecificationVersion = specificationVersion;
2425
}
2526

2627
/// <summary>
27-
/// Initializes the <see cref="AsyncApiUnsupportedSpecVersionException"/> class with a specification version and
28+
/// Initializes a new instance of the <see cref="AsyncApiUnsupportedSpecVersionException"/> class.
2829
/// inner exception.
2930
/// </summary>
3031
/// <param name="specificationVersion">Version that caused this exception to be thrown.</param>
32+
/// <param name="settings">The setting used for reading and writing</param>
3133
/// <param name="innerException">Inner exception that caused this exception to be thrown.</param>
3234
public AsyncApiUnsupportedSpecVersionException(string specificationVersion, Exception innerException)
3335
: base(string.Format(CultureInfo.InvariantCulture, MessagePattern, specificationVersion), innerException)

src/LEGO.AsyncAPI.Readers/JsonHelper.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/LEGO.AsyncAPI.Readers/ParseNodes/MapNode.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace LEGO.AsyncAPI.Readers.ParseNodes
88
using System.Linq;
99
using System.Text.Json;
1010
using System.Text.Json.Nodes;
11+
using LEGO.AsyncAPI.Exceptions;
1112
using LEGO.AsyncAPI.Models;
1213
using LEGO.AsyncAPI.Models.Interfaces;
1314
using LEGO.AsyncAPI.Readers.Exceptions;
@@ -18,7 +19,7 @@ public class MapNode : ParseNode, IEnumerable<PropertyNode>
1819
private readonly List<PropertyNode> nodes;
1920

2021
public MapNode(ParsingContext context, string jsonString)
21-
: this(context, JsonHelper.ParseJsonString(jsonString))
22+
: this(context, JsonNode.Parse(jsonString))
2223
{
2324
}
2425

@@ -196,7 +197,7 @@ public string GetReferencePointer()
196197
return null;
197198
}
198199

199-
return refNode.GetScalarValue();
200+
return this.ToScalarValue(refNode);
200201
}
201202

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

208-
return scalarNode.GetScalarValue();
209+
return this.ToScalarValue(scalarNode);
209210
}
210211

211212
public override AsyncApiAny CreateAny()
212213
{
213214
return new AsyncApiAny(this.node);
214215
}
216+
217+
private string ToScalarValue(JsonNode node)
218+
{
219+
var scalarNode = node is JsonValue value ? value : throw new AsyncApiException($"Expected scalar value");
220+
return Convert.ToString(scalarNode.GetValue<object>(), this.Context.Settings.CultureInfo);
221+
}
215222
}
216223
}

src/LEGO.AsyncAPI.Readers/ParseNodes/ValueNode.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace LEGO.AsyncAPI.Readers.ParseNodes
44
{
5+
using LEGO.AsyncAPI.Exceptions;
56
using LEGO.AsyncAPI.Models;
67
using LEGO.AsyncAPI.Readers.Exceptions;
8+
using System;
79
using System.Text.Json.Nodes;
810

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

3236
return this.cachedScalarValue;

src/LEGO.AsyncAPI.Readers/ParsingContext.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,30 @@ internal Dictionary<string, Func<AsyncApiAny, IAsyncApiExtension>> ExtensionPars
3939

4040
public AsyncApiDiagnostic Diagnostic { get; }
4141

42+
/// <summary>
43+
/// Gets the settings used fore reading json.
44+
/// </summary>
45+
public AsyncApiReaderSettings Settings { get; }
46+
47+
///// <summary>
48+
///// Initializes a new instance of the <see cref="ParsingContext"/> class.
49+
///// </summary>
50+
/// <param name="diagnostic">The diagnostics.</param>
51+
[Obsolete($"Please use the overloaded version that takes in an instance of {nameof(AsyncApiReaderSettings)} isntead.")]
4252
public ParsingContext(AsyncApiDiagnostic diagnostic)
53+
: this(diagnostic, new AsyncApiReaderSettings())
54+
{
55+
}
56+
57+
/// <summary>
58+
/// Initializes a new instance of the <see cref="ParsingContext"/> class.
59+
/// </summary>
60+
/// <param name="diagnostic">The diagnostics.</param>
61+
/// <param name="settings">The settings used to read json.</param>
62+
public ParsingContext(AsyncApiDiagnostic diagnostic, AsyncApiReaderSettings settings)
4363
{
4464
this.Diagnostic = diagnostic;
65+
this.Settings = settings;
4566
}
4667

4768
internal AsyncApiDocument Parse(JsonNode jsonNode)

src/LEGO.AsyncAPI.Readers/V2/AsyncApiSchemaDeserializer.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ public class JsonSchemaDeserializer
5050
"multipleOf",
5151
(a, n) =>
5252
{
53-
a.MultipleOf = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
53+
a.MultipleOf = double.Parse(n.GetScalarValue(), NumberStyles.Float, n.Context.Settings.CultureInfo);
5454
}
5555
},
5656
{
5757
"maximum",
5858
(a, n) =>
5959
{
60-
a.Maximum = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
60+
a.Maximum = double.Parse(n.GetScalarValue(), NumberStyles.Float, n.Context.Settings.CultureInfo);
6161
}
6262
},
6363
{
@@ -67,37 +67,37 @@ public class JsonSchemaDeserializer
6767
"minimum",
6868
(a, n) =>
6969
{
70-
a.Minimum = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
70+
a.Minimum = double.Parse(n.GetScalarValue(), NumberStyles.Float, n.Context.Settings.CultureInfo);
7171
}
7272
},
7373
{
7474
"exclusiveMinimum", (a, n) => { a.ExclusiveMinimum = bool.Parse(n.GetScalarValue()); }
7575
},
7676
{
77-
"maxLength", (a, n) => { a.MaxLength = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture); }
77+
"maxLength", (a, n) => { a.MaxLength = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
7878
},
7979
{
80-
"minLength", (a, n) => { a.MinLength = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture); }
80+
"minLength", (a, n) => { a.MinLength = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
8181
},
8282
{
8383
"pattern", (a, n) => { a.Pattern = n.GetScalarValue(); }
8484
},
8585
{
86-
"maxItems", (a, n) => { a.MaxItems = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture); }
86+
"maxItems", (a, n) => { a.MaxItems = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
8787
},
8888
{
89-
"minItems", (a, n) => { a.MinItems = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture); }
89+
"minItems", (a, n) => { a.MinItems = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
9090
},
9191
{
9292
"uniqueItems", (a, n) => { a.UniqueItems = bool.Parse(n.GetScalarValue()); }
9393
},
9494
{
9595
"maxProperties",
96-
(a, n) => { a.MaxProperties = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture); }
96+
(a, n) => { a.MaxProperties = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
9797
},
9898
{
9999
"minProperties",
100-
(a, n) => { a.MinProperties = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture); }
100+
(a, n) => { a.MinProperties = int.Parse(n.GetScalarValue(), n.Context.Settings.CultureInfo); }
101101
},
102102
{
103103
"enum", (a, n) => { a.Enum = n.CreateListOfAny(); }

src/LEGO.AsyncAPI.Readers/YamlConverter.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,55 @@ namespace LEGO.AsyncAPI.Readers
1010

1111
internal static class YamlConverter
1212
{
13-
public static JsonNode ToJsonNode(this YamlDocument yamlDocument)
13+
public static JsonNode ToJsonNode(this YamlDocument yamlDocument, AsyncApiReaderSettings settings)
1414
{
15-
return yamlDocument.RootNode.ToJsonNode();
15+
return yamlDocument.RootNode.ToJsonNode(settings);
1616
}
1717

18-
public static JsonObject ToJsonObject(this YamlMappingNode yamlMappingNode)
18+
public static JsonObject ToJsonObject(this YamlMappingNode yamlMappingNode, AsyncApiReaderSettings settings)
1919
{
2020
var node = new JsonObject();
2121
foreach (var keyValuePair in yamlMappingNode)
2222
{
2323
var key = ((YamlScalarNode)keyValuePair.Key).Value!;
24-
node[key] = keyValuePair.Value.ToJsonNode();
24+
node[key] = keyValuePair.Value.ToJsonNode(settings);
2525
}
2626

2727
return node;
2828
}
2929

30-
public static JsonArray ToJsonArray(this YamlSequenceNode yaml)
30+
public static JsonArray ToJsonArray(this YamlSequenceNode yaml, AsyncApiReaderSettings settings)
3131
{
3232
var node = new JsonArray();
3333
foreach (var value in yaml)
3434
{
35-
node.Add(value.ToJsonNode());
35+
node.Add(value.ToJsonNode(settings));
3636
}
3737

3838
return node;
3939
}
4040

41-
public static JsonNode ToJsonNode(this YamlNode yaml)
41+
public static JsonNode ToJsonNode(this YamlNode yaml, AsyncApiReaderSettings settings)
4242
{
4343
return yaml switch
4444
{
45-
YamlMappingNode map => map.ToJsonObject(),
46-
YamlSequenceNode seq => seq.ToJsonArray(),
47-
YamlScalarNode scalar => scalar.ToJsonValue(),
45+
YamlMappingNode map => map.ToJsonObject(settings),
46+
YamlSequenceNode seq => seq.ToJsonArray(settings),
47+
YamlScalarNode scalar => scalar.ToJsonValue(settings),
4848
_ => throw new NotSupportedException("This yaml isn't convertible to JSON"),
4949
};
5050
}
5151

52-
private static JsonValue ToJsonValue(this YamlScalarNode yaml)
52+
private static JsonValue ToJsonValue(this YamlScalarNode yaml, AsyncApiReaderSettings settings)
5353
{
5454
switch (yaml.Style)
5555
{
5656
case ScalarStyle.Plain:
57-
return decimal.TryParse(yaml.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var d)
57+
return decimal.TryParse(yaml.Value, NumberStyles.Float, settings.CultureInfo, out var d)
5858
? JsonValue.Create(d)
5959
: bool.TryParse(yaml.Value, out var b)
6060
? JsonValue.Create(b)
61-
: JsonValue.Create(yaml.Value)!;
61+
: JsonValue.Create(yaml.Value) !;
6262
case ScalarStyle.SingleQuoted:
6363
case ScalarStyle.DoubleQuoted:
6464
case ScalarStyle.Literal:

0 commit comments

Comments
 (0)