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

Parse exceptions with (nested) static classes #773

Closed
poostwoud opened this issue Feb 6, 2024 · 3 comments · Fixed by #864
Closed

Parse exceptions with (nested) static classes #773

poostwoud opened this issue Feb 6, 2024 · 3 comments · Fixed by #864
Assignees

Comments

@poostwoud
Copy link

poostwoud commented Feb 6, 2024

1. Description

I'm trying to create an Excel-like function syntax using nested static classes. I'm experiencing difficulties with some class names and nested classes. Below is the Fiddle code:

using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Linq.Expressions;

public class Program
{
    public static void Main()
    {
        var config = new ParsingConfig
        {
            CustomTypeProvider = new CustomTypeProvider(new[] { typeof(IST), typeof(IST.NOT) })
        };

        // Not working;
        var expression = "IS.NULL(null)";
        try
        {
            var result = DynamicExpressionParser.ParseLambda(
                config,
                false,
                new ParameterExpression[] {  },
                typeof(bool),
                expression
            );
            Console.WriteLine($"{expression} success");
        }
        catch (Exception ex)
        {
            Console.WriteLine(
                $"{expression} failed, because of {ex.GetType().FullName} with message {ex.Message}"
            );
        }

        // Working;
        expression = "IST.NULL(null)";
        try
        {
            var result = DynamicExpressionParser.ParseLambda(
                config,
                false,
                new ParameterExpression[] {  },
                typeof(bool),
                expression
            );
            Console.WriteLine($"{expression} success");
        }
        catch (Exception ex)
        {
            Console.WriteLine(
                $"{expression} failed, because of {ex.GetType().FullName} with message {ex.Message}"
            );
        }

        // Not working;
        expression = "IS.NOT.NULL(null)";
        try
        {
            var result = DynamicExpressionParser.ParseLambda(
                config,
                false,
                new ParameterExpression[] {  },
                typeof(bool),
                expression
            );
            Console.WriteLine($"{expression} success");
        }
        catch (Exception ex)
        {
            Console.WriteLine(
                $"{expression} failed, because of {ex.GetType().FullName} with message {ex.Message}"
            );
        }

        // Not working;
        expression = "IST.NOT.NULL(null)";
        try
        {
            var result = DynamicExpressionParser.ParseLambda(
                config,
                false,
                new ParameterExpression[] {  },
                typeof(bool),
                expression
            );
            Console.WriteLine($"{expression} success");
        }
        catch (Exception ex)
        {
            Console.WriteLine(
                $"{expression} failed, because of {ex.GetType().FullName} with message {ex.Message}"
            );
        }
    }

    public class CustomTypeProvider : DefaultDynamicLinqCustomTypeProvider
    {
        private readonly HashSet<Type> _types;

        public CustomTypeProvider(Type[] types)
        {
            _types = new HashSet<Type>(types);
        }

        public override HashSet<Type> GetCustomTypes()
        {
            return _types;
        }
    }

    public static class IS
    {
        public static bool NULL(object value) => value == null;

        public static class NOT
        {
            public static bool NULL(object value) => value != null;
        }
    }

    public static class IST
    {
        public static bool NULL(object value) => value == null;

        public static class NOT
        {
            public static bool NULL(object value) => value != null;
        }
    }
}

2. Exception

Fiddle output:

IS.NULL(null) failed, because of System.Linq.Dynamic.Core.Exceptions.ParseException with message '(' expected
IST.NULL(null) success
IS.NOT.NULL(null) failed, because of System.Linq.Dynamic.Core.Exceptions.ParseException with message '(' expected
IST.NOT.NULL(null) failed, because of System.Linq.Dynamic.Core.Exceptions.ParseException with message Identifier expected

3. Fiddle

https://dotnetfiddle.net/nRMI2q

@StefH
Copy link
Collaborator

StefH commented Feb 15, 2024

Hello @poostwoud;

This is indeed an error, related to #772 .

However IS is a reserved keyword.
The casing issue for keywords is modified in #735, however this PR is not yet completed.

@StefH
Copy link
Collaborator

StefH commented Dec 24, 2024

#864

@StefH
Copy link
Collaborator

StefH commented Dec 24, 2024

This has been fixed.

And to make this work, use the following:

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using FluentAssertions;
using Xunit;

namespace System.Linq.Dynamic.Core.Tests.CustomTypeProviders
{
    [SuppressMessage("ReSharper", "InconsistentNaming")]
    [SuppressMessage("ReSharper", "MemberHidesStaticFromOuterClass")]
    public class CustomTypeProviderTests
    {
        private readonly ParsingConfig _config = new()
        {
            CustomTypeProvider = new MyCustomTypeProvider(ParsingConfig.Default, typeof(IS), typeof(IS.NOT), typeof(IST), typeof(IST.NOT)),
            IsCaseSensitive = true
        };

        [Theory]
        [InlineData("IS.NULL(null)", true)]
        [InlineData("IS.NOT.NULL(null)", false)]
        [InlineData("IST.NULL(null)", true)]
        [InlineData("IST.NOT.NULL(null)", false)]
        public void Test(string expression, bool expected)
        {
            // Act 1
            var lambdaExpression = DynamicExpressionParser.ParseLambda(
                _config,
                false,
                [],
                typeof(bool),
                expression
            );

            // Act 2
            var result = (bool?)lambdaExpression.Compile().DynamicInvoke();

            // Assert
            result.Should().Be(expected);
        }

        public class MyCustomTypeProvider : DefaultDynamicLinqCustomTypeProvider
        {
            private readonly HashSet<Type> _types;

            public MyCustomTypeProvider(ParsingConfig config, params Type[] types) : base(config)
            {
                _types = new HashSet<Type>(types);
            }

            public override HashSet<Type> GetCustomTypes()
            {
                return _types;
            }
        }

        public static class IS
        {
            public static bool NULL(object? value) => value == null;

            public static class NOT
            {
                public static bool NULL(object? value) => value != null;
            }
        }

        public static class IST
        {
            public static bool NULL(object? value) => value == null;

            public static class NOT
            {
                public static bool NULL(object? value) => value != null;
            }
        }
    }
}

@StefH StefH closed this as completed Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants