Skip to content

Commit

Permalink
Added dictionaryofobject resolvers, and updated the README.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwadams committed Sep 28, 2022
1 parent b7bd7c5 commit c1a0d20
Show file tree
Hide file tree
Showing 7 changed files with 485 additions and 131 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,39 @@ The `TParameterProvider` is a type which implements `ITemplateParameterProvider<

This allows you to process parameters as efficiently as possible, based on the types you need to support.

The benchmarks contain an example built over the low-allocation [Corvus.JsonSchema.ExtendedTypes](https://github.com/corvus-dotnet/Corvus.JsonSchema) called `JsonTemplateParameterProvider` that takes a parameter set based on a JSON object, supporting all JSON element types as parameter values.
The package `Corvus.UriTemplates.Resolvers.Json` contains a `JsonTemplateResolver` that takes a parameter set based on a `System.Text.Json.JsonElement` of `JsonValueKind.Object`. Its properties become the named parameters.

```csharp
using JsonDocument jsonValues = JsonDocument.Parse("{ \"foo\": \"bar\", \"bar\": \"baz\", \"baz\": \"bob\" }");
object? nullState = default;
JsonUriTemplateResolver.TryResolveResult(UriTemplate.AsSpan(), false, JsonValues, HandleResult, ref nullState);
JsonUriTemplateResolver.TryResolveResult(UriTemplate.AsSpan(), false, jsonValues.RootElement, HandleResult, ref nullState);

static void HandleResult(ReadOnlySpan<char> resolvedTemplate, ref object? state)
{
Do what you want with the resolved template!
// Do what you want with the resolved template
// (Typically, you use the state you have passed in to pprovide the resolved template
// to the outside world in some form.)
}
```

There are also overloads of `TryResolveResult` which will write to an `IBufferWriter<char>` instead of providing the `ReadOnlySpan<char>` to a callback.

Similarly, a resolver that takes parameters from a `Dictionary<string, object?>` can be found in the package `Corvus.UriTemplates.Resolvers.DictionaryOfObject`.

```csharp
using JsonDocument jsonValues = JsonDocument.Parse("{ \"foo\": \"bar\", \"bar\": \"baz\", \"baz\": \"bob\" }");
object? nullState = default;
JsonUriTemplateResolver.TryResolveResult(UriTemplate.AsSpan(), false, jsonValues.RootElement, HandleResult, ref nullState);

static void HandleResult(ReadOnlySpan<char> resolvedTemplate, ref object? state)
{
// Do what you want with the resolved template
// (Typically, you use the state you have passed in to pprovide the resolved template
// to the outside world in some form.)
}
```

You should examine the implementations of those types if you wish to implement your own low-allocation parameter providers.

## Build and test

Expand Down
20 changes: 0 additions & 20 deletions Solutions/Corvus.UriTemplate.Benchmarking/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,6 @@
"Microsoft.SourceLink.GitHub": "1.1.1"
}
},
"Roslynator.Analyzers": {
"type": "Direct",
"requested": "[4.1.1, )",
"resolved": "4.1.1",
"contentHash": "3cPVlrB1PytlO1ztZZBOExDKQWpMZgI15ZDa0BqLu0l6xv+xIRfEpqjNRcpvUy3aLxWTkPgSKZbbaO+VoFEJ1g=="
},
"StyleCop.Analyzers": {
"type": "Direct",
"requested": "[1.2.0-beta.435, )",
"resolved": "1.2.0-beta.435",
"contentHash": "TADk7vdGXtfTnYCV7GyleaaRTQjfoSfZXprQrVMm7cSJtJbFc1QIbWPyLvrgrfGdfHbGmUPvaN4ODKNxg2jgPQ==",
"dependencies": {
"StyleCop.Analyzers.Unstable": "1.2.0.435"
}
},
"Tavis.UriTemplates": {
"type": "Direct",
"requested": "[1.1.1, )",
Expand Down Expand Up @@ -360,11 +345,6 @@
"Microsoft.NETCore.Targets": "1.0.1"
}
},
"StyleCop.Analyzers.Unstable": {
"type": "Transitive",
"resolved": "1.2.0.435",
"contentHash": "ouwPWZxbOV3SmCZxIRqHvljkSzkCyi1tDoMzQtDb/bRP8ctASV/iRJr+A2Gdj0QLaLmWnqTWDrH82/iP+X80Lg=="
},
"System.AppContext": {
"type": "Transitive",
"resolved": "4.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(EndjinProjectPropsPath)" Condition="$(EndjinProjectPropsPath) != ''" />

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Description>Dictionary of object parameter provider for a low allocation implementation of URI template functions conforming to http://tools.ietf.org/html/rfc6570.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.0.0" />
<PackageReference Include="Endjin.RecommendedPractices.GitHub" Version="2.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Collections.Immutable" Version="7.0.0-rc.1.22426.10" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="7.0.0-rc.1.22427.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Corvus.UriTemplates\Corvus.UriTemplates.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="StyleCop.Analyzers" Version="1.2.0-beta.435" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Roslynator.Analyzers" Version="4.1.1" />
</ItemGroup>

</Project>
Loading

0 comments on commit c1a0d20

Please sign in to comment.