Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: drop prefix option #42

Merged
merged 1 commit into from
Nov 10, 2023
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
21 changes: 1 addition & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ go get go-simpler.org/env
* Simple API
* Dependency-free
* Per-variable options: [required](#required), [expand](#expand)
* Global options: [source](#source), [prefix](#prefix), [slice separator](#slice-separator)
* Global options: [source](#source), [slice separator](#slice-separator)
* Auto-generated [usage message](#usage-message)

## 📋 Usage
Expand Down Expand Up @@ -185,25 +185,6 @@ fmt.Println(cfg.Port)
// Output: 8080
```

#### Prefix

It is a common practice to prefix app's environment variables with some string
(e.g., its name). Such a prefix can be set using the `WithPrefix` option:

```go
os.Setenv("APP_PORT", "8080")

var cfg struct {
Port int `env:"PORT"`
}
if err := env.Load(&cfg, env.WithPrefix("APP_")); err != nil {
fmt.Println(err)
}

fmt.Println(cfg.Port)
// Output: 8080
```

#### Slice separator

Space is the default separator when parsing slice values. It can be changed
Expand Down
10 changes: 1 addition & 9 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ func WithSource(src Source) Option {
return func(l *loader) { l.source = src }
}

// WithPrefix configures [Load] to automatically add the provided prefix to each environment variable.
// By default, no prefix is configured.
func WithPrefix(prefix string) Option {
return func(l *loader) { l.prefix = prefix }
}

// WithSliceSeparator configures [Load] to use the provided separator when parsing slice values.
// The default separator is space.
func WithSliceSeparator(sep string) Option {
Expand All @@ -82,14 +76,12 @@ func (e *NotSetError) Error() string {

type loader struct {
source Source
prefix string
sliceSep string
}

func newLoader(opts []Option) *loader {
l := loader{
source: OS,
prefix: "",
sliceSep: " ",
}
for _, opt := range opts {
Expand Down Expand Up @@ -189,7 +181,7 @@ func (l *loader) parseVars(v reflect.Value) []Var {
}

vars = append(vars, Var{
Name: l.prefix + name,
Name: name,
Type: field.Type(),
Desc: sf.Tag.Get("desc"),
Default: defValue,
Expand Down
14 changes: 0 additions & 14 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,6 @@ func ExampleWithSource() {
// Output: 8080
}

func ExampleWithPrefix() {
os.Setenv("APP_PORT", "8080")

var cfg struct {
Port int `env:"PORT"`
}
if err := env.Load(&cfg, env.WithPrefix("APP_")); err != nil {
fmt.Println(err)
}

fmt.Println(cfg.Port)
// Output: 8080
}

func ExampleWithSliceSeparator() {
os.Setenv("PORTS", "8080;8081;8082")

Expand Down
2 changes: 1 addition & 1 deletion usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// Var contains the information about the environment variable parsed from a struct field.
type Var struct {
Name string // The full name of the variable, including the prefix.
Name string // The full name of the variable.
Type reflect.Type // The type of the variable.
Desc string // The description parsed from the `desc` tag (if exists).
Default string // The default value of the variable. Empty, if the variable is required.
Expand Down