本文整理汇总了Golang中github.com/vrischmann/envconfig.Init函数的典型用法代码示例。如果您正苦于以下问题:Golang Init函数的具体用法?Golang Init怎么用?Golang Init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestParseSimpleConfig
func TestParseSimpleConfig(t *testing.T) {
var conf struct {
Name string
Log struct {
Path string
}
}
err := envconfig.Init(&conf)
require.Equal(t, "envconfig: keys NAME, name not found", err.Error())
os.Setenv("NAME", "foobar")
err = envconfig.Init(&conf)
require.Equal(t, "envconfig: keys LOG_PATH, log_path not found", err.Error())
os.Setenv("LOG_PATH", "/var/log/foobar")
err = envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "foobar", conf.Name)
require.Equal(t, "/var/log/foobar", conf.Log.Path)
// Clean up at the end of the test - some tests share the same key and we don't values to be seen by those tests
os.Setenv("NAME", "")
os.Setenv("LOG_PATH", "")
}
示例2: TestParseWrongValues
func TestParseWrongValues(t *testing.T) {
var conf struct{ OK bool }
os.Setenv("OK", "foobar")
err := envconfig.Init(&conf)
require.Equal(t, `strconv.ParseBool: parsing "foobar": invalid syntax`, err.Error())
var conf2 struct{ Port int }
os.Setenv("PORT", "foobar")
err = envconfig.Init(&conf2)
require.Equal(t, `strconv.ParseInt: parsing "foobar": invalid syntax`, err.Error())
var conf3 struct{ Port uint }
os.Setenv("PORT", "foobar")
err = envconfig.Init(&conf3)
require.Equal(t, `strconv.ParseUint: parsing "foobar": invalid syntax`, err.Error())
var conf4 struct{ Port float32 }
os.Setenv("PORT", "foobar")
err = envconfig.Init(&conf4)
require.Equal(t, `strconv.ParseFloat: parsing "foobar": invalid syntax`, err.Error())
var conf5 struct{ Data []byte }
os.Setenv("DATA", "foobar")
err = envconfig.Init(&conf5)
require.Equal(t, "illegal base64 data at input byte 4", err.Error())
}
示例3: TestParseDefaultVal
func TestParseDefaultVal(t *testing.T) {
var conf struct {
MySQL struct {
Master struct {
Address string `envconfig:"default=localhost"`
Port int `envconfig:"default=3306"`
}
Timeout time.Duration `envconfig:"default=1m,myTimeout"`
LocalTimeout time.Duration `envconfig:"myTimeout2,default=1m"`
}
}
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "localhost", conf.MySQL.Master.Address)
require.Equal(t, 3306, conf.MySQL.Master.Port)
require.Equal(t, time.Minute*1, conf.MySQL.Timeout)
os.Setenv("myTimeout", "2m")
os.Setenv("myTimeout2", "20m")
err = envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "localhost", conf.MySQL.Master.Address)
require.Equal(t, 3306, conf.MySQL.Master.Port)
require.Equal(t, time.Minute*2, conf.MySQL.Timeout)
require.Equal(t, time.Minute*20, conf.MySQL.LocalTimeout)
}
示例4: TestParseSliceConfig
func TestParseSliceConfig(t *testing.T) {
var conf struct {
Names []string
Ports []int
Shards []struct {
Name string
Addr string
}
}
os.Setenv("NAMES", "foobar,barbaz")
os.Setenv("PORTS", "900,100")
os.Setenv("SHARDS", "{foobar,localhost:2929},{barbaz,localhost:2828}")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, 2, len(conf.Names))
require.Equal(t, "foobar", conf.Names[0])
require.Equal(t, "barbaz", conf.Names[1])
require.Equal(t, 2, len(conf.Ports))
require.Equal(t, 900, conf.Ports[0])
require.Equal(t, 100, conf.Ports[1])
require.Equal(t, 2, len(conf.Shards))
require.Equal(t, "foobar", conf.Shards[0].Name)
require.Equal(t, "localhost:2929", conf.Shards[0].Addr)
require.Equal(t, "barbaz", conf.Shards[1].Name)
require.Equal(t, "localhost:2828", conf.Shards[1].Addr)
}
示例5: TestParseOptionalConfig
func TestParseOptionalConfig(t *testing.T) {
var conf struct {
Name string `envconfig:"optional"`
Flag bool `envconfig:"optional"`
Timeout time.Duration `envconfig:"optional"`
Port int `envconfig:"optional"`
Port2 uint `envconfig:"optional"`
Delta float32 `envconfig:"optional"`
DeltaV float64 `envconfig:"optional"`
Slice []string `envconfig:"optional"`
Struct struct {
A string
B int
} `envconfig:"optional"`
}
os.Setenv("NAME", "")
os.Setenv("FLAG", "")
os.Setenv("TIMEOUT", "")
os.Setenv("PORT", "")
os.Setenv("PORT2", "")
os.Setenv("DELTA", "")
os.Setenv("DELTAV", "")
os.Setenv("SLICE", "")
os.Setenv("STRUCT", "")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "", conf.Name)
}
示例6: TestInvalidDurationConfig
func TestInvalidDurationConfig(t *testing.T) {
var conf struct {
Timeout time.Duration
}
os.Setenv("TIMEOUT", "foo")
err := envconfig.Init(&conf)
require.NotNil(t, err)
}
示例7: TestInvalidSliceElementValueKind
func TestInvalidSliceElementValueKind(t *testing.T) {
var conf struct {
Foo []interface{}
}
os.Setenv("FOO", "lalala")
err := envconfig.Init(&conf)
require.Equal(t, "envconfig: kind interface not supported", err.Error())
}
示例8: initializeConfig
// initialize config and turns it to map
func initializeConfig() map[string]string {
err := envconfig.Init(&conf)
if err != nil {
log.Panic("Error on env config initialize! ", err.Error())
}
return map[string]string{
"dbConnection": conf.DB.Connection,
}
}
示例9: TestUnmarshaler
func TestUnmarshaler(t *testing.T) {
var conf struct {
LogMode logMode
}
os.Setenv("LOGMODE", "file")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, logFile, conf.LogMode)
var conf2 struct {
LogMode *logMode
}
err = envconfig.Init(&conf2)
require.Nil(t, err)
require.Equal(t, logFile, *conf2.LogMode)
}
示例10: ExampleInit
func ExampleInit() {
var conf struct {
MySQL struct {
Host string
Port int
Database struct {
User string
Password string
Name string
}
Params struct {
Charset string `envconfig:"-"`
}
}
Log struct {
Path string `envconfig:"default=/var/log/mylog.log"`
Rotate bool `envconfig:"logRotate"`
}
NbWorkers int
Timeout time.Duration
Cassandra struct {
SSLCert string
SSLKey string
}
}
os.Setenv("MYSQL_HOST", "localhost")
os.Setenv("MYSQL_PORT", "3306")
os.Setenv("MYSQL_DATABASE_USER", "root")
os.Setenv("MYSQL_DATABASE_PASSWORD", "foobar")
os.Setenv("MYSQL_DATABASE_NAME", "default")
os.Setenv("logRotate", "true")
os.Setenv("NBWORKERS", "10")
os.Setenv("TIMEOUT", "120s")
os.Setenv("CASSANDRA_SSL_CERT", "/etc/cassandra/ssl.crt")
os.Setenv("CASSANDRA_SSL_KEY", "/etc/cassandra/ssl.key")
if err := envconfig.Init(&conf); err != nil {
fmt.Printf("err=%s\n", err)
}
fmt.Println(conf.MySQL.Database.User)
fmt.Println(conf.Log.Rotate)
fmt.Println(conf.Timeout)
fmt.Println(conf.Log.Path)
fmt.Println(conf.Cassandra.SSLCert)
fmt.Println(conf.Cassandra.SSLKey)
// Output:
// root
// true
// 2m0s
// /var/log/mylog.log
// /etc/cassandra/ssl.crt
// /etc/cassandra/ssl.key
}
示例11: TestParseEmptyTag
func TestParseEmptyTag(t *testing.T) {
var conf struct {
Name string `envconfig:""`
}
os.Setenv("NAME", "foobar")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "foobar", conf.Name)
}
示例12: TestParseMapType
func TestParseMapType(t *testing.T) {
var conf struct {
Map myMapType
}
os.Setenv("MAP", "a")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, 1, conf.Map["a"])
}
示例13: TestParseCustomNameConfig
func TestParseCustomNameConfig(t *testing.T) {
var conf struct {
Name string `envconfig:"customName"`
}
os.Setenv("customName", "foobar")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "foobar", conf.Name)
}
示例14: TestParseSkippableConfig
func TestParseSkippableConfig(t *testing.T) {
var conf struct {
Flag bool `envconfig:"-"`
}
os.Setenv("FLAG", "true")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, false, conf.Flag)
}
示例15: TestParseBoolConfig
func TestParseBoolConfig(t *testing.T) {
var conf struct {
DoIt bool
}
os.Setenv("DOIT", "true")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, true, conf.DoIt)
}