-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve AsyncApiAny api surface.
## About the PR Fixes some issues with working with AsyncApiAny, figuring out the correct types etc. Added new GetValue type methods for extracting expected values. These use `system.text.json` to deserialize to `T` from the `JsonNode` type. Added a static FromExtension method, to remove redundant type casting. So instead of ```csharp if (TryGetValue(key, out IAsyncApiExtension extension)) { var myType = (extension as AsyncApiAny).GetValue<MyType>(); } ``` You do ```csharp if (TryGetValue(key, out IAsyncApiExtension extension)) { var myType = AsyncApiAny.FromExtensionOrDefault<MyType>(extension); } ``` Added new constructor allowing for much simpler `AsyncApiAny` initialization, utlizing `system.json.text` to figure out the JsonNode type. ### Changelog - Added: `GetValue<T>()` - Added: `GetValueOrDefault<T>()` - Added: `TryGetValue<T>()` - Added: static `FromExtensionOrDefault<T>(IAsyncApiExtension extension)` - Added: new constructor to allow for easier object creation. - Obsoleted: `AsyncApiArray` - Obsoleted: `AsyncApiObject`
- Loading branch information
1 parent
b58f042
commit 5ebfbf8
Showing
8 changed files
with
209 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,13 @@ | |
|
||
namespace LEGO.AsyncAPI.Models | ||
{ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
using System.Text.Json.Nodes; | ||
using LEGO.AsyncAPI.Models.Interfaces; | ||
using LEGO.AsyncAPI.Writers; | ||
|
||
[Obsolete("Please use AsyncApiAny instead")] | ||
public class AsyncApiArray : Collection<AsyncApiAny>, IAsyncApiExtension, IAsyncApiElement | ||
{ | ||
Check warning on line 13 in src/LEGO.AsyncAPI/Models/Any/AsyncAPIArray.cs GitHub Actions / Publish NuGet packages (LEGO.AsyncAPI.Bindings)
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using LEGO.AsyncAPI.Models; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace LEGO.AsyncAPI.Tests | ||
{ | ||
|
||
public class AsyncApiAnyTests | ||
{ | ||
[Test] | ||
public void GetValue_ReturnsCorrectConversions() | ||
{ | ||
// Arrange | ||
// Act | ||
var a = new AsyncApiAny("string"); | ||
var b = new AsyncApiAny(1); | ||
var c = new AsyncApiAny(1.1); | ||
var d = new AsyncApiAny(true); | ||
var e = new AsyncApiAny(new MyType("test")); | ||
var f = new AsyncApiAny(new List<string>() { "test", "test2"}); | ||
var g = new AsyncApiAny(new List<string>() { "test", "test2"}.AsEnumerable()); | ||
var h = new AsyncApiAny(new List<MyType>() { new MyType("test") }); | ||
var i = new AsyncApiAny(new Dictionary<string, int>() { { "t", 2 } }); | ||
var j = new AsyncApiAny(new Dictionary<string, MyType>() { { "t", new MyType("test") } }); | ||
|
||
// Assert | ||
Assert.AreEqual("string", a.GetValue<string>()); | ||
Assert.AreEqual(1, b.GetValue<int>()); | ||
Assert.AreEqual(1.1, c.GetValue<double>()); | ||
Assert.AreEqual(true, d.GetValue<bool>()); | ||
Assert.NotNull(e.GetValue<MyType>()); | ||
Assert.IsNotEmpty(f.GetValue<List<string>>()); | ||
Assert.IsNotEmpty(f.GetValue<IEnumerable<string>>()); | ||
Assert.IsNotEmpty(g.GetValue<List<string>>()); | ||
Assert.IsNotEmpty(g.GetValue<IEnumerable<string>>()); | ||
Assert.IsNotEmpty(h.GetValue<List<MyType>>()); | ||
Assert.IsNotEmpty(h.GetValue<IEnumerable<MyType>>()); | ||
Assert.IsNotEmpty(i.GetValue<Dictionary<string, int>>()); | ||
Assert.IsNotEmpty(j.GetValue<Dictionary<string, MyType>>()); | ||
} | ||
|
||
class MyType | ||
{ | ||
public MyType(string value) | ||
{ | ||
this.Value = value; | ||
} | ||
|
||
public string Value { get; set; } | ||
} | ||
} | ||
} |