Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ func (e *ParseError) Unwrap() error {
return e.Err
}

// ValidationError is returned when struct validation fails.
type ValidationError struct {
Field string
Message string
}

func (e *ValidationError) Error() string {
return fmt.Sprintf("dotenvgo: validation failed for field %q: %s", e.Field, e.Message)
}

// MultiError contains multiple errors from struct loading.
type MultiError struct {
Errors []error
Expand Down
8 changes: 0 additions & 8 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ func TestErrors(t *testing.T) {
}
})

t.Run("ValidationError", func(t *testing.T) {
err := &ValidationError{Field: "Age", Message: "too low"}
expected := "dotenvgo: validation failed for field \"Age\": too low"
if err.Error() != expected {
t.Errorf("Expected %q, got %q", expected, err.Error())
}
})

t.Run("MultiError", func(t *testing.T) {
e1 := errors.New("error 1")
e2 := errors.New("error 2")
Expand Down
8 changes: 8 additions & 0 deletions examples/struct/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/godeh/dotenvgo"
)

type Database struct {
URL string `env:"URL" default:"postgres://localhost:5432/defaultdb"`
}

// Config defines your application configuration
type Config struct {
// Server settings
Expand All @@ -34,6 +38,8 @@ type Config struct {
AllowedOrigins []string `env:"ALLOWED_ORIGINS" default:"*"`

IDs []int `env:"IDS" default:"1,2,3,4,5"`

DB Database
}

func main() {
Expand All @@ -44,6 +50,7 @@ func main() {
os.Setenv("READ_TIMEOUT", "1m")
os.Setenv("ALLOWED_ORIGINS", "http://localhost:3000, https://myapp.com")
os.Setenv("IDS", "1, 2, 3")
os.Setenv("URL", "postgres://localhost:5432/prod")

// Load configuration
var cfg Config
Expand All @@ -65,4 +72,5 @@ func main() {
fmt.Printf("Max Connections: %d\n", cfg.MaxConnections)
fmt.Printf("Allowed Origins: %v\n", cfg.AllowedOrigins)
fmt.Printf("IDs: %v\n", cfg.IDs)
fmt.Printf("DB URL: %s\n", cfg.DB.URL)
}
3 changes: 3 additions & 0 deletions loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func TestLoad(t *testing.T) {
if cfg.Timeout != 30*time.Second {
t.Errorf("Expected Timeout 30s, got %v", cfg.Timeout)
}
if cfg.secret != "" {
t.Errorf("Expected unexported field to remain unset, got %q", cfg.secret)
}
})

t.Run("Env Overrides", func(t *testing.T) {
Expand Down
Loading