Handlebars-Net / Handlebars.Net.Helpers

Handlebars.Net helpers in the categories: 'Boolean', 'Constants', 'Enumerable', 'Environment', 'Math', 'Regex', 'String', 'DateTime' and 'Url'.
MIT License
38 stars 14 forks source link

Add Path.Lookup #70

Closed StefH closed 1 year ago

codecov[bot] commented 1 year ago

Codecov Report

Merging #70 (5e16b8c) into master (d6fe6df) will increase coverage by 0.99%. The diff coverage is 68.13%.

:exclamation: Current head 5e16b8c differs from pull request most recent head 2651c94. Consider uploading reports for the commit 2651c94 to get more accurate results

@@            Coverage Diff             @@
##           master      #70      +/-   ##
==========================================
+ Coverage   43.98%   44.97%   +0.99%     
==========================================
  Files          40       43       +3     
  Lines        2626     2679      +53     
  Branches      362      367       +5     
==========================================
+ Hits         1155     1205      +50     
- Misses       1372     1375       +3     
  Partials       99       99              
Impacted Files Coverage Δ
...c/Handlebars.Net.Helpers.Core/Utils/ObjectUtils.cs 0.00% <0.00%> (ø)
...ebars.Net.Helpers.DynamicLinq/JObjectExtensions.cs 57.25% <ø> (+4.62%) :arrow_up:
...ers.DynamicLinq/Models/DynamicPropertyWithValue.cs 80.00% <ø> (ø)
...ars.Net.Helpers/Extensions/MethodBaseExtensions.cs 25.92% <31.57%> (+12.88%) :arrow_up:
....Helpers.DynamicLinq/Helpers/DynamicLinqHelpers.cs 18.40% <76.19%> (+5.36%) :arrow_up:
src/Handlebars.Net.Helpers/HandlebarsHelpers.cs 82.07% <90.90%> (+1.17%) :arrow_up:
.../Handlebars.Net.Helpers/Helpers/DateTimeHelpers.cs 66.66% <100.00%> (ø)
src/Handlebars.Net.Helpers/Helpers/PathHelpers.cs 100.00% <100.00%> (ø)
...ars.Net.Helpers/PathStructure/PathResolverProxy.cs 100.00% <100.00%> (ø)
... and 1 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

oformaniuk commented 1 year ago

How is this different from build-in lookup helper?

StefH commented 1 year ago

@zjklee Ah. I understand. There's a builtin helper. But that helper does not allow for 3 arguments with a defaultValue.

So a change could be like:

using HandlebarsDotNet.PathStructure;

namespace HandlebarsDotNet.Helpers
{
    public sealed class LookupReturnHelperDescriptor : IHelperDescriptor<HelperOptions>
    {
        public PathInfo Name { get; } = "lookup";

        public object Invoke(in HelperOptions options, in Context context, in Arguments arguments)
        {
            if (arguments.Length != 2 && arguments.Length != 3)
            {
                throw new HandlebarsException("{{lookup}} helper must have two or three arguments");
            }

            var segment = ChainSegment.Create(arguments[1]);

            var defaultValueIfNotFound = arguments.Length == 3 ? arguments[2] : UndefinedBindingResult.Create(segment);

            return !options.TryAccessMember(arguments[0], segment, out var value)
                ? defaultValueIfNotFound
                : value;
        }

        public void Invoke(in EncodedTextWriter output, in HelperOptions options, in Context context, in Arguments arguments)
        {
            output.Write(Invoke(options, context, arguments));
        }
    }
}
oformaniuk commented 1 year ago

@StefH , looks good to me 🙂

StefH commented 1 year ago

@zjklee Shall I create a PR for Handlebars.Net ?

StefH commented 1 year ago

@zjklee See PR https://github.com/Handlebars-Net/Handlebars.Net/pull/542