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

koanf.Unmarshal doesn't work when loaded with env provider #275

Closed
nomikz opened this issue Feb 22, 2024 · 2 comments · Fixed by #278
Closed

koanf.Unmarshal doesn't work when loaded with env provider #275

nomikz opened this issue Feb 22, 2024 · 2 comments · Fixed by #278

Comments

@nomikz
Copy link

nomikz commented Feb 22, 2024

Description:

koanf.Unmarshal doesn't work when loaded by env provider

Is this expected behaviour?

Test: this test has no dependencies like files, envrionemnt variables.

package config

import (
	"os"
	"testing"

	"github.com/knadh/koanf/providers/env"
	koanf "github.com/knadh/koanf/v2"
	"github.com/stretchr/testify/require"
)

type Conf struct {
	Key string `koanf:"key"`
}

func TestTest(t *testing.T) {
	k := koanf.New("")
        
	// Set env variable
	require.NoError(t, os.Setenv("key", "value"))

	// Load environment variables
	err := k.Load(env.Provider("", "", nil), nil)
	require.NoError(t, err)
	// This passes successfully
	require.Equal(t, "value", k.String("key")) 

	var c Conf
	err = k.Unmarshal("", &c)
	require.NoError(t, err)

	// Here, it is expected that value is unmarshalled	
	require.Equal(t, "value", c.Key)
}

Expected behavior
Value must have been successfully unmarshalled into a struct.

OS: macOS Sonama Version 14.3.1
Koanf Version: v2.1.0
@nomikz nomikz added the bug Something isn't working label Feb 22, 2024
@wass3r
Copy link

wass3r commented Feb 24, 2024

in case you didn't know, it works when you provide a delimiter: env.Provider("", ".", nil)

@knadh knadh added needs-investigation and removed bug Something isn't working labels Feb 27, 2024
@nomikz
Copy link
Author

nomikz commented Feb 28, 2024

@wass3r thanks. But this is undefined behaviour. Although I didn't provide delimeter, accessing with k.String("key") in above example works fine. However, the field didn't get unmarshalled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants