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

Extensions methods/properties support for Spring Expressions #221

Open
amadare42 opened this issue May 26, 2022 · 1 comment
Open

Extensions methods/properties support for Spring Expressions #221

amadare42 opened this issue May 26, 2022 · 1 comment

Comments

@amadare42
Copy link

Hey.
I would like to have functionality to add extension methods to objects. So user could define spring (or .net) lambda function for specific type. For example:

var vars = new Dictionary<string, object>{ 
 { "Foo", new Foo() { InternalValue = 2 } }
};
Expressions.RegisterExtensionFunction<Foo>(vars, "SpelExtension", "{|self, arg| $self.InternalValue + $arg}");
Expression.Parse("Foo.SpelExtension(1)").GetValue(null, vars); // result in Foo.InternalValue + 1 -> 3

Expressions.RegisterExtensionFunction<Foo, int>(vars, "CsExtension", (self, arg) => sef.InternalValue + arg);
Expression.Parse("Foo.CsExtension(1)").GetValue(null, vars); // result in Foo.InternalValue + 1 -> 3

Expressions.RegisterExtensionProperty<Foo>(vars, "SpelProp", "{|self| $self.InternalValue + 2}");
Expression.Parse("Foo.SpelProp").GetValue(null, vars); // result in Foo.InternalValue + 2 -> 4

Expressions.RegisterExtensionProperty<Foo>(vars, "CsProp", (self, arg) => sef.InternalValue + 2);
Expression.Parse("Foo.CsProp").GetValue(null, vars); // result in Foo.InternalValue + 2 -> 4

I have a quick and dirty POC that provides this functionality. I can clean it up and create PR. Are maintainers interested in this functionality?

@lahma
Copy link
Collaborator

lahma commented Jul 17, 2022

Sorry for the delay. Yes, all contributions are always appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants