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.
MIT License
2.56k stars 146 forks source link

Can't unmarshall env variable with multiple words #263

Closed efiShtain closed 7 months ago

efiShtain commented 7 months ago

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):

Additional context Add any other context about the problem here.