WampSharp icon indicating copy to clipboard operation
WampSharp copied to clipboard

Keyword arguments in reflection-based Caller/Callee

Open supermihi opened this issue 8 years ago • 6 comments

Hi, is it possible to use keyword arguments (see WAMP specs with the simple, reflection-based API? It seems to be possible using the raw API, but that is rather cumbersome when implementing a lot of services. Thanks, Michael

supermihi avatar Mar 22 '16 14:03 supermihi

Hi Michael,

It is not possible for callers. For callees this is done automatically by mapping keyword arguments to the arguments with the same names. Note that positional parameters have higher priority than keyword arguments - i.e. a keyword argument with the requested name is looked up only if the argument position (of the method) is larger than the number of arguments received in arguments array.

Elad

darkl avatar Mar 22 '16 14:03 darkl

Hi Elad, thans for your answer. Are there any plans to support kwargs for callers? I could think of an attribute to mark the keywords part, e.g.

[WampProcedure("com.example.keywordsexample")]
object KeywordsExample(int arg1, string arg2, [KwArgs] Dictionary<string, object> kwargs);

In my case, actually, there's a fixed set of keyword arguments (basically, some user identification) which I need to pass to all method calls. In order to avoid using the raw caller approach for all of these methods, I'm afraid I will have to implement a reflection-based caller mechanism based on a custom ProxyGenerator?

For the callee part: If I get it right, this means that e.g.

[WampProcedure("com.example.keywordsexample")]
object KeywordsExample(int arg1, string arg2, string user = null);

could be called with positional args [arg1=5, arg2="bar"] and kwargs {user="foo"}?

Michael

supermihi avatar Mar 23 '16 09:03 supermihi

Hi,

I prefer having an attribute indicating whether the whole arguments are positional or keywords. (Otherwise it might get too complicated)

Regarding callees: If you have a method

[WampProcedure("com.example.keywordsexample")] object KeywordsExample(int arg1, string arg2, string user);

Then the arguments resolve is done in this order:

  • Check if you got 3 arguments in the arguments array
    • If yes, map every argument in the arguments array to the matching method argument (by the corresponding positional)
    • If not (you got n < 3 arguments) map the first n arguments to the n arguments of the method. Lookup the 3-n arguments from the kwArgs dictionary.

For example if you get args = [1,2,"Hello"], kwArgs = { "user": "Bill" }, then the method will be called with arg1=1,arg2=2,user="Hello".

If you get args = [1], kwArgs = { "arg2": 2, user: "Bill" }, then the method will be called with arg1=1, arg2=2, user="Bill". Elad

darkl avatar Mar 23 '16 09:03 darkl

Hi Elad, thanks, your examples made clear to me how it works. I'll think about the caller-side ... unfortunatelly, I'll need mixed positional and keyword argument because the service I want to use explicitly looks for the user id information in the kwargs part, where all other args ar explicitly positional.

supermihi avatar Mar 23 '16 12:03 supermihi

Hi Elad, a few years have passed since this question... Is this still valid? I.e. Keyword arguments are not supported for refelction based callers?

I am implementing the caller side for an application and all the arguments are passed as keyword arguments. So it would make my life easier :)

Thank you very much.

Chunky-Monkey avatar Dec 04 '20 12:12 Chunky-Monkey

Hi @Chunky-Monkey, I never implemented such feature. As stated above, I support adding an attribute which indicates whether all arguments should be treated as keyword arguments/positional arguments.

darkl avatar Dec 04 '20 14:12 darkl