Thanks for reading my question.
I have a class which reads a configuration and based on the configuration, populates a data structure by reading from a database, which is ultimately presented to a processing layer that makes use of the data for various processing.
<FieldConfigs> <ConfigField name="MyDBCol1"/> <ConfigField name="MyDBCol2"/> <ConfigField name="MyDBCol3"/> </FieldConfigs>
I store my data on a std::map with string keys. As the data fields are defined in the configuration, if someone makes the mistake of not adding the configuration entries to the configuration files but tries to access them in the code, it would require a check every time data is accessed, to avoid crashes. Since this is a setup issue, it will be counter productive to run the check every time data is accessed.
To detect the setup errors, I have to run a check when the program starts whether all the fields accessed by the program are actually defined in the configuration file. Since I’m trying to avoid a developer mishap, the ideal solution should throw compiler errors if the check fails. Third party library usage have to go through an approval process by the client which can take months, and I want to avoid them if possible.
So far I have tried these avenues:
- I tried solving this problem using reflection, but all implementations I tried, require some intervention by the developer as C++ does not provide object metadata by default. Since, we are trying to avoid a developer mistake, I doubt that adding another thing for the developer to do (which can easily be missed) would be of much use to avoid the problem I’m trying to solve.
- Another option will be to force the developer to register all the map keys in advance via a separate
register(std::string FieldName)method at the program startup and then validate the configuration file fields against it.
- Last method I thought was to run the actual processing method with some mock data on the DB. If any undefined field (in configuration) is used, then the errors will be fired before the BAU.
What I still wonder is whether there is a better way to handle this problem than above methods to make this implementation future-proof.