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

Question: Support to build Expressions besides LambdaExpressions #170

Closed
nsarris opened this issue Jun 2, 2018 · 7 comments
Closed

Question: Support to build Expressions besides LambdaExpressions #170

nsarris opened this issue Jun 2, 2018 · 7 comments

Comments

@nsarris
Copy link

nsarris commented Jun 2, 2018

I'm trying to migrate an old project that had the original ms code embedded to using this library.

However this version has removed the Expression returning Parse methods and the parser class is internal so there is no way of using this other than building a lambda and replacing the input parameter with a visitor which is kind of costly.

For the record we are using this library to parse arbitrary mappings and build dynamic projections of tables so we need a source field expression

@StefH
Copy link
Collaborator

StefH commented Jun 2, 2018

Can you post a clear code example?

@nsarris
Copy link
Author

nsarris commented Jun 2, 2018

I'm refering to the Parse method as it appears in your wiki which seems to have been removed:

https://github.com/StefH/System.Linq.Dynamic.Core/wiki/Dynamic-Expressions#the-parse-method

@StefH StefH changed the title Readd support to build Expressions besides LambdaExpressions Question: Support to build Expressions besides LambdaExpressions Jun 3, 2018
@StefH
Copy link
Collaborator

StefH commented Jun 3, 2018

The Wiki is not really correct at some points, I've removed that part.

Can't use use this class:
https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/src/System.Linq.Dynamic.Core/DynamicExpressionParser.cs#L11

@nsarris
Copy link
Author

nsarris commented Jun 3, 2018

This class exposes only methods that return LambdaExpressions (LambdaExpresion or Expression<Func<T,bool>> )

They all eventually do this:

Expression.Lambda(parser.Parse(args))

The old version exposed the same methods without the lambda conversion returning "unbound" expressions, essentially ommiting the Expression.Lamda() and returning the result of parser.Parse()

So basicly something like this (in DynamicExpressionParser - not sure all the overloads are needed I just copied all that didn't return a strongly typed lambda)

public static Expression Parse([CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
{
    Check.NotEmpty(expression, nameof(expression));

    var parser = new ExpressionParser(new ParameterExpression[0], expression, values, parsingConfig);

    return parser.Parse(resultType, createParameterCtor);
}

public static Expression Parse([CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
{
    Check.NotNull(parameters, nameof(parameters));
    Check.HasNoNulls(parameters, nameof(parameters));
    Check.NotEmpty(expression, nameof(expression));

    var parser = new ExpressionParser(parameters, expression, values, parsingConfig);

    return parser.Parse(resultType, createParameterCtor);
}

public static Expression Parse(bool createParameterCtor, [NotNull] Type itType, [CanBeNull] Type resultType, string expression, params object[] values)
{
    Check.NotNull(itType, nameof(itType));
    Check.NotEmpty(expression, nameof(expression));

    return Parse(createParameterCtor, new[] { ParameterExpressionHelper.CreateParameterExpression(itType, string.Empty) }, resultType, expression, values);
}

public static Expression Parse([CanBeNull] ParsingConfig parsingConfig, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
{
    return Parse(parsingConfig, true, resultType, expression, values);
}

public static Expression Parse([CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
{
    Check.NotEmpty(expression, nameof(expression));

    return Parse(null, true, resultType, expression, values);
}


public static Expression Parse([NotNull] Type itType, [CanBeNull] Type resultType, string expression, params object[] values)
{
    return Parse(true, itType, resultType, expression, values);
}

public static Expression Parse([CanBeNull] ParsingConfig parsingConfig, [NotNull] Type itType, [CanBeNull] Type resultType, string expression, params object[] values)
{
    return Parse(true, itType, resultType, expression, parsingConfig, values);
}


public static Expression Parse([CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] Type itType, [CanBeNull] Type resultType, string expression, params object[] values)
{
    Check.NotNull(itType, nameof(itType));
    Check.NotEmpty(expression, nameof(expression));

    return Parse(parsingConfig, createParameterCtor, new[] { ParameterExpressionHelper.CreateParameterExpression(itType, string.Empty) }, resultType, expression, values);
}

public static Expression Parse([NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, string expression, params object[] values)
{
    return Parse(null, true, parameters, resultType, expression, values);
}

public static Expression Parse([CanBeNull] ParsingConfig parsingConfig, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, string expression, params object[] values)
{
    return Parse(parsingConfig, true, parameters, resultType, expression, values);
}

public static Expression Parse(bool createParameterCtor, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
{
    return Parse(null, createParameterCtor, parameters, resultType, expression, values);
}

@StefH
Copy link
Collaborator

StefH commented Jun 4, 2018

So if the ExpressionParser was public, this will solve your problem?

@nsarris
Copy link
Author

nsarris commented Jun 4, 2018 via email

@StefH
Copy link
Collaborator

StefH commented Jun 5, 2018

New NuGet will be uploaded in some time.

@StefH StefH closed this as completed Jun 5, 2018
StefH added a commit that referenced this issue Jun 5, 2018
1.0.8.10 (make ExpressionParser public again) linked to issue #170
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants