Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added StringComparer property to TemplateContext #681

Merged
merged 21 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Fluid.Tests/TemplateContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ public async Task ShouldNotReleaseScopeAsynchronously()

Assert.Equal("101", output);
}

[Fact]
public void ShouldUseStringComparer()
{
var context = new TemplateContext(StringComparer.InvariantCultureIgnoreCase);
context.SetValue("PageState", "insert");

Assert.Equal("insert", context.GetValue("pageState").ToStringValue());
gumbarros marked this conversation as resolved.
Show resolved Hide resolved
}

private class TestClass
{
Expand Down
18 changes: 11 additions & 7 deletions Fluid/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@ public sealed class Scope
private Dictionary<string, FluidValue> _properties;
private readonly bool _forLoopScope;

public Scope()
public Scope() : this(null)
{
Parent = null;
}

public Scope(Scope parent)
{
Parent = parent;
StringComparer = parent.StringComparer;
}

public Scope(Scope parent, bool forLoopScope)
{
if (forLoopScope && parent == null) ExceptionHelper.ThrowArgumentNullException(nameof(parent));

Parent = parent;

// A ForLoop scope reads and writes its values in the parent scope.
// Internal accessors to the inner properties grant access to the local properties.
_forLoopScope = forLoopScope;

_properties = new Dictionary<string, FluidValue>();
StringComparer = parent.StringComparer;
_properties = new Dictionary<string, FluidValue>(StringComparer);
}

/// <summary>
/// Gets or sets the <see cref="StringComparer"/> used for matching keys of this scope values.
/// </summary>
public StringComparer StringComparer { get; set; }

/// <summary>
/// Gets the own properties of the scope
Expand Down Expand Up @@ -115,7 +119,7 @@ public void SetValue(string name, FluidValue value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetOwnValue(string name, FluidValue value)
{
_properties ??= new Dictionary<string, FluidValue>();
_properties ??= new Dictionary<string, FluidValue>(StringComparer);
_properties[name] = value ?? NilValue.Instance;
}

Expand All @@ -138,4 +142,4 @@ public void CopyTo(Scope scope)
}
}
}
}
}
58 changes: 37 additions & 21 deletions Fluid/TemplateContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,36 @@ public class TemplateContext
/// <summary>
/// Initializes a new instance of <see cref="TemplateContext"/>.
/// </summary>
public TemplateContext() : this(TemplateOptions.Default)
public TemplateContext() : this(TemplateOptions.Default, StringComparer.CurrentCulture)
{
}

/// <summary>
/// Initializes a new instance of <see cref="TemplateContext"/>.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="options">The template options.</param>
/// <param name="allowModelMembers">Whether the members of the model can be accessed by default.</param>
public TemplateContext(object model, TemplateOptions options, bool allowModelMembers = true) : this(options)
public TemplateContext(StringComparer stringComparer) : this(TemplateOptions.Default, stringComparer)
{
if (model == null)
{
ExceptionHelper.ThrowArgumentNullException(nameof(model));
}
}

if (model is FluidValue fluidValue)
{
Model = fluidValue;
}
else
{
Model = FluidValue.Create(model, options);
AllowModelMembers = allowModelMembers;
}
/// <summary>
/// Initializes a new instance of <see cref="TemplateContext"/> with the specified <see cref="TemplateOptions"/>.
/// </summary>
/// <param name="options">The template options.</param>
public TemplateContext(TemplateOptions options) : this(options, StringComparer.CurrentCulture)
{
}

/// <summary>
/// Initializes a new instance of <see cref="TemplateContext"/> with the specified <see cref="TemplateOptions"/>.
/// </summary>
/// <param name="options">The template options.</param>
public TemplateContext(TemplateOptions options)
/// <param name="stringComparer">The the <see cref="StringComparer"/> used for matching keys of this scope values.</param>
public TemplateContext(TemplateOptions options, StringComparer stringComparer)
{
Options = options;
LocalScope = new Scope(options.Scope);
RootScope = LocalScope;
RootScope.StringComparer = stringComparer;
CultureInfo = options.CultureInfo;
TimeZone = options.TimeZone;
Captured = options.Captured;
Expand All @@ -61,7 +54,30 @@ public TemplateContext(TemplateOptions options)
/// </summary>
/// <param name="model">The model.</param>
/// <param name="allowModelMembers">Whether the members of the model can be accessed by default.</param>
public TemplateContext(object model, bool allowModelMembers = true) : this()
public TemplateContext(object model, bool allowModelMembers = true)
: this(model, TemplateOptions.Default, allowModelMembers)
{
}

/// <summary>
/// Initializes a new instance of <see cref="TemplateContext"/>.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="options">The template options.</param>
/// <param name="allowModelMembers">Whether the members of the model can be accessed by default.</param>
public TemplateContext(object model, TemplateOptions options, bool allowModelMembers = true)
: this(model, options, StringComparer.CurrentCulture, allowModelMembers)
{
}

/// <summary>
/// Initializes a new instance of <see cref="TemplateContext"/>.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="options">The template options.</param>
/// <param name="stringComparer">The the <see cref="StringComparer"/> used for matching keys of this scope values.</param>
/// <param name="allowModelMembers">Whether the members of the model can be accessed by default.</param>
public TemplateContext(object model, TemplateOptions options, StringComparer stringComparer, bool allowModelMembers = true) : this(options, stringComparer)
{
if (model == null)
{
Expand All @@ -74,7 +90,7 @@ public TemplateContext(object model, bool allowModelMembers = true) : this()
}
else
{
Model = FluidValue.Create(model, TemplateOptions.Default);
Model = FluidValue.Create(model, options);
AllowModelMembers = allowModelMembers;
}
}
Expand Down
Loading