Skip to content

Support for unmarshalling of loaded config #61

@kravemir

Description

@kravemir

In extensible/modular application, it's not possible to create statically defined struct with configuration. And, modular configuration would have for example following configuration:

# syntax: <module_key>.<options...>

module1.option1 = "..."
module1.option2.a = "..."
module1.option2.b = "..."
module1.option3.m = "..."
module1.option3.n = "..."
module1.option3.o = "..."

module2.option1.x = "..."
module2.option1.y = "..."
module2.option1.z = "..."
module2.option2 = "..."

# ...

So:

type Module interface {
    LoadConfig(s konfig.Store)
}

type ModuleX struct {}

func (m *ModuleX) LoadConfig(s konfig.Store) {
    var options ModuleXOptions
    s.Unmarshal(&options)
}

func main() {
    var modules map[string]Module = ...
    var config konfig.Store =  ...

    for key, module := range modules {
        // actually, don't know if this works in konfig,... 
        // get all options with prefix `<key>.`
        module.LoadConfig(config.Group(key))
    } 
}

Somewhat similar functionality is provided by https://github.com/knadh/koanf#unmarshalling. But, it would need a bit different Module interface:

type Module interface {
    LoadConfig(path string, k *koanf.Koanf)
}

type ModuleX struct {}

func (m *ModuleX) LoadConfig(path string, k *koanf.Koanf) {
    var options ModuleXOptions
    k.Unmarshal(path, &options)
}

func main() {
    ....
}

Are there any plans for such/similar support?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions