hilbert / hilbert-cli

Backend management tools: CLI
Apache License 2.0
6 stars 2 forks source link

Configuration Validator: modularity #35

Open malex984 opened 7 years ago

malex984 commented 7 years ago

@elondaits commented on the internals of hilbert-cli-config.py:

In my experience (and this is not a hard rule, just a comment on errors I made in my past) it's better if individual components consume "their part" of the cfg on their own, and validate it on their own. The config loader simply validates the general syntax (what the YAML parser is doing now). That way you can modularly extend the system and each module/class "knows" what the correct options and values are. If you have a single "config validator" class then that class holds internal knowledge of every class in the system, which breaks modularity.

malex984 commented 7 years ago

@malex984's reply :

each Validator class only knows about its structure (which may depend on the format version). Tool can extend by adding new Validator classes and new rules to existing one (for later format versions).

malex984 commented 7 years ago

@elondaits reply:

For instance, your StationPowerOnMethod class is instantiated by Station... so Station "knows" StationPowerOnMethod...so it's coupled. Following the dependency tree from the top down you get to every class.

The alternative that usually works better for me is for every component to handle its own config. Analogous to how the Windows registry is used by different applications and they are free to use whatever keys they want (either their own or any others they have access to). The config is a centralized store of operational parameters, but each component knows what information it needs from it.

malex984 commented 7 years ago

IMO poweron_settings: StationPowerOnMethod is an integral part of a Station, which was separated only to make a clear structure due to multiple types (e.g. it may need Station's address).

@elondaits Which parts of our current configuration you would treat that way? Do you know any open example for such a modular design?

elondaits commented 7 years ago

In complex applications I tried the approach where the top level component (in the object instantiation tree) read the CFG and then passed on to its children the parts they needed (and those parts passed relevant cfg to lower levels, etc.) This creates messy code where encapsulation and modularity are broken.

Instead I had better luck in the approach where the cfg is loaded, parsed, and either kept as a global object or passed to new objects that require it always as a full thing. Then each object knows which parts of the cfg to read on its own... and don't care about the rest.

In the case above, even if the concept of "power on settings" is essential to the station, that doesn't mean the station has to know anything about what settings are needed for that. Does the component in charge of powering on stations require a MAC? An IP? A URL? A timeout value? Some other parameter? The station shouldn't know or care, because that's relative to the internals of the module doing the power on (which could even have special settings for debugging).

Validating the whole cfg is a harder problem with a modular design, because no module knows the full description. To this, I have three things to say:

malex984 commented 7 years ago

@porst17 @elondaits I am planning to create a json schema for validation of Hilbert YAML Configuration following Docker-Compose approach: https://github.com/docker/compose/tree/master/compose/config

IMO this has low priority ATM since it is an improvement/re-implementation of what is already available. Ok?

elondaits commented 7 years ago

Why JSON and not a YAML schema? Kwalify and Rx (two of the more popular schema validation libraries for YAML) support YAML schemas.

But I agree it should be low priority.

malex984 commented 7 years ago

@elondaits I have no real preferences yet (just stumbled upon several advises about json schema validation...)

ps: here are some suggestions: http://stackoverflow.com/questions/3262569/validating-a-yaml-document-in-python

elondaits commented 7 years ago

I see the second answer suggesting using a JSON schema validator based on the fact that they're "similar" so you can validate "a sizeable subset of YAML", but there are enough YAML schema validators and most of the ones I've seen do both.

malex984 commented 6 years ago

@porst17 no such modular Validator. Can we postpone this for some later project?