Best way to handle generation of optional values

Hi,

I've a optional field.

Counter concept
concept Counter ...            

  children: 
  start        : IntegerProperty[0..1]       
  ...
  numberFormat : StringProperty[0..1]
prettyPrint();

StringProperty is a abstract concept that is extended by StringLiteral and StringExpression.

In generator I want the following:
  • if numberFormat is set, then generate the value
  • if numberFormat is not set, use "" (empty string)


Currently I use $COPY_SRC$ [ "" ], and "mapped node" is customized as
{ 
  if (node.maximum.isNull) { return <"">; } 
  node.maximum; 
}
prettyPrint();

Is it a proper way to handle missing values?

Are there any short-cuts for if (node.maximum.isNull) { return <"">; }   ?

I know of pre-processing scripts. However wonder what are the possibilities.
4 comments
You may use the $IF$ node macro, I guess.
0
I just thought there might be some secret "use this if empty" COPY_SRC (or whatever) setting.

I might try implementing $COPY_SRC_DEF$ [ node ] that will basically be rewritten to $IF node.isNotNull$ then { $COPY_SRC#[node] } else { node }

Well, it does look natural to use "wrapped node as is if "mapped node" turned out to be empty, doesn't it?
0
<table width="100%" cellspacing="0" border="0"><tbody><tr><td align="left"><table width="80%" cellspacing="5" cellpadding="0" border="0"><tbody><tr><td><table width="100%" cellspacing="0" cellpadding="0" border="0"><tbody> <tr><td> <a href="http://youtrack.jetbrains.net/issue/MPS-22052">MPS-22052</a> Best way to handle generation of optional values</td></tr> <tr><td>Hi,<br><br>I've a optional field.<br><br><div>Counter concept</div> <pre> concept Counter ...               children:   start        : IntegerProperty[0..1]         ...   numberFormat : StringProperty[0..1] </pre> <br><br><span>StringProperty</span> is a abstract concept that is extended by <span>StringLiteral</span> and <span>StringExpression</span>.<br><br>In generator I want the following:<br><ul> <li> if <span>numberFormat</span> is set, then generate the value<br> </li> <li> if <span>numberFormat</span> is not set, use <span>""</span> (empty string)</li> </ul> <br><br>Currently I use <span>$COPY_SRC$ [ "" ]</span>, and "mapped node" is customized as<br><pre> {   if (node.maximum.isNull) { return &lt;""&gt;; }   node.maximum; } </pre> <br><br>I think it would be nice if <span>COPY_SRC</span>, <span>PROPERTY_MACRO</span>, <span>REFERENCE_MACRO</span>, etc had an option "use wrapped node if mapped node is null".<br><br>I think it would simplify certain cases of generation.<br><br>Current workarounds are:<br>1) <span>if (node.maximum.isNull) { return &lt;"default value"&gt;; } </span> in the COPY_SRC "mapped node" query.<br>This is somewhat elegant, however it is not that easy to type as it involves quotations.<br><br>2) Helper template fragment:<br>&lt;TF $IF$ [$COPY_SRC$ ["tmp"]] / <br>$ELSE$ [&lt;T  "$[tmp]"  T&gt; ] TF&gt;<br><br>Basically, "if (node.isNotNull) { copy_src } else { use default }<br><br>This is not easy to type and the usage is not that good: no default intention for $CALL$ macro.<br>"default value" is hidden in the CALL template parameter. </td></tr> <tr><td>Normal | Feature | Open | atikhomirov</td></tr> </tbody></table></td></tr></tbody></table></td></tr></tbody></table>
0
You may use the $IF$ node macro, I guess


That's quite a few of $IF$/else <T T> macros. I wish MPS could use "wrapped" node in case context is empty.
0

Please sign in to leave a comment.