【问题标题】:reading bool value using viper in Go在 Go 中使用 viper 读取布尔值
【发布时间】:2021-07-16 04:15:31
【问题描述】:

我正在使用 viper 来管理 Go 应用程序的配置和环境变量。
json 配置文件中的所有值都是正确的,即使 bool 值在 json 文件中为真值,布尔值也总是为假

{
  "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("Can't load config file")
    }
    err = viper.Unmarshal(&viperconfig)
    return
}

当我尝试访问任何字符串时,所有这些都是工作文件,但是当我尝试访问 bool 变量时,它总是给出错误

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)
}

 

【问题讨论】:

    标签: go viper-go golang-migrate


    【解决方案1】:

    首先json标签写错了。

    我认为viper 不使用json 标签。它使用mapstructure 标签。 其他变量正在工作,因为变量的名称与 json 标记上的映射相同。 (查看https://github.com/spf13/viper#unmarshaling

    2 个解决方案:

    首先:更改变量名

    
    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
    }
    
    

    【讨论】:

    • 它使用 viper 文档中的 json 标签,从 JSON、TOML、YAML、HCL、envfile 和 Java 属性配置文件中读取
    • 否...阅读疑难解答github.com/spf13/viper/blob/master/…
    • "这个问题的最常见原因是结构标签使用不当(例如 yaml 或 json)。Viper 在后台使用 github.com/mitchellh/mapstructure 来解组使用 mapstructure 标签的值默认。使用其他结构标签请参考库的文档。"
    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 2020-10-21
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多