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

URL Pointers are no longer left nil #317

Closed
jgustie opened this issue Jun 6, 2024 · 1 comment · Fixed by #318
Closed

URL Pointers are no longer left nil #317

jgustie opened this issue Jun 6, 2024 · 1 comment · Fixed by #318
Labels

Comments

@jgustie
Copy link

jgustie commented Jun 6, 2024

Prior to 11.0.1 (related?) a URL pointer would be left nil if the environment variable was unset.

I tried writing a quick test to illustrate:

import (
	"net/url"
	"testing"

	"github.com/caarlos0/env/v11"
	"github.com/stretchr/testify/assert"
)

func TestParseEnv(t *testing.T) {
	type TestConfig struct {
		FooBar *url.URL `env:"FOOBAR"`
	}
	cases := []struct {
		desc        string
		environment map[string]string
		expected    *url.URL
	}{
		{
			desc:        "unset",
			environment: map[string]string{},
			expected:    nil,
		},
		{
			desc:        "empty",
			environment: map[string]string{"FOOBAR": ""},
			expected:    &url.URL{},
		},
		{
			desc:        "set",
			environment: map[string]string{"FOOBAR": "https://example.com/"},
			expected:    &url.URL{Scheme: "https", Host: "example.com", Path: "/"},
		},
	}
	for _, tc := range cases {
		t.Run(tc.desc, func(t *testing.T) {
			cfg := TestConfig{}
			err := env.ParseWithOptions(&cfg, env.Options{Environment: tc.environment})
			assert.NoError(t, err)
			assert.Equal(t, tc.expected, cfg.FooBar)
		})
	}
}

The unset test case passes in v11.0.0 but fails in v11.0.1.

The set test case always passes, and the empty test case never passes (I'm guessing there is plumbing that does not differentiate between "" and unset).

caarlos0 added a commit that referenced this issue Jun 19, 2024
as it would automatically initialize nil pointers.

this retracts that version, and gate this new feature behind an `init`
tag option.

closes #317
refs  #306
@caarlos0
Copy link
Owner

ah, sorry for that, good catch!

#318

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 a pull request may close this issue.

2 participants