loganasherjones / yapconf

Yet Another Python Configuration
http://yapconf.readthedocs.io/en/stable/
MIT License
18 stars 4 forks source link

Fallback values #45

Closed loganasherjones closed 6 years ago

loganasherjones commented 6 years ago

I may want to specify the following:

SPEC = {
    'defaults': {
        'host': {'type': 'str', 'default': 'localhost'},
    },
    'app1': {
        'host': {'type': 'str', 'fallback': 'defaults.host'},
    },
}

Should be able to load app1.host if its specified, or if it is in defaults.host.

hazmat345 commented 6 years ago

:+1:

loganasherjones commented 6 years ago

Okay, there are some pretty interesting corner cases here. Let's say I have the following:

spec = {
  'my_string': {'type': 'str', 'fallback': 'fallback_str'},
  'fallback_str': {'type': 'str', 'default': 'default_from_fallback'},
}

If we load the config, the following should work:

config = spec.load_config()
assert config == {'my_string': 'default_from_fallback', 'fallback_str': 'default_from_fallback'}

Now let's say we had a config like the following:

config = spec.load_config(
  {'fallback_str': 'config1_value'}, 
  {'my_string': 'config2_value'}
)

Should my_string == "config1_value" or should my_string == "config2_value"?

hazmat345 commented 6 years ago

Sorry to bump this after closing, but will this work for dictionaries? For example, do you think this will work:

spec:

# 'default' ssl options to use for all connections
'ssl': {
    'type': 'dict',
    'items': {
        "enabled": {
            "type": "bool",
            "default": False,
            "description": "Serve content using SSL",
        },  
        "private_key": {
            "type": "str",
            "description": "Path to a private key",
            "required": False,
        },  
        "public_key": {
            "type": "str",
            "description": "Path to a public key",
            "required": False,
        },
    },
},
...
'connection1':
    'type': 'dict',
    'items': {
        'host': {
            "type": "str",
            "description": "The host",
        },
        # ssl config for this specific connection
        "ssl": {
            "type": "dict",
            "fallback": "ssl",
         },
    },
},

So ideally i'd specify values for ssl_enabled, ssl_private_key, and ssl_public_key and they would be used in connection1 (and others not shown). Then I could specify a different value to modify the more specific config (connection1_ssl_enabled=False).

loganasherjones commented 6 years ago

So, this example by itself does not work. Right now this would throw an error because you define the ssl item as a dictionary with no items which would not be allowed. The fallback only applies to the values, not the specification itself. So you would still have to define enabled, private_key and public_key for the ssl dictionary under connection1. Furthermore, you'd have to define that you want those to have fallbacks of ssl.public_key, ssl.private_key, etc

loganasherjones commented 6 years ago

I could see how this would be useful. It is probably worth adding.

I'll open another issue for the new feature.