Viper: Unmarshal returning an empty struct

Created on 26 Jul 2019  路  3Comments  路  Source: spf13/viper

I have a toml config file that I am able to perfectly read from viper.
If I use viper.ReadInconfig I can access all values. But if I try to Unmarshal them in a struct, the structs remains empty.
Here is my config.toml file:

[generale]

approot = "D:\\Documenti\\Progetti Sw\\DrClick_go\\"
serverroot = "server\\src\\"
logdir = "server\\src\\log\\"
separator = "\\"
enabled = false
domain =  "//www.dottnet.it"
cookiedomain = ".dottnet.it"
sessionexpiration = 30
analytics = "Mycode"

[address]
address = "127.0.0.1"
port = "8000"

[database]
tipo = "mysql"
server="db2.dottnet.it" 
port = "3306"
user_id="myuser"
password="mypassword"
database="test_dottnetct"
[dbtemplates]
mysql = "<user_id>:<password>@tcp(<server>:<port>)/<database>"
mssql = "server=<server>;user id=<user_id>;password=<password>;database=<database>"

[log]
echo = true
loglevel = 1
prefix = "drclick"

````
And here is the code I use to read (works perfectly) and to unmarshal (returns an empty struct)
I've tried also without tags but nothing changed.

package util

import (
"log"

"github.com/spf13/viper"

)

//Config 猫 la struttura che contiene tutte le configurazioni
type Config struct {
generale struct {
approot string mapstructure:"approot"
serverroot string mapstructure:"serverroot"
logdir string mapstructure:"logdir"
separator string mapstructure:"separator"
enabled bool mapstructure:"enabled"
domain string mapstructure:"domain"
cookiedomain string mapstructure:"cookiedomain"
sessionexpiration int mapstructure:"sessionexpiration"
analytics string mapstructure:"analytics"
} mapstructure:"generale"
address struct {
address string mapstructure:"address"
port string mapstructure:"port"
} mapstructure:"address"
database struct {
tipo string mapstructure:"tipo"
server string mapstructure:"server"
port string mapstructure:"port"
userid string mapstructure:"user_id"
password string mapstructure:"password"
database string mapstructure:"database"
} mapstructure:"database"
dbtemplates struct {
mysql string mapstructure:"mysql"
mssql string mapstructure:"mssql"
} mapstructure:"dbtemplates"
log struct {
echo bool mapstructure:"echo"
loglevel int mapstructure:"loglevel"
prefix string mapstructure:"prefix"
} mapstructure:"log"
}

// ContextKey 猫 un tipo creato per scrivere in un contesto
type ContextKey string

// Dirs 猫 la struutura delle cartelle dell'app
type Dirs struct {
AppRoot string
ServerRoot string
LogDir string
Separator string
}

// GetConfigurazione legge la configurazione da config.toml
func GetConfigurazione() *viper.Viper {

var conf Config
Myviper := viper.New()
Myviper.SetConfigName("config")
Myviper.AddConfigPath("..")
Myviper.SetConfigType("toml")

if err := Myviper.ReadInConfig(); err != nil {
    log.Fatalf("Error reading config file, %s", err)
}
if err := Myviper.Unmarshal(&conf); err != nil {
    log.Fatalf("Error reading config file, %s", err)
}
return Myviper

}

```

Most helpful comment

Hi,

your fieldnames the fist letter must be uppercase other wise the field will not exported and the unmarshal function can not access it.

like:
Approot string mapstructure:"approot"
Serverroot string mapstructure:"serverroot"
Logdir string mapstructure:"logdir"
Separator string mapstructure:"separator"
......

best regards

All 3 comments

Hi,

your fieldnames the fist letter must be uppercase other wise the field will not exported and the unmarshal function can not access it.

like:
Approot string mapstructure:"approot"
Serverroot string mapstructure:"serverroot"
Logdir string mapstructure:"logdir"
Separator string mapstructure:"separator"
......

best regards

thanks @messias72 ,
it has worked perfectly.
My mistake

Holly crap! My problem was exactly the same, fields in lowercase. Could have a note in the readme about it, would help a lot! Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jgoodall picture jgoodall  路  5Comments

gmaxwell94 picture gmaxwell94  路  5Comments

therealfakemoot picture therealfakemoot  路  5Comments

noandrea picture noandrea  路  4Comments

xh3b4sd picture xh3b4sd  路  6Comments