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

Parse empty string as an empty slice of numbers #119

Merged
merged 1 commit into from May 24, 2019
Merged

Parse empty string as an empty slice of numbers #119

merged 1 commit into from May 24, 2019

Conversation

tetafro
Copy link
Contributor

@tetafro tetafro commented May 28, 2018

I've added code to parse empty string as an empty slice of numbers.

Currently the following code gives an error if SLICE variable is empty, which is quite unexpected, I think.

type conf struct {
	Slice []int
}

func main() {
	var c conf
	envconfig.MustProcess("", &c)
	fmt.Println(c.Slice)
}
panic: envconfig.Process: assigning SLICE to Slice: converting '' to type []int. details: strconv.ParseInt: parsing "": invalid syntax

@eloff
Copy link

eloff commented Sep 29, 2018

Good lord this has been open since the end of May. Please merge this.

@eloff
Copy link

eloff commented Oct 20, 2018

You can workaround this problem by looking for fields set to []string{""}, and setting them to nil after calling envconfig.Process:

if len(Conf.CORSOrigins) == 1 && Conf.CORSOrigins[0] == "" {
	conf.CORSOrigins = nil
}

@teepark
Copy link
Collaborator

teepark commented May 24, 2019

thanks for the fix, @tetafro

@teepark teepark merged commit a52ba04 into kelseyhightower:master May 24, 2019
imiric pushed a commit to grafana/k6 that referenced this pull request Oct 24, 2019
We were using a very old version (Jan 2017), and the new one includes
fixes like kelseyhightower/envconfig#119 ,
which would avoid workarounds in PRs like #1143.
imiric pushed a commit to grafana/k6 that referenced this pull request Oct 24, 2019
We were using a very old version (Jan 2017), and the new one includes
fixes like kelseyhightower/envconfig#119 ,
which would avoid workarounds in PRs like #1143.
imiric pushed a commit to grafana/k6 that referenced this pull request Oct 25, 2019
We were using a very old version (Jan 2017), and the new one includes
fixes like kelseyhightower/envconfig#119 ,
which would avoid workarounds in PRs like #1143.
srguglielmo pushed a commit to srguglielmo/k6 that referenced this pull request Nov 3, 2019
We were using a very old version (Jan 2017), and the new one includes
fixes like kelseyhightower/envconfig#119 ,
which would avoid workarounds in PRs like grafana#1143.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants