Skip to content

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

@nomikz

Description

@nomikz

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions