Skip to content

Commit

Permalink
Add default value option.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiavrammsd committed Jan 6, 2020
1 parent 7a610b3 commit 56beaf0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

type Config struct {
Username string `env:"USERNAME"`
Tag string `env:"TAG" default:"none"`
}

func main() {
Expand All @@ -33,6 +34,7 @@ func main() {
}

fmt.Println(cfg.Username)
fmt.Println(cfg.Tag)
}
```

Expand Down
1 change: 0 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"os"
)

const tag = "env"
const dotEnvFile = ".env"

// Loader provides methods to load configuration values into a struct
Expand Down
7 changes: 5 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,6 +47,7 @@ type Config struct {
UA uint8
IsSet bool
Interpolated string
Default string `default:"default value"`
}

type Struct struct {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -372,6 +374,7 @@ func testdata() ([]byte, Config, error) {
},
}},
Interpolated: "$B env_1 $ $B \\3 6379 + $",
Default: "default value",
}

return input, expected, nil
Expand Down
3 changes: 3 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type Config struct {
Username string `env:"USERNAME"`
Tag string `env:"TAG" default:"none"`
}

func ExampleLoader_Env() {
Expand All @@ -23,9 +24,11 @@ func ExampleLoader_Env() {
}

fmt.Println(cfg.Username)
fmt.Println(cfg.Tag)

// Output:
// msd
// none
}

func ExampleLoader_Bytes() {
Expand Down
13 changes: 13 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"strings"
)

const (
tag = "env"
defaultValueTag = "default"
)

type getValue func(string) string

func parseIntoStruct(i interface{}, data getValue) error {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 56beaf0

Please sign in to comment.