Support Missing Key Handlers

A request was made to allow missing keys to be handled in a custom
manner. I created an event that can be registered with the Generator
instances.

I also fixed a deployment issue causing debug versions of the library to
be packaged and released to NuGet. This should boost performance
considerably.
This commit is contained in:
Travis Parks 2013-04-18 19:26:58 -04:00
parent bfc579be22
commit ac09c8fc38
8 changed files with 1385 additions and 1277 deletions

View File

@ -1,3 +1,4 @@
nuget pack ../mustache-sharp/mustache-sharp.csproj -Prop Configuration=Release -Build msbuild ../mustache-sharp.sln /p:Configuration=Release
nuget push *.nupkg nuget pack ../mustache-sharp/mustache-sharp.csproj -Properties Configuration=Release
nuget push *.nupkg
del *.nupkg del *.nupkg

File diff suppressed because it is too large Load Diff

View File

@ -1,35 +1,35 @@
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following // General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information // set of attributes. Change these attribute values to modify the information
// associated with an assembly. // associated with an assembly.
[assembly: AssemblyTitle("mustache-sharp.test")] [assembly: AssemblyTitle("mustache-sharp.test")]
[assembly: AssemblyDescription("")] [assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")] [assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("mustache-sharp.test")] [assembly: AssemblyProduct("mustache-sharp.test")]
[assembly: AssemblyCopyright("Copyright © 2013")] [assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible // Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from // to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type. // COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)] [assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM // The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("9975f293-f972-4751-9c8c-e25b17c0c8bc")] [assembly: Guid("9975f293-f972-4751-9c8c-e25b17c0c8bc")]
// Version information for an assembly consists of the following four values: // Version information for an assembly consists of the following four values:
// //
// Major Version // Major Version
// Minor Version // Minor Version
// Build Number // Build Number
// Revision // Revision
// //
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("0.0.3.0")] [assembly: AssemblyVersion("0.0.4.0")]
[assembly: AssemblyFileVersion("0.0.3.0")] [assembly: AssemblyFileVersion("0.0.4.0")]

View File

@ -1,56 +1,72 @@
using System; using System;
using System.Globalization; using System.Collections.Generic;
using System.IO; using System.Globalization;
using System.IO;
namespace mustache
{ namespace mustache
/// <summary> {
/// Generates text by substituting an object's values for placeholders. /// <summary>
/// </summary> /// Generates text by substituting an object's values for placeholders.
public sealed class Generator /// </summary>
{ public sealed class Generator
private readonly IGenerator _generator; {
private readonly IGenerator _generator;
/// <summary> private readonly List<EventHandler<MissingKeyEventArgs>> _handlers;
/// Initializes a new instance of a Generator.
/// </summary> /// <summary>
/// <param name="generator">The text generator to wrap.</param> /// Initializes a new instance of a Generator.
internal Generator(IGenerator generator) /// </summary>
{ /// <param name="generator">The text generator to wrap.</param>
_generator = generator; internal Generator(IGenerator generator)
} {
_generator = generator;
/// <summary> _handlers = new List<EventHandler<MissingKeyEventArgs>>();
/// Gets the text that is generated for the given object. }
/// </summary>
/// <param name="source">The object to generate the text with.</param> /// <summary>
/// <returns>The text generated for the given object.</returns> /// Occurs when a key/property is not found in the object graph.
public string Render(object source) /// </summary>
{ public event EventHandler<MissingKeyEventArgs> KeyNotFound
return render(CultureInfo.CurrentCulture, source); {
} add { _handlers.Add(value); }
remove { _handlers.Remove(value); }
/// <summary> }
/// Gets the text that is generated for the given object.
/// </summary> /// <summary>
/// <param name="provider">The format provider to use.</param> /// Gets the text that is generated for the given object.
/// <param name="source">The object to generate the text with.</param> /// </summary>
/// <returns>The text generated for the given object.</returns> /// <param name="source">The object to generate the text with.</param>
public string Render(IFormatProvider provider, object source) /// <returns>The text generated for the given object.</returns>
{ public string Render(object source)
if (provider == null) {
{ return render(CultureInfo.CurrentCulture, source);
provider = CultureInfo.CurrentCulture; }
}
return render(provider, source); /// <summary>
} /// Gets the text that is generated for the given object.
/// </summary>
private string render(IFormatProvider provider, object source) /// <param name="provider">The format provider to use.</param>
{ /// <param name="source">The object to generate the text with.</param>
KeyScope scope = new KeyScope(source); /// <returns>The text generated for the given object.</returns>
StringWriter writer = new StringWriter(provider); public string Render(IFormatProvider provider, object source)
_generator.GetText(scope, writer); {
return writer.ToString(); if (provider == null)
} {
} provider = CultureInfo.CurrentCulture;
} }
return render(provider, source);
}
private string render(IFormatProvider provider, object source)
{
KeyScope scope = new KeyScope(source);
foreach (EventHandler<MissingKeyEventArgs> handler in _handlers)
{
scope.KeyNotFound += handler;
}
StringWriter writer = new StringWriter(provider);
_generator.GetText(scope, writer);
return writer.ToString();
}
}
}

View File

@ -1,96 +1,111 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using mustache.Properties; using mustache.Properties;
namespace mustache namespace mustache
{ {
/// <summary> /// <summary>
/// Represents a scope of keys. /// Represents a scope of keys.
/// </summary> /// </summary>
public sealed class KeyScope public sealed class KeyScope
{ {
private readonly object _source; private readonly object _source;
private readonly KeyScope _parent; private readonly KeyScope _parent;
/// <summary> /// <summary>
/// Initializes a new instance of a KeyScope. /// Initializes a new instance of a KeyScope.
/// </summary> /// </summary>
/// <param name="source">The object to search for keys in.</param> /// <param name="source">The object to search for keys in.</param>
internal KeyScope(object source) internal KeyScope(object source)
: this(source, null) : this(source, null)
{ {
} }
/// <summary> /// <summary>
/// Initializes a new instance of a KeyScope. /// Initializes a new instance of a KeyScope.
/// </summary> /// </summary>
/// <param name="source">The object to search for keys in.</param> /// <param name="source">The object to search for keys in.</param>
/// <param name="parent">The parent scope to search in if the value is not found.</param> /// <param name="parent">The parent scope to search in if the value is not found.</param>
internal KeyScope(object source, KeyScope parent) internal KeyScope(object source, KeyScope parent)
{ {
_parent = parent; _parent = parent;
_source = source; _source = source;
} }
/// <summary> /// <summary>
/// Creates a child scope that searches for keys in the given object. /// Occurs when a key/property is not found in the object graph.
/// </summary> /// </summary>
/// <param name="source">The object to search for keys in.</param> public event EventHandler<MissingKeyEventArgs> KeyNotFound;
/// <returns>The new child scope.</returns>
public KeyScope CreateChildScope(object source) /// <summary>
{ /// Creates a child scope that searches for keys in the given object.
KeyScope scope = new KeyScope(source, this); /// </summary>
return scope; /// <param name="source">The object to search for keys in.</param>
} /// <returns>The new child scope.</returns>
public KeyScope CreateChildScope(object source)
/// <summary> {
/// Attempts to find the value associated with the key with given name. KeyScope scope = new KeyScope(source, this);
/// </summary> scope.KeyNotFound = KeyNotFound;
/// <param name="name">The name of the key.</param> return scope;
/// <returns>The value associated with the key with the given name.</returns> }
/// <exception cref="System.Collections.Generic.KeyNotFoundException">A key with the given name could not be found.</exception>
internal object Find(string name) /// <summary>
{ /// Attempts to find the value associated with the key with given name.
string[] names = name.Split('.'); /// </summary>
string member = names[0]; /// <param name="name">The name of the key.</param>
object nextLevel = _source; /// <returns>The value associated with the key with the given name.</returns>
if (member != "this") /// <exception cref="System.Collections.Generic.KeyNotFoundException">A key with the given name could not be found.</exception>
{ internal object Find(string name)
nextLevel = find(member); {
} string[] names = name.Split('.');
for (int index = 1; index < names.Length; ++index) string member = names[0];
{ object nextLevel = _source;
IDictionary<string, object> context = toLookup(nextLevel); if (member != "this")
member = names[index]; {
nextLevel = context[member]; nextLevel = find(member);
} }
return nextLevel; for (int index = 1; index < names.Length; ++index)
} {
IDictionary<string, object> context = toLookup(nextLevel);
private object find(string name) member = names[index];
{ nextLevel = context[member];
IDictionary<string, object> lookup = toLookup(_source); }
if (lookup.ContainsKey(name)) return nextLevel;
{ }
return lookup[name];
} private object find(string name)
if (_parent == null) {
{ IDictionary<string, object> lookup = toLookup(_source);
string message = String.Format(CultureInfo.CurrentCulture, Resources.KeyNotFound, name); if (lookup.ContainsKey(name))
throw new KeyNotFoundException(message); {
} return lookup[name];
return _parent.find(name); }
} if (_parent == null)
{
private static IDictionary<string, object> toLookup(object value) MissingKeyEventArgs args = new MissingKeyEventArgs(name);
{ if (KeyNotFound != null)
IDictionary<string, object> lookup = value as IDictionary<string, object>; {
if (lookup == null) KeyNotFound(this, args);
{ }
lookup = new PropertyDictionary(value); if (args.Handled)
} {
return lookup; return args.Substitute;
} }
} string message = String.Format(CultureInfo.CurrentCulture, Resources.KeyNotFound, name);
} throw new KeyNotFoundException(message);
}
return _parent.find(name);
}
private static IDictionary<string, object> toLookup(object value)
{
IDictionary<string, object> lookup = value as IDictionary<string, object>;
if (lookup == null)
{
lookup = new PropertyDictionary(value);
}
return lookup;
}
}
}

View File

@ -0,0 +1,34 @@
using System;
namespace mustache
{
/// <summary>
/// Holds the information needed to handle a missing key.
/// </summary>
public class MissingKeyEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of a MissingKeyEventArgs.
/// </summary>
/// <param name="missingKey">The key that had no match.</param>
internal MissingKeyEventArgs(string missingKey)
{
MissingKey = missingKey;
}
/// <summary>
/// Gets the key that could not be found.
/// </summary>
public string MissingKey { get; private set; }
/// <summary>
/// Gets or sets whether to use the substitute.
/// </summary>
public bool Handled { get; set; }
/// <summary>
/// Gets or sets the object to use as the substitute.
/// </summary>
public object Substitute { get; set; }
}
}

View File

@ -1,39 +1,39 @@
using System; using System;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
// General Information about an assembly is controlled through the following // General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information // set of attributes. Change these attribute values to modify the information
// associated with an assembly. // associated with an assembly.
[assembly: AssemblyTitle("mustache-sharp")] [assembly: AssemblyTitle("mustache-sharp")]
[assembly: AssemblyDescription("A extension of the mustache text template engine for .NET.")] [assembly: AssemblyDescription("A extension of the mustache text template engine for .NET.")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("truncon")] [assembly: AssemblyCompany("truncon")]
[assembly: AssemblyProduct("mustache-sharp")] [assembly: AssemblyProduct("mustache-sharp")]
[assembly: AssemblyCopyright("Copyright © 2013")] [assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]
// Setting ComVisible to false makes the types in this assembly not visible // Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from // to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type. // COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)] [assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM // The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e5a4263d-d450-4d85-a4d5-44c0a2822668")] [assembly: Guid("e5a4263d-d450-4d85-a4d5-44c0a2822668")]
// Version information for an assembly consists of the following four values: // Version information for an assembly consists of the following four values:
// //
// Major Version // Major Version
// Minor Version // Minor Version
// Build Number // Build Number
// Revision // Revision
// //
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.3.0")] [assembly: AssemblyVersion("0.0.4.0")]
[assembly: AssemblyFileVersion("0.0.3.0")] [assembly: AssemblyFileVersion("0.0.4.0")]
[assembly: InternalsVisibleTo("mustache-sharp.test")] [assembly: InternalsVisibleTo("mustache-sharp.test")]

View File

@ -1,82 +1,83 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion> <ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{D71B378F-A4BA-4263-A4F0-07A49A0C528D}</ProjectGuid> <ProjectGuid>{D71B378F-A4BA-4263-A4F0-07A49A0C528D}</ProjectGuid>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>mustache</RootNamespace> <RootNamespace>mustache</RootNamespace>
<AssemblyName>mustache-sharp</AssemblyName> <AssemblyName>mustache-sharp</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ArgumentCollection.cs" /> <Compile Include="ArgumentCollection.cs" />
<Compile Include="CompoundGenerator.cs" /> <Compile Include="CompoundGenerator.cs" />
<Compile Include="ConditionTagDefinition.cs" /> <Compile Include="ConditionTagDefinition.cs" />
<Compile Include="ContentTagDefinition.cs" /> <Compile Include="ContentTagDefinition.cs" />
<Compile Include="InlineTagDefinition.cs" /> <Compile Include="InlineTagDefinition.cs" />
<Compile Include="EachTagDefinition.cs" /> <Compile Include="EachTagDefinition.cs" />
<Compile Include="ElifTagDefinition.cs" /> <Compile Include="ElifTagDefinition.cs" />
<Compile Include="ElseTagDefinition.cs" /> <Compile Include="ElseTagDefinition.cs" />
<Compile Include="FormatCompiler.cs" /> <Compile Include="FormatCompiler.cs" />
<Compile Include="Generator.cs" /> <Compile Include="Generator.cs" />
<Compile Include="IfTagDefinition.cs" /> <Compile Include="IfTagDefinition.cs" />
<Compile Include="IGenerator.cs" /> <Compile Include="IGenerator.cs" />
<Compile Include="InlineGenerator.cs" /> <Compile Include="InlineGenerator.cs" />
<Compile Include="KeyGenerator.cs" /> <Compile Include="KeyGenerator.cs" />
<Compile Include="MasterTagDefinition.cs" /> <Compile Include="MasterTagDefinition.cs" />
<Compile Include="NestedContext.cs" /> <Compile Include="MissingKeyEventArgs.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="NestedContext.cs" />
<Compile Include="Properties\Resources.Designer.cs"> <Compile Include="Properties\AssemblyInfo.cs" />
<AutoGen>True</AutoGen> <Compile Include="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime> <AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon> <DesignTime>True</DesignTime>
</Compile> <DependentUpon>Resources.resx</DependentUpon>
<Compile Include="PropertyDictionary.cs" /> </Compile>
<Compile Include="RegexHelper.cs" /> <Compile Include="PropertyDictionary.cs" />
<Compile Include="StaticGenerator.cs" /> <Compile Include="RegexHelper.cs" />
<Compile Include="TagDefinition.cs" /> <Compile Include="StaticGenerator.cs" />
<Compile Include="TagParameter.cs" /> <Compile Include="TagDefinition.cs" />
<Compile Include="KeyScope.cs" /> <Compile Include="TagParameter.cs" />
<Compile Include="Trimmer.cs" /> <Compile Include="KeyScope.cs" />
<Compile Include="WithGenerator.cs" /> <Compile Include="Trimmer.cs" />
</ItemGroup> <Compile Include="WithGenerator.cs" />
<ItemGroup> </ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx"> <ItemGroup>
<Generator>ResXFileCodeGenerator</Generator> <EmbeddedResource Include="Properties\Resources.resx">
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource> <LastGenOutput>Resources.Designer.cs</LastGenOutput>
</ItemGroup> </EmbeddedResource>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> </ItemGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Other similar extension points exist, see Microsoft.Common.targets. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<Target Name="BeforeBuild"> Other similar extension points exist, see Microsoft.Common.targets.
</Target> <Target Name="BeforeBuild">
<Target Name="AfterBuild"> </Target>
</Target> <Target Name="AfterBuild">
--> </Target>
-->
</Project> </Project>