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

fix: skip fields without env key in onSet #266

Merged
merged 1 commit into from
May 12, 2023

Conversation

aymanbagabas
Copy link
Contributor

Options.OnSet gets called on all fields, even the ones that don't have an env key defined. This simply ignore those.

Example: https://go.dev/play/p/U0dET0zgYto

package main

import (
	"fmt"
	"log"

	"github.com/caarlos0/env/v8"
)

type User struct {
	Name     string `env:"NAME"`
	Password string `env:"PASSWORD"`
}

type Config struct {
	User     User   `envPrefix:"USER_"` // this shouldn't be passed to OnSet
	Username string `env:"USERNAME" envDefault:"admin"`
	Password string `env:"PASSWORD"`
}

func main() {
	cfg := &Config{}
	opts := env.Options{
		Prefix: "ENVENV_",
		OnSet: func(tag string, value interface{}, isDefault bool) {
			fmt.Printf("Set %s to %v (default? %v)\n", tag, value, isDefault)
		},
	}

	// Load env vars.
	if err := env.ParseWithOptions(cfg, opts); err != nil {
		log.Fatal(err)
	}

	// Print the loaded data.
	fmt.Printf("%+v\n", cfg)
}

Produces:

Set ENVENV_ to  (default? false)
Set ENVENV_USER_NAME to  (default? false)
Set ENVENV_USER_PASSWORD to  (default? false)
Set ENVENV_USERNAME to admin (default? true)
Set ENVENV_PASSWORD to  (default? false)
&{User:{Name: Password:} Username:admin Password:}

Notice the first line Set ENVENV_ to (default? false) shouldn't really be there

Options.OnSet gets called on _all_ fields, even the ones that _don't_
have an `env` key defined. This simply ignore those.

Signed-off-by: Ayman Bagabas <ayman.bagabas@gmail.com>
@codecov
Copy link

codecov bot commented May 12, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (3f7d8cf) 100.00% compared to head (1a3ea0b) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #266   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines          426       427    +1     
=========================================
+ Hits           426       427    +1     
Impacted Files Coverage Δ
env.go 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@caarlos0 caarlos0 merged commit 0b4d8a6 into caarlos0:main May 12, 2023
12 checks passed
@caarlos0
Copy link
Owner

thanks!!

caarlos0 added a commit that referenced this pull request May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants