caarlos0/env

`UseFieldNameByDefault` doesn't support acronyms

Closed this issue ยท 1 comments

First of all, this is a great feature ๐Ÿš€

Quick description of the issue

I have a field called HTTPVersion.
When using the UseFieldNameByDefault option, it translates it to H_TT_P_VERSION, when I expect it to be HTTP_VERSION.

A demo of the issue

basel@MM main1 % cat main.go 
package main

import (
        "fmt"
        "github.com/caarlos0/env/v10"
        "log"
)

type Config struct {
        HTTPVersion string
}

func main() {
        cfg := &Config{}
        opts := env.Options{
                UseFieldNameByDefault: true,
        }

        // Load env vars.
        if err := env.ParseWithOptions(cfg, opts); err != nil {
                log.Fatal(err)
        }

        // Print the loaded data.
        fmt.Printf("%+v\n", cfg)
}
basel@MM main1 % 
basel@MM main1 % go build -o app . && HTTP_VERSION=2.0 ./app
&{HTTPVersion:}
basel@MM main1 % go build -o app . && H_TT_P_VERSION=2.0 ./app
&{HTTPVersion:2.0}
basel@MM main1 % 

Suggestion

Looks like it comes down to toEnvName.
I've tried a quick, simple and efficient fix, which will preserve acronyms (i.e. it will translate it to HTTP_VERSION).
I'll be happy to open a PR in case it's relevant.

sorry for the lack of response, def a bug, feel free to PR it, otherwise I'll give it a try when I can

PS: probably worth looking into how the json package does it