knadh / koanf

Simple, extremely lightweight, extensible, configuration management library for Go. Support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

nomikz opened this issue · comments

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

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

@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.