microsoft / microsoft/AdaptiveCards

[Feature Request] C# Use defined types in AdaptiveExpressions.Expression.Function.Add by using a Delegate instead of an object

Open
#8,787 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Request
Dominant language
C#
Stars
2k
Forks
595
Avg merge
1d 19h
Merged PRs (30d)
1

Description

Problem Statement

In C# Template Engine AdaptiveExpressions.Expression.Functions.Add() has type of Add(string key, Func<IReadOnlyList, object> func) which causes us to create very ugly functions like ...

        AdaptiveExpressions.Expression.Functions.Add("IsRightToLeft", (args) =>
        {
            t model = ((args[0] as JToken).ToObject<t>());
    
            return "some string or JObject";
        });
Proposed solution

I am going to paste an unoptimized potential solution here. Basically, it is preferable to take a delegate instead of an object, and attempt to deserialize the properties, that way we can write normal function and give the expected types. It is preferable to throw an exception and fail inlining if the function does not work. Currently you catch the exception and skip the function.

        public void Test()
        {
            var m = new Func<Test, string, string>(Main);

            var model = new JObject
            {
                ["hi"] = "hi"
            };

            var token = JToken.FromObject(model);

            var item = new List<dynamic>
            {
                token,
                "hello"
            };

            string a = Do(item, m) as string;
            Console.WriteLine(a);
         }

        public static object Do(IReadOnlyList<dynamic> items, Delegate del, Newtonsoft.Json.JsonSerializer serializer = null)
        {
            var method = del.Method;
            ParameterInfo[] paramList = method.GetParameters();

            int i = 0;
            object[] inputs = new object[paramList.Length];

            if (items != null && items.Count == paramList.Length)
            {
                foreach (dynamic item in items)
                {
                    if (paramList[i].ParameterType == item.GetType())
                    {
                        inputs[i] = item;
                    }
                    else if(item is JToken token)
                    {
                        if (serializer == null)
                        {
                            inputs[i] = token.ToObject(paramList[i].ParameterType);
                        }
                        else
                        {
                            inputs[i] = token.ToObject(paramList[i].ParameterType, serializer);
                        }
                    }
                    else
                    {
                        inputs[i] = Convert.ChangeType(item, paramList[i].ParameterType);
                    }

                    i++;
                }
            }

            bool isAsync = method.ReturnType == typeof(Task<>);

            if (!isAsync)
            {
                return method.Invoke(null, inputs);
            }

            return JoinableTaskFactory.Run(() => (Task<object>)method.Invoke(null, inputs));
        }

        public static string Main(Test hi, string intro)
        {
            return intro + " " + hi.Hi;
        }
Alternatives or Workarounds

I am doing this in my code, it is really ugly and should be internal in the library.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating AdaptiveExpressions.Expression.Functions.Add and the code that invokes or inlines registered functions. Review how arguments and exceptions are currently handled, then define the typed-Delegate behavior, conversion rules, and failure semantics; done means the API supports typed delegate parameters without each caller performing manual JToken conversion.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.