how to set default values for unspecified arguments when sending requests?

Poster Content
nk4um User
Posts: 27
April 9, 2015 00:51

Hi Peter,

I forgot to ask you that is it safe to check if a parameter exists with the function "context.exists()"? Thank you very much.

Regards, Yang

Posted by pjr (View)
Hi Yang,

Without a snippet of your code its hard to know quite the best answer. I assume you have an active grammar and have defined an argument like this..

<active>
  <identifier>active:foo&lt;/identifier&gt;
  <argumentname="baa" /&gt;
&lt;/active&gt;

So this is actually the same as ...

<active>
  <identifier>active:foo&lt;/identifier&gt;
  <argumentname="baa" min="0" max="1" /&gt;
&lt;/active&gt;

That is the "baa" argument is expected once or not at all.

So in your endpoint you can do this...

String baa=context.getThisRequest().getArgumentValue("baa")
if(baa==null)
{  baa="res:/my/default/whatever"
}
//Now go and SOURCE the argument (or the default)
baaRep=context.source(baa)

Am I on the right track? Is this what you're looking for?

Peter

Like · Post Reply
nk4um User
Posts: 27
April 9, 2015 00:48

Hi Stefan,

Thank you very much. This is really useful!

Regards, Yang

Posted by stefan144 (View)
You could use the literal notation and do something like this in module.xml:

<rootspacename="testing - Defaults Space" uri="urn:stefan:testing:defaults" public="true">
  <literaltype="string" uri="res:/defaults/baa">default_baa&lt;/literal&gt;
&lt;/rootspace&gt;

Regards,

Stefan

Like · Post Reply
1 person likes this post
nk4um User
Posts: 90
March 31, 2015 11:07

You could use the literal notation and do something like this in module.xml:

<rootspacename="testing - Defaults Space" uri="urn:stefan:testing:defaults" public="true">
  <literaltype="string" uri="res:/defaults/baa">default_baa&lt;/literal&gt;
&lt;/rootspace&gt;

Regards,

Stefan

Like · Post Reply
nk4um User
Posts: 27
March 30, 2015 10:04

Hi Peter,

Thank you very much for your reply.

I have posted my code but I don't know where it is after published :D

In fact your solution is what I am using currently. I was just wondering if there is another better way to define it in the module definition directly.

Thank you very much for your help and have a nice day~

Regards, Yang

Posted by pjr (View)
Hi Yang,

Without a snippet of your code its hard to know quite the best answer. I assume you have an active grammar and have defined an argument like this..

<active>
  <identifier>active:foo&lt;/identifier&gt;
  <argumentname="baa" /&gt;
&lt;/active&gt;

So this is actually the same as ...

<active>
  <identifier>active:foo&lt;/identifier&gt;
  <argumentname="baa" min="0" max="1" /&gt;
&lt;/active&gt;

That is the "baa" argument is expected once or not at all.

So in your endpoint you can do this...

String baa=context.getThisRequest().getArgumentValue("baa")
if(baa==null)
{  baa="res:/my/default/whatever"
}
//Now go and SOURCE the argument (or the default)
baaRep=context.source(baa)

Am I on the right track? Is this what you're looking for?

Peter

Like · Post Reply
nk4um Moderator
Posts: 901
March 26, 2015 18:07

Hi Yang,

Without a snippet of your code its hard to know quite the best answer. I assume you have an active grammar and have defined an argument like this..

<active>
  <identifier>active:foo&lt;/identifier&gt;
  <argumentname="baa" /&gt;
&lt;/active&gt;

So this is actually the same as ...

<active>
  <identifier>active:foo&lt;/identifier&gt;
  <argumentname="baa" min="0" max="1" /&gt;
&lt;/active&gt;

That is the "baa" argument is expected once or not at all.

So in your endpoint you can do this...

String baa=context.getThisRequest().getArgumentValue("baa")
if(baa==null)
{  baa="res:/my/default/whatever"
}
//Now go and SOURCE the argument (or the default)
baaRep=context.source(baa)

Am I on the right track? Is this what you're looking for?

Peter

Like · Post Reply
1 person likes this post
nk4um User
Posts: 27

Hi there,

I am looking for a solution for a quite practical problem: how to set default values for some arguments that is not specified in a request? I made some trials but none of them succeeded, as you may know there are rather limited resources for NetKernel all over the Internet. Therefore, I am here to seek for some help for this issue and I hope I can get some suggestions. Thank you very much in advance.

Here is part of module definition I am using. I set all the arguments to optional because I hope when an argument is not given, then a corresponding default value is used. I put some default values in the request filed, but obviously they won't work because I don't know how to refer to an incoming request parameter in the meanwhile.

I hope I described my intention clearly. Is there any way to get around this? Thank you very much.

Regards, Yang

Like · Post Reply