DataDrivenConstants 1.1.0
dotnet add package DataDrivenConstants --version 1.1.0
NuGet\Install-Package DataDrivenConstants -Version 1.1.0
<PackageReference Include="DataDrivenConstants" Version="1.1.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="DataDrivenConstants" Version="1.1.0" />
<PackageReference Include="DataDrivenConstants"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add DataDrivenConstants --version 1.1.0
#r "nuget: DataDrivenConstants, 1.1.0"
#:package DataDrivenConstants@1.1.0
#addin nuget:?package=DataDrivenConstants&version=1.1.0
#tool nuget:?package=DataDrivenConstants&version=1.1.0
DataDrivenConstants
If you have ever worked on a project that stores a lot of configuration data, you've likely felt the pain of redefining constants to access data in those files from your code. DataDrivenConstants fixes this by automatically generating constant classes from your data. Simply create a (usually empty) static partial class with a marker attribute to define the data you want and the constants will be added to that class.
DataDrivenConstants currently only supports JSON files, but additional formats may be supported in the future.
Adding data files
Data files are accessed by the source generator as AdditionalFiles items in your csproj, for example:
<ItemGroup>
<AdditionalFiles Include="Resources/foo.json"/>
<AdditionalFiles Include="Resources/Nested/bar.json"/>
</ItemGroup>
If you want to send all items of a specific type (e.g. embedded resources) to the analyzer, you can also
set the AdditionalFileItemNames property, like so:
<PropertyGroup>
<AdditionalFileItemNames>EmbeddedResource</AdditionalFileItemNames>
</PropertyGroup>
All data types supported by DataDrivenConstants use globs
to select which files will contain the data. In the simplest class, the glob **/* will match all files, but
you can of course be more selective if you want to split your data across multiple files. It is highly
recommended that you start all your globs with **, AdditionalFiles are typically converted to absolute paths
before the source generator processes them.
JSON
JSON files can be accessed using the DataDrivenConstants.Marker.JsonData attribute, for example:
using DataDrivenConstants.Marker;
// gets the "name" property of all objects in the root list
[JsonData("$[*].name", "**/listOfObjects.json")]
public static partial class MyConstants {}
The query syntax used is a slight extension of JSONPath which supports
a ~ operator at the end of a query to get the keys of the selected node. This can be used to get keys of an object;
while $.* will give you all values of an object, $.*~ will give the keys of the object.
This website can be used to test your JSONPath; the ~ operator is supported in the JSONPath Plus`
parser (note that it also supports other features which are not supported here).
JSONPath cookbook
$.*- get all values of the root object$.*~- get all keys of the root object$[*]- get all elements of the root array$[*].name- get the value of thenameproperty for all elements of the root array$.*[*]- in a object where all values are lists, gets all elements of all the lists
Replacements
In the general case, the generator will perform a mostly minimal set of replacements to make the selected value into a legal
C# symbol (details here).
In some cases however, more sophisticated replacement rules may be needed. Custom replacements can be specified with the
DataDrivenConstants.Marker.ReplacementRule attribute. Multiple rules can be applied to a class and they will be evaluated
in order. Regex replacement is also supported.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.