Code-Sharp / WampSharp

A C# implementation of WAMP (The Web Application Messaging Protocol)
http://wampsharp.net
Other
386 stars 84 forks source link

Keyword arguments in reflection-based Caller/Callee #132

Open supermihi opened 8 years ago

supermihi commented 8 years ago

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

darkl commented 8 years ago

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

supermihi commented 8 years ago

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

darkl commented 8 years ago

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:

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

supermihi commented 8 years ago

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.

Chunky-Monkey commented 3 years ago

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.

darkl commented 3 years ago

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.