wscott / fplan

Early retirement financial calculator
GNU General Public License v3.0
26 stars 6 forks source link

Validate input file and complain about any unused data items #30

Open wscott opened 5 months ago

wscott commented 5 months ago

If I had a regression framework setup, I would add some test cases to demonstrate that extra data generates warning messages.

wscott commented 5 months ago

This code could be simplified with the right helper function, but I ran out of time to mess with this at the moment.

cmovic commented 5 months ago

A simpler or at least less intrusive way to do this is to define a set of known keys and use set difference to see if there are any unknown keys prior to parsing the data. Here's a hacky example:

    def load_file(self, file):
        with open(file) as conffile:
            d = tomllib.loads(conffile.read())

        known_keys = {'returns', 'inflation', 'startage', 'endage', 'taxes', 'prep', 'income', 'expense', 'aftertax', 'IRA', 'roth'}
        # known_keys = {'inflation', 'startage', 'endage', 'taxes', 'prep', 'income', 'expense', 'aftertax', 'IRA', 'roth'}   # BAD missing key
        # known_keys = {'moose', 'returns', 'inflation', 'startage', 'endage', 'taxes', 'prep', 'income', 'expense', 'aftertax', 'IRA', 'roth'} # OK extra key
        extra_keys = d.keys() - known_keys
        if extra_keys:
            for k in extra_keys:
                print(f'{k} = {d[k]}')
            raise Exception(f'Unknown keys found in config file: {file}')