Skip to content

tpluscode/UriTemplateString

Repository files navigation

trail icon

UriTemplateString Build status NuGet version codecov.io codefactor

Parses URI Templates into a friendly object structure.

Installation

Get from nuget.org

install-package UriTemplateString

Usage

Simply cast a string to UriTemplateString.

var template = (UriTemplateString)"/base/users{/page:2}{?name}";

Now you can inspect the structure of the template

template.Parts.Length == 3;

// access literal parts
var literal = template.Parts[0] as Literal;
literal.ToString() == "/base/users";

// access parts with modifier
var pageSeg = template[1] as Expression;
(pageSeg.Modifier as MaxLength).MaxLength == 2;

// access variables and operator
var query = template.Parts[2] as Expression;
query.Operator.Equals(Operator.QueryComponent);
query.Variables[0].Name == "name";

And do simple operations

// concatenate templates (and raw strings)
template += "{&rest*}";
// produces /base/users{/page:2}{?name}{&rest*}

// append query
template.AppendQueryParam("rest", explode: true);
// produces /base/users{/page:2}{?name,rest*}

Gotchas

Parsing is implemented using regular expressions so performance can be suboptimal.

Also, currently the expressions are an approximation of the real spec. Quirks possible ahead.

The stencil print icon by Dairy Free Design from The Noun Project