From 56beaf08c06f610087f119460d51ce26b721ff18 Mon Sep 17 00:00:00 2001 From: Andrei Avram Date: Mon, 6 Jan 2020 11:06:34 +0200 Subject: [PATCH] Add default value option. --- README.md | 2 ++ config.go | 1 - config_test.go | 7 +++++-- examples_test.go | 3 +++ parser.go | 13 +++++++++++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2fafac5..5dff1ea 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ import ( type Config struct { Username string `env:"USERNAME"` + Tag string `env:"TAG" default:"none"` } func main() { @@ -33,6 +34,7 @@ func main() { } fmt.Println(cfg.Username) + fmt.Println(cfg.Tag) } ``` diff --git a/config.go b/config.go index 0a94db2..6e4cf64 100644 --- a/config.go +++ b/config.go @@ -17,7 +17,6 @@ import ( "os" ) -const tag = "env" const dotEnvFile = ".env" // Loader provides methods to load configuration values into a struct diff --git a/config_test.go b/config_test.go index 65948d7..7cf4faf 100644 --- a/config_test.go +++ b/config_test.go @@ -28,7 +28,7 @@ type Config struct { Port int `env:"REDIS_PORT"` } } - String string `env:"ABC"` + String string `env:"ABC" default:"ignored"` Struct Struct StructPtr *Struct D int64 @@ -47,6 +47,7 @@ type Config struct { UA uint8 IsSet bool Interpolated string + Default string `default:"default value"` } type Struct struct { @@ -185,7 +186,8 @@ func TestJson(t *testing.T) { "Struct":{ "Field":"Value" }, - "Interpolated":"$B env_1 $ $B \\3 6379 + $" + "Interpolated":"$B env_1 $ $B \\3 6379 + $", + "Default":"default value" }`) _, expected, err := testdata() @@ -372,6 +374,7 @@ func testdata() ([]byte, Config, error) { }, }}, Interpolated: "$B env_1 $ $B \\3 6379 + $", + Default: "default value", } return input, expected, nil diff --git a/examples_test.go b/examples_test.go index 1a7b015..22f7dd4 100644 --- a/examples_test.go +++ b/examples_test.go @@ -10,6 +10,7 @@ import ( type Config struct { Username string `env:"USERNAME"` + Tag string `env:"TAG" default:"none"` } func ExampleLoader_Env() { @@ -23,9 +24,11 @@ func ExampleLoader_Env() { } fmt.Println(cfg.Username) + fmt.Println(cfg.Tag) // Output: // msd + // none } func ExampleLoader_Bytes() { diff --git a/parser.go b/parser.go index b0454c6..c7a3a91 100644 --- a/parser.go +++ b/parser.go @@ -8,6 +8,11 @@ import ( "strings" ) +const ( + tag = "env" + defaultValueTag = "default" +) + type getValue func(string) string func parseIntoStruct(i interface{}, data getValue) error { @@ -51,6 +56,10 @@ func parse(typ reflect.Type, val reflect.Value, getValue getValue, path string) value := value(&field, getValue, path) + if value == "" { + value = defaultValue(&field) + } + if value == "" { continue } @@ -81,6 +90,10 @@ func key(field *reflect.StructField, path string) string { return key } +func defaultValue(field *reflect.StructField) string { + return field.Tag.Get(defaultValueTag) +} + func setFieldValue(field *reflect.StructField, fieldValue reflect.Value, value string) error { switch field.Type.Kind() { case reflect.String: