-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added env parser inside the library (#24)
- Loading branch information
Showing
4 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package goconf | ||
|
||
import "github.com/caarlos0/env/v11" | ||
|
||
// ParseEnv parse the env values to given struct fields | ||
// env variables are defined using struct tags. It utilizes the "github.com/caarlos0/env/v11" | ||
// package for parse. | ||
// | ||
// Parameters: | ||
// - config (interface{}): The struct to be populated by env variables. The struct should have | ||
// env variables defined using tags such as `env:"USERNAME"`. | ||
// | ||
// Returns: | ||
// - error: Returns nil if the struct created using env variables. if fails, it returns an error | ||
// which provides details information about the error | ||
// | ||
// Usage Example: | ||
// | ||
// type Config struct { | ||
// Name string `env:"MY_NAME"` | ||
// Age int `env:"MY_AGE"` | ||
// } | ||
// | ||
// var conf Config | ||
// if err := Register(&conf); err != nil { | ||
// // Handle env pass error, e.g., log or return | ||
// } | ||
// | ||
// Note: | ||
// - The function will panic if the `config` parameter is not a pointer to struct | ||
// | ||
// More env package information https://github.com/caarlos0/env/v11 | ||
func ParseEnv(config interface{}) error { | ||
return env.Parse(config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package goconf | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type Conf struct { | ||
Name string `env:"MY_NAME"` | ||
Age int `env:"MY_AGE"` | ||
Team string `env:"MY_TEAM"` | ||
} | ||
|
||
func TestParseEnv(t *testing.T) { | ||
updateEnv() | ||
tests := []struct { | ||
name string | ||
input Conf | ||
expectedOutput Conf | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "Successfully parsed the configs", | ||
input: Conf{}, | ||
expectedOutput: Conf{ | ||
Name: "coderx", | ||
Age: 99, | ||
Team: "backend", | ||
}, | ||
expectedErr: "", | ||
}, | ||
{ | ||
name: "config parse failure scenario", | ||
input: Conf{}, | ||
expectedOutput: Conf{}, | ||
expectedErr: "env: expected a pointer to a Struct", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if test.expectedErr == "" { | ||
err := ParseEnv(&test.input) | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.expectedOutput, test.input) | ||
} else { | ||
err := ParseEnv(test.input) | ||
require.ErrorContains(t, err, test.expectedErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func updateEnv() { | ||
_ = os.Setenv("MY_NAME", "coderx") | ||
_ = os.Setenv("MY_AGE", "99") | ||
_ = os.Setenv("MY_TEAM", "backend") | ||
} |