Skip to content

Commit

Permalink
Add EDN support
Browse files Browse the repository at this point in the history
Added support for edn files
  • Loading branch information
ilyakaznacheev committed Apr 25, 2020
2 parents 5c00d86 + cce160e commit b14a753
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ There are several most popular config file formats supported:
- JSON
- TOML
- ENV
- EDN

## Integration

Expand Down
8 changes: 8 additions & 0 deletions cleanenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/joho/godotenv"
"gopkg.in/yaml.v2"
"olympos.io/encoding/edn"
)

const (
Expand Down Expand Up @@ -124,6 +125,8 @@ func parseFile(path string, cfg interface{}) error {
err = parseJSON(f, cfg)
case ".toml":
err = parseTOML(f, cfg)
case ".edn":
err = parseEDN(f, cfg)
case ".env":
err = parseENV(f, cfg)
default:
Expand Down Expand Up @@ -151,6 +154,11 @@ func parseTOML(r io.Reader, str interface{}) error {
return err
}

// parseEDN parses EDN from reader to data structure
func parseEDN(r io.Reader, str interface{}) error {
return edn.NewDecoder(r).Decode(str)
}

// parseENV, in fact, doesn't fill the structure with environment variable values.
// It just parses ENV file and sets all variables to the environment.
// Thus, the structure should be filled at the next steps.
Expand Down
53 changes: 49 additions & 4 deletions cleanenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,10 +852,10 @@ func TestFUsage(t *testing.T) {

func TestReadConfig(t *testing.T) {
type config struct {
Number int64 `yaml:"number" env:"TEST_NUMBER" env-default:"1"`
String string `yaml:"string" env:"TEST_STRING" env-default:"default"`
NoDefault string `yaml:"no-default" env:"TEST_NO_DEFAULT"`
NoEnv string `yaml:"no-env" env-default:"default"`
Number int64 `edn:"number" yaml:"number" env:"TEST_NUMBER" env-default:"1"`
String string `edn:"string" yaml:"string" env:"TEST_STRING" env-default:"default"`
NoDefault string `edn:"no-default" yaml:"no-default" env:"TEST_NO_DEFAULT"`
NoEnv string `edn:"no-env" yaml:"no-env" env-default:"default"`
}

tests := []struct {
Expand All @@ -866,6 +866,51 @@ func TestReadConfig(t *testing.T) {
want *config
wantErr bool
}{
{
name: "edn_only",
file: `
{
:number 2
:string "test"
:no-default "NoDefault"
:no-env "this"
}
`,
ext: "edn",
env: nil,
want: &config{
Number: 2,
String: "test",
NoDefault: "NoDefault",
NoEnv: "this",
},
wantErr: false,
},

{
name: "edn_and_env",
file: `
{
:number 2
:string "test"
:no-default "NoDefault"
:no-env "this"
}
`,
ext: "edn",
env: map[string]string{
"TEST_NUMBER": "3",
"TEST_STRING": "fromEnv",
},
want: &config{
Number: 3,
String: "fromEnv",
NoDefault: "NoDefault",
NoEnv: "this",
},
wantErr: false,
},

{
name: "yaml_only",
file: `
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require (
github.com/BurntSushi/toml v0.3.1
github.com/joho/godotenv v1.3.0
gopkg.in/yaml.v2 v2.2.2
olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24
)

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24 h1:sreVOrDp0/ezb0CHKVek/l7YwpxPJqv+jT3izfSphA4=
olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=

0 comments on commit b14a753

Please sign in to comment.