英文:
reading bool value using viper in Go
问题
我正在使用viper来管理Go应用程序的配置和环境变量。除了布尔值之外,所有的值都从JSON配置文件中正确获取,即使在JSON文件中有一个true值,布尔值始终为false。
{
"database" : {
"host" : "localhost",
"port": "3306"
},
"host": "localhost",
"port": ":3000",
"production": true,
"cache": true
}
配置包如下:
package config
import (
"github.com/spf13/viper"
"html/template"
"log"
)
type ViperConfig struct {
Database struct {
Host string `json:'host'`
Port string `json:'port'`
} `json:'database'`
Host string `json:'host'`
Port string `json:'port'`
ProductionMod bool `json:'production'`
UseCache bool `json:'cache'`
TemplaCache map[string]*template.Template
}
func LoadConfig(path string) (viperconfig ViperConfig, err error) {
viper.AddConfigPath(path)
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AutomaticEnv()
err = viper.ReadInConfig()
if err != nil {
log.Fatal("无法加载配置文件")
}
err = viper.Unmarshal(&viperconfig)
return
}
当我尝试访问任何字符串时,所有这些都能正常工作,但是当我尝试访问布尔变量时,它总是返回false。
package main
import (
"github.com/alexedwards/scs/v2"
"github.com/fouad82/news-api/cmd/config"
"github.com/fouad82/news-api/cmd/routes"
"log"
"net/http"
"time"
)
func main() {
ViperConfig, err := config.LoadConfig("../")
if err != nil {
log.Fatal("无法读取viper配置", err)
}
if err != nil {
log.Fatal("解析模板时出错", err)
}
session := scs.New()
session.Lifetime = 24 * time.Hour
session.Cookie.Persist = true
session.Cookie.SameSite = http.SameSiteLaxMode
session.Cookie.Secure = ViperConfig.ProductionMod
routes.Routes()
// 这些值始终为false,而不是实际值
log.Fatal(ViperConfig.UseCache, ViperConfig.ProductionMod)
http.ListenAndServe(ViperConfig.Port, nil)
}
英文:
I'm using viper to manage the configurations and environment variables for a Go app .<br>
all of values are coming correct from the json config file expect the bool values are always coming false even it have a true value within the json file
{
"database" : {
"host" : "localhost",
"port": "3306"
},
"host": "localhost",
"port": ":3000",
"production": true,
"cache": true
}
the configuration package
package config
import (
"github.com/spf13/viper"
"html/template"
"log"
)
type ViperConfig struct {
Database struct {
Host string "json:'host'"
Port string "json:'port'"
} "json:'database'"
Host string "json:'host'"
Port string "json:'port'"
ProductionMod bool "json:'production'"
UseCache bool "json:'cache'"
TemplaCache map[string]*template.Template
}
func LoadConfig(path string) (viperconfig ViperConfig, err error) {
viper.AddConfigPath(path)
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AutomaticEnv()
err = viper.ReadInConfig()
if err != nil {
log.Fatal("Can't load config file")
}
err = viper.Unmarshal(&viperconfig)
return
}
all of this are working file when i tried to access any string but when tried to access the bool variables it always give false
package main
import (
"github.com/alexedwards/scs/v2"
"github.com/fouad82/news-api/cmd/config"
"github.com/fouad82/news-api/cmd/routes"
"log"
"net/http"
"time"
)
func main() {
ViperConfig, err := config.LoadConfig("../")
if err != nil {
log.Fatal("Can't read viper configurations", err)
}
if err != nil {
log.Fatal("Error Parsing template", err)
}
session := scs.New()
session.Lifetime = 24 * time.Hour
session.Cookie.Persist = true
session.Cookie.SameSite = http.SameSiteLaxMode
session.Cookie.Secure = ViperConfig.ProductionMod
routes.Routes()
// the values never gives the actuall value it always gives false
log.Fatal(ViperConfig.UseCache, ViperConfig.ProductionMod)
http.ListenAndServe(ViperConfig.Port, nil)
}
答案1
得分: 2
首先,json标签的写法是错误的。
我认为viper
不使用json
标签,而是使用mapstructure
标签。
其他变量能够正常工作是因为变量的名称与映射到json标签上的名称相同。(请查看https://github.com/spf13/viper#unmarshaling)
有两种解决方案:
第一种:更改变量名称
type ViperConfig struct {
Database struct {
Host string `json:"host"`
Port string `json:"port"`
} `json:"database"`
Host string `json:"host"`
Port string `json:"port"`
Production bool `json:"production"`
Cache bool `json:"cache"`
TemplaCache map[string]*template.Template
}
或者使用mapstructure标签
type ViperConfig struct {
Database struct {
Host string `json:"host"`
Port string `json:"port"`
} `json:"database"`
Host string `json:"host"`
Port string `json:"port"`
ProductionMod bool `json:"production" mapstructure:"production"`
UseCache bool `json:"cache" mapstructure:"cache"`
TemplaCache map[string]*template.Template
}
英文:
First the json tags are written wrong.
I think viper
doesn't use json
tags. It uses the mapstructure
tags.
The other variables are working because the name of the variable is the same mapped on the json tags. (check this https://github.com/spf13/viper#unmarshaling)
2 solutions:
First: change the variables name
type ViperConfig struct {
Database struct {
Host string `json:"host"`
Port string `json:"port"`
} `json:"database"`
Host string `json:"host"`
Port string `json:"port"`
Production bool `json:"production"`
Cache bool `json:"cache"`
TemplaCache map[string]*template.Template
}
Or use mapstructure tags
type ViperConfig struct {
Database struct {
Host string `json:"host"`
Port string `json:"port"`
} `json:"database"`
Host string `json:"host"`
Port string `json:"port"`
ProductionMod bool `json:"production" mapstructure:"production"`
UseCache bool `json:"cache" mapstructure:"cache"`
TemplaCache map[string]*template.Template
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论