DeltaWare.SDK.SmartFormat 6.0.1.1

dotnet add package DeltaWare.SDK.SmartFormat --version 6.0.1.1
NuGet\Install-Package DeltaWare.SDK.SmartFormat -Version 6.0.1.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DeltaWare.SDK.SmartFormat" Version="6.0.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DeltaWare.SDK.SmartFormat --version 6.0.1.1
#r "nuget: DeltaWare.SDK.SmartFormat, 6.0.1.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DeltaWare.SDK.SmartFormat as a Cake Addin
#addin nuget:?package=DeltaWare.SDK.SmartFormat&version=6.0.1.1

// Install DeltaWare.SDK.SmartFormat as a Cake Tool
#tool nuget:?package=DeltaWare.SDK.SmartFormat&version=6.0.1.1

Overview

SmartFormat was built to provide a tool that supported the following requirements.

  • Concatenation
  • Optional Variables
  • Unordered arguments.
  • Continued support for standard .Net formatting.

Whilst SmartFormat does have some similarities with the standard .Net way of string formatting it does differ in some significant ways.

Declaring a Variable

A variable is declared using the following syntax {myVariable}. A declared variable can also be decorated to change how it is formatted.

  • Optional Variable - Appending a ? at the end of a variable name informs the parser that if the associated argument is not found for the variable it can be ignored and thus not included in the final output string.
  • Value Formatting - Appending :{Format String} after a variables name describes what kind of formatting should be used for the variable.

If you would like to use a { or } in your template without it being treated as key for variable declaration simply double it like {{ or }} and it will be ignored, the final string output will remove the duplicate.

Now that you've seen how to declare variables let's show some example.

Example 1

var format  = "FirstName: {firstName}";

var args = new Dictionary<string, object>
{
	{ "firstName", "John" }
};

var result = SmartFormat.Parse(format, args);

// Result: "FirstName: John"

Example 2

var format  = "FirstName: {firstName} LastName: {lastName?}";

var args = new Dictionary<string, object>
{
	{ "firstName", "John" }
};

var result = SmartFormat.Parse(format, args);

// Result: "FirstName: John LastName: "

Example 3

var format  = "{date?:d}";

var args = new Dictionary<string, object>
{
	{ "date", new DateTime(2020, 05, 05) }
};

var result = SmartFormat.Parse(format, args);

// Result: "5/05/2020"

Example 4

var format  = "{date?:d}";

var args = new Dictionary<string, object>();

var result = SmartFormat.Parse(format, args);

// Result: ""

Declaring a Concatenated Variable

As you could see from the above examples. It almost acts the same as .Nets string.Format. But now we're going to discuss concatenated variables.

Concatenated variables in conjuctions with nullable variables allows for some really powerful formatting to be done with very little effort.

A concatenated variable is declared using the following syntax [{myVariable}]. A concatenated variable declared like this will act example the same as a standard variable - but we can also decorated it like the following [LastName: {lastName}]. In this example we've added LastName: inside the square brackets to specify the start concatenation, it is also possible to decorate the back like this [{seconds} Seconds].

If you would like to use a [ or ] in your template without it being treated as key for concatenated variable declaration simply double it like [[ or ]] and it will be ignored, the final string output will remove the duplicate.

Now that you've seen how to declare concatenated variables let's show some example.

Example 1

var format  = "[FirstName: {firstName}] [LastName: {lastName?}]";

var args = new Dictionary<string, object>
{
	{ "firstName", "John" }
};

var result = SmartFormat.Parse(format, args);

// Result: "FirstName: John"

Example 2

var format  = "[{days?} Days] [{hours?} Hours] [{minutes?} Minutes] [and {seconds?}] Seconds";

var args = new Dictionary<string, object>
{
	{ "hours", 5 },
	{ "seconds", 30 }
};

var result = SmartFormat.Parse(format, args);

// Result: "5 Hours and 30 Seconds"

Example 3

var format  = "{baseUrl}/api/{resource}[/{template}][?{query}]";

var args = new Dictionary<string, object>
{
	{ "baseUrl", "http://localhost" }, 
	{ "resource", "Students" }, 
	{ "template", 5 }
};

var result = SmartFormat.Parse(format, args);

// Result: "http://localhost/api/resource/Students/5"

Example 4

var format  = "{baseUrl}/api/{resource}[/{template}][?{query}]";

var args = new Dictionary<string, object>
{
	{ "baseUrl", "http://localhost" }, 
	{ "resource", "Students" }, 
	{ "query", "firstName=John&lastName=Smith" }
};

var result = SmartFormat.Parse(format, args);

// Result: "http://localhost/api/resource/Students?firstName=John&lastName=Smith"

Code Example

Parse from Dictionary Arguments

string template = "{baseUrl}/api/{resource}[/{template}][?{query}]";

Dictionary<string, object> args = new Dictionary<string, object>
{
	{ "baseUrl", "http://localhost" }, 
	{ "resource", "Students" }, 
	{ "query", "firstName=John&lastName=Smith" }
};

string text = SmartFormat.Parse(template, args);

Parse from object

string template = "{BaseUrl}/api/{Resource}[/{Template}][?{Query}]";

UriTemplate uriTempalte = new UriTemplate
{
	BaseUrl = "http://localhost",
	Resource = "Students",
	Query = "firstName=John&lastName=Smith"
};

string text = SmartFormat.Parse(template, uriTempalte);
string template = "{BaseUrl}/api/{Resource}[/{Template}][?{Query}]";

string text = SmartFormat.Parse(template, new 
{
	BaseUrl = "http://localhost",
	Resource = "Students",
	Query = "firstName=John&lastName=Smith"
});

Possible Improvements

  • Allow a variable to be represeneted by an array, something like repeating the variable for x items.

  • Allow simple boolean logic to be perfomed in a Concated Variable. EG [{seconds}(>1)seconds:second]

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DeltaWare.SDK.SmartFormat:

Package Downloads
SereneApi.Core

An easy solution for consuming APIs.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.1.1 9,996 7/11/2022
6.0.0.1 412 6/29/2022
5.0.0.1 411 6/29/2022