From 8417b17efe14ea5ae78658200759bb2f0976d165 Mon Sep 17 00:00:00 2001 From: Arun Mathew Date: Thu, 23 Apr 2020 22:31:04 +0530 Subject: [PATCH 1/2] Added support for edn files --- README.md | 1 + cleanenv.go | 8 ++++++++ go.mod | 1 + go.sum | 2 ++ 4 files changed, 12 insertions(+) diff --git a/README.md b/README.md index 4f0457c..2670882 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,7 @@ There are several most popular config file formats supported: - JSON - TOML - ENV +- EDN ## Integration diff --git a/cleanenv.go b/cleanenv.go index b159510..31476c3 100644 --- a/cleanenv.go +++ b/cleanenv.go @@ -30,6 +30,7 @@ import ( "github.com/BurntSushi/toml" "github.com/joho/godotenv" "gopkg.in/yaml.v2" + "olympos.io/encoding/edn" ) const ( @@ -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: @@ -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. diff --git a/go.mod b/go.mod index 65cc8b7..00f2e53 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 2e8c64e..137cc5e 100644 --- a/go.sum +++ b/go.sum @@ -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= From cce160ebd6e4c299e1a2c8705839a37dc2289bde Mon Sep 17 00:00:00 2001 From: Arun Mathew Date: Sat, 25 Apr 2020 12:26:24 +0530 Subject: [PATCH 2/2] Added tests for reading configuration from edn files --- cleanenv_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/cleanenv_test.go b/cleanenv_test.go index 937c6ff..ea4cc34 100644 --- a/cleanenv_test.go +++ b/cleanenv_test.go @@ -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 { @@ -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: `