Converting 4gl Record Types to Dictionaries
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’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 or a hash function for a record type that can iterate through all the fields .
Fortunatly I’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.
Take this example
import util
tyope employee_type record like employee.*
{
Accepts an employee record type and converts it to a dictionary type.
}
function employee_to_dict(empl employee_type)
returns dictionary of string
define
dict dictionary of string,
json_str string
let json_str = util.json.stringify(empl)
call util.json.parse(json_str, dict)
return dict
end function
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.
Once the type has been converted, we can iterate through the values keys function.
define
i int
for i = 1 to dict.getKeys().getLength()
key = dict.getKeys()[i]
val = dict[key]
end for
It’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.