-
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4fe3f6
commit 9fcd086
Showing
4 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/StrawberryShake/Client/src/Core/Json/JsonExtensionParser.cs
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,18 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
|
||
namespace StrawberryShake.Json; | ||
|
||
public static class JsonExtensionParser | ||
{ | ||
public static IReadOnlyDictionary<string, object?>? ParseExtensions(JsonElement result) | ||
{ | ||
if (result is { ValueKind: JsonValueKind.Object }) | ||
{ | ||
var extensions = JsonSerializationHelper.ReadValue(result); | ||
return (IReadOnlyDictionary<string, object?>?)extensions; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
86 changes: 86 additions & 0 deletions
86
src/StrawberryShake/Client/test/Core.Tests/OperationResultBuilderTests.cs
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,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
|
||
namespace StrawberryShake; | ||
|
||
public class OperationResultBuilderTests | ||
{ | ||
[Fact] | ||
public void Build_With_Extensions() | ||
{ | ||
// arrange | ||
var factory = new DocumentDataFactory(); | ||
var builder = new DocumentOperationResultBuilder(factory); | ||
|
||
// According to the current design, all implementations of IConnection operate | ||
// on JsonDocuments which are deserialized straight from response streams and | ||
// very few properties in the Response object are in fact ever initialized, | ||
// including Response.Extensions. It is therefore safe to assume that, at | ||
// least for now, OperationResultBuilder is the best place to actually parse | ||
// and extract "extensions". | ||
var body = JsonDocument.Parse(@"{""data"": { }, ""extensions"": { ""a"": 1, ""b"": { ""c"": ""Strawberry"" }, ""d"": 3.14 } }"); | ||
var response = new Response<JsonDocument>(body, null); | ||
|
||
// act | ||
|
||
var result = builder.Build(response); | ||
|
||
// assert | ||
Assert.NotEmpty(result.Extensions); | ||
Assert.Equal(1L, result.Extensions["a"]); | ||
|
||
var b = (IReadOnlyDictionary<string, object?>?)result.Extensions["b"]; | ||
|
||
Assert.NotNull(b); | ||
Assert.Equal("Strawberry", b["c"]); | ||
|
||
Assert.Equal(3.14, result.Extensions["d"]); | ||
} | ||
|
||
internal class Document | ||
{ | ||
} | ||
|
||
internal class DocumentDataInfo : IOperationResultDataInfo | ||
{ | ||
public IReadOnlyCollection<EntityId> EntityIds { get; } = ArraySegment<EntityId>.Empty; | ||
|
||
public ulong Version { get; } = 0; | ||
|
||
public IOperationResultDataInfo WithVersion(ulong version) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
internal class DocumentDataFactory : IOperationResultDataFactory<Document> | ||
{ | ||
public Type ResultType { get => typeof(Document); } | ||
|
||
public Document Create(IOperationResultDataInfo dataInfo, IEntityStoreSnapshot? snapshot = null) | ||
{ | ||
return new Document(); | ||
} | ||
|
||
object IOperationResultDataFactory.Create(IOperationResultDataInfo dataInfo, IEntityStoreSnapshot? snapshot) | ||
{ | ||
return Create(dataInfo, snapshot); | ||
} | ||
} | ||
|
||
internal class DocumentOperationResultBuilder : OperationResultBuilder<Document> | ||
{ | ||
public DocumentOperationResultBuilder(IOperationResultDataFactory<Document> resultDataFactory) | ||
{ | ||
ResultDataFactory = resultDataFactory; | ||
} | ||
|
||
protected override IOperationResultDataFactory<Document> ResultDataFactory { get; } | ||
|
||
protected override IOperationResultDataInfo BuildData(JsonElement obj) | ||
{ | ||
return new DocumentDataInfo(); | ||
} | ||
} | ||
} |