jeudi 25 juillet 2019

Go: what is the right way to use go-ini's mapTo function?

Background

I'm trying to use go-fed's apcore framework to build a federated app. I have implemented the apcore.Application interface but I'm stuck during the loading of the configuration.

The server responds with panic: reflect: call of reflect.Value.Type on zero Value when trying to map the configuration loaded from config.ini to the apcore configuration struct.

This happens here:

func loadConfigFile(filename string, a Application, debug bool) (c *config, err error) {
    InfoLogger.Infof("Loading config file: %s", filename)
    var cfg *ini.File
    cfg, err = ini.Load(filename)
    if err != nil {
        return
    }
    err = cfg.MapTo(c)
    if err != nil {
        return
    }
.
.
.

where it clearly just uses an uninitialized *config pointer. This led me to believe this has nothing to do with the code I've written (an implementation of Application) which has not been yet used here.

The question

I decided to write a very simple usecase of MapTo to check if it works as I expect it to:

package main

import (
    "fmt"
    "gopkg.in/ini.v1"
)

type Config struct {
    Name      string `ini:"NAME"`
    Male      bool
    Age       int `comment:"Author's age"`
    GPA       float64
}

func main() {
    var cfg *ini.File
    var err error
    cfg, err = ini.Load("config.ini")
    var c *Config
    // c := &Config{ Name: "test", Male: true, Age: 13, GPA: 4.5 }
    // c := new(Config)
    fmt.Println(c);
    err = cfg.MapTo(c)
    fmt.Println(err.Error())
}

This also returns panic: reflect: call of reflect.Value.Type on zero Value. Neither passing a Config literal works (it then complains that it cannot map to non-pointer struct), nor an initialized pointer (signal SIGSEGV: segmentation violation).

This is the function declaration

func (s *Section) mapTo(val reflect.Value, isStrict bool) error {

What does it require as input and why does the go-ini library use it this way if it fails?

(I also tried with older go-ini just in case it's a recent bug)





Aucun commentaire:

Enregistrer un commentaire