-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fluent API includes json-oriented features (#110)
- Loading branch information
Showing
18 changed files
with
378 additions
and
21 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
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,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using Streamistry.Fluent; | ||
using Streamistry.Testability; | ||
using Streamistry.Json.Fluent; | ||
|
||
namespace Streamistry.Json.Testing.Fluent; | ||
public class ArrayParserTests | ||
{ | ||
[Test] | ||
public void ParseAsJsonArray_ValidEntry_Successful() | ||
{ | ||
var pipeline = new PipelineBuilder() | ||
.Source([ | ||
$"[{JsonTests.JsonFirst}, {JsonTests.JsonSecond}, {JsonTests.JsonThird}]", | ||
$"[{JsonTests.JsonFirst}, {JsonTests.JsonThird}]", | ||
$"[{JsonTests.JsonThird}]" | ||
]) | ||
.Parse() | ||
.AsJsonArray() | ||
.Checkpoint(out var parser) | ||
.Build(); | ||
|
||
var outputs = parser.GetOutputs(pipeline.Start); | ||
Assert.That(outputs, Has.Length.EqualTo(3)); | ||
Assert.That(outputs, Has.All.Not.Null); | ||
Assert.That(outputs, Has.All.TypeOf<JsonArray>()); | ||
} | ||
} |
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,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using Streamistry.Fluent; | ||
using Streamistry.Testability; | ||
using Streamistry.Json.Fluent; | ||
|
||
namespace Streamistry.Json.Testing.Fluent; | ||
public class ArrayPathPluckerTests | ||
{ | ||
[Test] | ||
public void Pluck_ValidSinglePath_ExistingValue() | ||
{ | ||
var pipeline = new PipelineBuilder() | ||
.Source([ | ||
(JsonArray)JsonNode.Parse( | ||
$"[{JsonTests.JsonFirst}, {JsonTests.JsonSecond}, {JsonTests.JsonThird}]" | ||
)! | ||
]) | ||
.Pluck<string>("$[1].user.contact.email") | ||
.Checkpoint(out var pluck) | ||
.Build(); | ||
|
||
var outputs = pluck.GetOutputs(pipeline.Start); | ||
Assert.That(outputs, Has.Length.EqualTo(1)); | ||
Assert.That(outputs[0], Does.Contain("[email protected]")); | ||
} | ||
} |
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,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using Streamistry.Fluent; | ||
using Streamistry.Testability; | ||
using Streamistry.Json.Fluent; | ||
|
||
namespace Streamistry.Json.Testing.Fluent; | ||
public class ArraySplitterTests | ||
{ | ||
[Test] | ||
public void Split_ValidEntry_Successful() | ||
{ | ||
var pipeline = new PipelineBuilder() | ||
.Source([ | ||
$"[{JsonTests.JsonFirst}, {JsonTests.JsonSecond}, {JsonTests.JsonThird}]", | ||
$"[{JsonTests.JsonFirst}, {JsonTests.JsonThird}]", | ||
]) | ||
.Parse() | ||
.AsJsonArray() | ||
.Split() | ||
.Checkpoint(out var splitter) | ||
.Build(); | ||
|
||
var outputs = splitter.GetOutputs(pipeline.Start); | ||
Assert.That(outputs, Has.Length.EqualTo(5)); | ||
Assert.That(outputs, Has.All.Not.Null); | ||
Assert.That(outputs, Has.All.TypeOf<JsonObject>()); | ||
} | ||
} |
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,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using Streamistry.Fluent; | ||
using Streamistry.Testability; | ||
using Streamistry.Json.Fluent; | ||
|
||
namespace Streamistry.Json.Testing.Fluent; | ||
public class ObjectParserTests | ||
{ | ||
[Test] | ||
public void ParseAsJsonObject_ValidEntries_Successful() | ||
{ | ||
var pipeline = new PipelineBuilder() | ||
.Source([JsonTests.JsonFirst, JsonTests.JsonSecond, JsonTests.JsonThird]) | ||
.Parse() | ||
.AsJsonObject() | ||
.Checkpoint(out var parser) | ||
.Build(); | ||
|
||
var outputs = parser.GetOutputs(pipeline.Start); | ||
Assert.That(outputs, Has.Length.EqualTo(3)); | ||
Assert.That(outputs, Has.All.Not.Null); | ||
Assert.That(outputs, Has.All.TypeOf<JsonObject>()); | ||
} | ||
} |
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,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using Streamistry.Fluent; | ||
using Streamistry.Testability; | ||
using Streamistry.Json.Fluent; | ||
|
||
namespace Streamistry.Json.Testing.Fluent; | ||
public class PathPluckerTests | ||
{ | ||
[Test] | ||
[TestCase(JsonTests.JsonFirst, "[email protected]")] | ||
[TestCase(JsonTests.JsonThird, null)] | ||
public void Pluck_ValidPath_ExistingValue(string jsonString, string? email) | ||
{ | ||
var pipeline = new PipelineBuilder() | ||
.Source([(JsonObject)JsonNode.Parse(jsonString)!]) | ||
.Pluck<string>("$.user.contact.email") | ||
.Checkpoint(out var pluck) | ||
.Build(); | ||
|
||
var outputs = pluck.GetOutputs(pipeline.Start); | ||
Assert.That(outputs, Has.Length.EqualTo(1)); | ||
Assert.That(outputs, Does.Contain(email)); | ||
} | ||
|
||
[Test] | ||
[TestCase(JsonTests.JsonThird, null)] | ||
public void Pluck_NonExistingPath_Null(string jsonString, string? email) | ||
{ | ||
var pipeline = new PipelineBuilder() | ||
.Source([(JsonObject)JsonNode.Parse(jsonString)!]) | ||
.Pluck<string>("$.user.contact.email") | ||
.Checkpoint(out var pluck) | ||
.Build(); | ||
|
||
var outputs = pluck.GetOutputs(pipeline.Start); | ||
Assert.That(outputs, Has.Length.EqualTo(1)); | ||
Assert.That(outputs, Does.Contain(email)); | ||
} | ||
} |
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,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using Streamistry.Fluent; | ||
using Streamistry.Testability; | ||
using Streamistry.Json.Fluent; | ||
using System.Globalization; | ||
|
||
namespace Streamistry.Json.Testing.Fluent; | ||
public class ValueMapperTests | ||
{ | ||
[Test] | ||
public void AsJsonValue_ValidEntry_Successful() | ||
{ | ||
var pipeline = new PipelineBuilder() | ||
.Source([new DateOnly(1879, 3, 14), new DateOnly(1856, 7, 10), new DateOnly(1903, 12, 28)]) | ||
.AsJsonValue() | ||
.Checkpoint(out var mapper) | ||
.Build(); | ||
|
||
var outputs = mapper.GetOutputs(pipeline.Start); | ||
Assert.Multiple(() => { | ||
Assert.That(outputs, Has.Length.EqualTo(3)); | ||
Assert.That(outputs, Has.All.Not.Null); | ||
Assert.That(outputs, Has.All.AssignableTo<JsonValue>()); | ||
}); | ||
Assert.That(outputs.Select(x => x!.ToJsonString()), Has.One.EqualTo("\"1879-03-14\"")); | ||
} | ||
|
||
[Test] | ||
public void AsJsonValue_ValidEntryWithCustomFunction_Successful() | ||
{ | ||
var pipeline = new PipelineBuilder() | ||
.Source([new DateOnly(1879, 3, 14), new DateOnly(1856, 7, 10), new DateOnly(1903, 12, 28)]) | ||
.AsJsonValue(x => $"{x.ToString("MMMM", CultureInfo.InvariantCulture)} {x.Year}") | ||
.Checkpoint(out var mapper) | ||
.Build(); | ||
|
||
var outputs = mapper.GetOutputs(pipeline.Start); | ||
Assert.Multiple(() => { | ||
Assert.That(outputs, Has.Length.EqualTo(3)); | ||
Assert.That(outputs, Has.All.Not.Null); | ||
Assert.That(outputs, Has.All.AssignableTo<JsonValue>()); | ||
}); | ||
Assert.That(outputs.Select(x => x!.ToJsonString()), Has.One.EqualTo("\"March 1879\"")); | ||
} | ||
} |
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,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using Streamistry.Fluent; | ||
|
||
namespace Streamistry.Json.Fluent; | ||
public class ArrayPathPluckerBuilder<TOutput> : PipeElementBuilder<JsonArray, TOutput> | ||
{ | ||
protected string Path { get; set; } | ||
|
||
public ArrayPathPluckerBuilder(IPipeBuilder<JsonArray> upstream, string path) | ||
: base(upstream) | ||
=> (Path) = (path); | ||
|
||
public override IChainablePort<TOutput> OnBuildPipeElement() | ||
=> new PathArrayPlucker<TOutput>( | ||
Upstream.BuildPipeElement() | ||
, Path ?? throw new InvalidOperationException() | ||
); | ||
} |
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,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using Streamistry.Fluent; | ||
|
||
namespace Streamistry.Json.Fluent; | ||
public class ArraySplitterBuilder : PipeElementBuilder<JsonArray, JsonObject> | ||
{ | ||
public ArraySplitterBuilder(IPipeBuilder<JsonArray> upstream) | ||
: base(upstream) | ||
{ } | ||
|
||
public override IChainablePort<JsonObject> OnBuildPipeElement() | ||
=> new ArraySplitter( | ||
Upstream.BuildPipeElement() | ||
); | ||
} |
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,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using Streamistry.Fluent; | ||
|
||
namespace Streamistry.Json.Fluent; | ||
|
||
public static class BasePipeBuilderExtension | ||
{ | ||
public static ValueMapperBuilder<TInput> AsJsonValue<TInput>(this BasePipeBuilder<TInput> builder, Func<TInput, string>? toString = null) | ||
=> new(builder, toString); | ||
|
||
public static PathPluckerBuilder<TOutput> Pluck<TOutput>(this BasePipeBuilder<JsonObject> builder, string path) | ||
=> new(builder, path); | ||
|
||
public static ArrayPathPluckerBuilder<TOutput> Pluck<TOutput>(this BasePipeBuilder<JsonArray> builder, string path) | ||
=> new(builder, path); | ||
|
||
public static ArraySplitterBuilder Split(this BasePipeBuilder<JsonArray> builder) | ||
=> new(builder); | ||
} | ||
|
||
public static class ParserBuilderExtension | ||
{ | ||
public static SpecializedParserBuilder<TInput, JsonObject> AsJsonObject<TInput>(this ParserBuilder<TInput> builder) | ||
=> new(builder.Upstream, typeof(ObjectParser), null); | ||
|
||
public static SpecializedParserBuilder<TInput, JsonArray> AsJsonArray<TInput>(this ParserBuilder<TInput> builder) | ||
=> new(builder.Upstream, typeof(ArrayParser), null); | ||
} |
Oops, something went wrong.