{"id":172,"date":"2019-12-05T23:46:21","date_gmt":"2019-12-05T23:46:21","guid":{"rendered":"https:\/\/binaryopus.com\/?p=172"},"modified":"2019-12-05T23:46:21","modified_gmt":"2019-12-05T23:46:21","slug":"converting-4gl-record-types-to-dictionaries","status":"publish","type":"post","link":"https:\/\/binaryopus.com\/?p=172","title":{"rendered":"Converting 4gl Record Types to Dictionaries"},"content":{"rendered":"\n<p>Another feature that Genero record types lack is object inspection.  This allows you to discover the properties of an object at runtime rather than knowing it in advance.  I&#8217;ve found a need for this when implementing an audit logger for data changes to Genero records.<\/p>\n\n\n\n<p>For instance suppose I wanted to implement an equals method or a hash function for a record type that can iterate through all the fields   .<\/p>\n\n\n\n<p>Fortunatly I&#8217;ve been able to find a workaround that converts Genero record types to dictionary types. Therefore, by converting the record to a dictionary, the keys method on the dictionary can be used to iterate through the keys.<\/p>\n\n\n\n<p>Take this example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import util\ntyope employee_type record like employee.*\n\n{\nAccepts an employee record type and converts it to a dictionary type.\n}\nfunction employee_to_dict(empl employee_type)\n returns dictionary of string\n    define\n        dict dictionary of string,\n        json_str string\n    \n    let json_str = util.json.stringify(empl)\n    call util.json.parse(json_str, dict)\n    return dict\nend function<\/code><\/pre>\n\n\n\n<p>So by converting the record type to a json string using the util.json.stringify method and then converting it back using the util.json.parse call we can convert any record type to a dictionary.<\/p>\n\n\n\n<p>Once the type has been converted, we can iterate through the values keys function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>define\n    i int\nfor i = 1 to dict.getKeys().getLength()\n    key = dict.getKeys()[i]\n    val = dict[key]\nend for<\/code><\/pre>\n\n\n\n<p>It&#8217;s not as elegant as what can be implemented in less restrictive languages but in a pinch, it can save a lot of duplicate code writing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another feature that Genero record types lack is object inspection. This allows you to discover the properties of an object at runtime rather than knowing it in advance. I&#8217;ve found a need for this when implementing an audit logger for data changes to Genero records. For instance suppose I wanted to implement an equals method [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,8],"tags":[],"_links":{"self":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts\/172"}],"collection":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=172"}],"version-history":[{"count":1,"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":173,"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts\/172\/revisions\/173"}],"wp:attachment":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}