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 6 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
10 changes: 10 additions & 0 deletions Fluid.Tests/TemplateContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ public async Task ShouldNotReleaseScopeAsynchronously()

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

[Fact]
public void ShouldUseStringComparer()
{
var context = new TemplateContext();
context.StringComparer = 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
16 changes: 11 additions & 5 deletions Fluid/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,31 @@ public sealed class Scope
public Scope()
{
Parent = null;
StringComparer = StringComparer.CurrentCulture;
}
hishamco marked this conversation as resolved.
Show resolved Hide resolved

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 +121,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 +144,4 @@ public void CopyTo(Scope scope)
}
}
}
}
}
9 changes: 9 additions & 0 deletions Fluid/TemplateContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ public void IncrementSteps()
/// </summary>
public Func<string, string, ValueTask<string>> Captured { get; set; }

/// <summary>
/// Gets or sets the <see cref="StringComparer"/> used for matching keys of scope values.
/// </summary>
public StringComparer StringComparer
gumbarros marked this conversation as resolved.
Show resolved Hide resolved
{
get => LocalScope.StringComparer;
set => LocalScope.StringComparer = value;
}

/// <summary>
/// Creates a new isolated child scope. After than any value added to this content object will be released once
/// <see cref="ReleaseScope" /> is called. The previous scope is linked such that its values are still available.
Expand Down
8 changes: 8 additions & 0 deletions Fluid/TemplateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public class TemplateOptions
/// </summary>
public bool Greedy { get; set; } = true;

/// <summary>
/// Gets or sets the <see cref="StringComparer"/> used for matching keys of scope values.
/// </summary>
public StringComparer StringComparer
gumbarros marked this conversation as resolved.
Show resolved Hide resolved
{
get => Scope.StringComparer;
set => Scope.StringComparer = value;
}

public TemplateOptions()
{
Expand Down