Description
Hello @unknwon,
I am thinking to add support for loading from .ini
for Iris Configuration and custom configurations. So far we have support for json, yaml and toml and they're working fine. I have a problem though, while trying to read a config file to the iris.Configuration
structure, I have defined the ini
fields, I tried allowShadow
with custom ini.LoaderOptions
but that doesn't work either. Code speaks by itself:
type Configuration struct {
Tunneling TunnelingConfiguration `ini:"tunneling"`
}
type TunnelingConfiguration struct {
WebInterface string `ini:"web_interface"`
Tunnels []Tunnel `ini:"tunnels"`
}
type Tunnel struct {
Name string `ini:"name"`
Addr string `ini:"addr"`
}
I tried plenty of ini formats but I would love to support something like that (if already exists, I couldn't find it):
[tunneling]
web_interface = http://127.0.0.1:5050
[tunneling.tunnels]
name = tunnel1
addr = test1
[tunneling.tunnels]
name = tunnel2
addr = test2
How I load
b, err := ioutil.ReadFile(filename)
f, err := ini.LoadSources(ini.LoadOptions{
Insensitive: true,
InsensitiveKeys: true,
InsensitiveSections: true,
AllowNonUniqueSections: true,
// AllowShadows: true,
DebugFunc: func(s string) {
fmt.Printf("debug: %s\n", s)
},
}, b)
return f.StrictMapTo(dest) // where dest is *Configuration
So even if AllowNonUniqueSections
is true, the Tunnels
are never binded to the dest
one.
I did manage to do it by using this code,befoer StrictMapTo:
if sections, err := f.SectionsByName("tunneling.tunnels"); err == nil {
for _, section := range sections {
nameKey, err := section.GetKey("name")
if err != nil || nameKey == nil {
continue
}
name := nameKey.Value()
if name == "" {
continue
}
addrKey, err := section.GetKey("addr")
if err != nil || addrKey == nil {
continue
}
addr := addrKey.Value()
dest.Tunneling.Tunnels = append(dest.Tunneling.Tunnels, iris.Tunnel{
Name: name,
Addr: addr,
})
}
}
Is there a way to do that mapping automatically or it's a planned feature?
I think would be trivial to do that, you already collecting multi sections of the same key under a section, so why not add support for appending them to the corresponding field?
Thanks,
Gerasimos Maropoulos.
Activity