Skip to content

Commit

Permalink
feat: Added env parser inside the library (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
coderx31 authored Jan 18, 2025
1 parent dea11a4 commit 6404bb9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 5 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package main

import (
"errors"
"github.com/caarlos0/env/v11"
"github.com/wgarunap/goconf"
"log"
)
Expand All @@ -28,7 +27,7 @@ type Conf struct {
var Config Conf

func (Conf) Register() error {
return env.Parse(&Config)
return goconf.ParseEnv(&Config)
}

func (Conf) Validate() error {
Expand Down
4 changes: 1 addition & 3 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"log"
"os"

"github.com/caarlos0/env/v11"

"github.com/wgarunap/goconf"
)

Expand All @@ -19,7 +17,7 @@ type Conf struct {
var Config Conf

func (Conf) Register() error {
return env.Parse(&Config)
return goconf.ParseEnv(&Config)
}

func (Conf) Validate() error {
Expand Down
35 changes: 35 additions & 0 deletions parse.go
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)
}
61 changes: 61 additions & 0 deletions parse_test.go
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")
}

0 comments on commit 6404bb9

Please sign in to comment.