openSUSE:YaST development tricks

Jump to: navigation, search

Development Tricks for YaST

Access 'any' values from maps and lists

This is taken from bug #239005, original by Olaf Dabrunz

There are many places where the is() function (e.g.: "is(WFM::Args(0), string)") is used, but I was unable to find the documentation.

This function is needed whenever a value of type "any" is retrieved from a map or list, to use a proper default value. E.g., if the <any> values in my_map can contain <string>s or <integer>s:

   map<string,any> my_map = ...;
   sformat("%1", is(my_map[this]:nil, string) ? my_map[this]:"" : (
                   my_map[this]:nil == nil ? "" : my_map[this]:nil
                   )
           );


Most other approaches give error messages or unwanted behaviour as follows:

   my_map[this] = 123;
   sformat("%1", my_map[this]:"")
       -> Can't convert value '123' to type 'string'
   my_map[this] = "testing";
   sformat("%1", my_map[this]:0)
       -> Can't convert value '"testing"' to type 'const integer'
   my_map[this] = nil;
   sformat("%1", my_map[this]:nil)
       -> no error, but output is 'nil' (and I want the empty string )


The only working alternative that does not need the "is()" function is this:

   sformat("%1", my_map[this]:nil == nil ? "" : my_map[this]:nil)