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

Can't unmarshall env variable with multiple words

efiShtain opened this issue · comments

Describe the bug
Can't get env variable unmarshalling to work if the env variable has multiple words in it

This is not matched correctly with the following example

MYVAR_GOOGLE_IS_ENABLED: "true"

To Reproduce

package main

import (
	"fmt"
	"strings"

	"github.com/knadh/koanf/providers/env"
	"github.com/knadh/koanf/v2"
)

type Config struct {
	Google struct {
		IsEnabled bool `koanf:"is.enabled"`
	} `koanf:"google"`
}

func main() {
	k := koanf.New(".")
	prefix := "MYVAR_"
	if err := k.Load(env.Provider(prefix, ".", func(s string) string {
		return strings.Replace(strings.ToLower(
			strings.TrimPrefix(s, prefix)), "_", ".", -1)
	}), nil); err != nil {
		panic(err)
	}

	var cfg Config
	if err := k.Unmarshal("", &cfg); err != nil {
		fmt.Printf("error unmarshalling config: %v\n", err)
		return
	}

	fmt.Println("Google is enabled:", cfg.Google.IsEnabled)
}

Result is false

Expected behavior
cfg.Google.IsEnabled should be true

Please provide the following information):

  • OS: [e.g. linux/osx/windows]
  • Koanf Version [e.g. v1.0.0]

Additional context
Add any other context about the problem here.