Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseFieldNameByDefault doesn't support acronyms #295

Closed
Baselkh opened this issue Feb 22, 2024 · 1 comment · Fixed by #297
Closed

UseFieldNameByDefault doesn't support acronyms #295

Baselkh opened this issue Feb 22, 2024 · 1 comment · Fixed by #297

Comments

@Baselkh
Copy link

Baselkh commented Feb 22, 2024

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.

@Baselkh Baselkh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2024
@caarlos0
Copy link
Owner

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

@caarlos0 caarlos0 reopened this Mar 14, 2024
caarlos0 added a commit that referenced this issue Mar 16, 2024
closes #295

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
caarlos0 added a commit that referenced this issue Mar 16, 2024
closes #295

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
caarlos0 added a commit that referenced this issue Mar 16, 2024
closes #295

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
BorzdeG pushed a commit to BorzdeG/env that referenced this issue Aug 24, 2024
closes caarlos0#295

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants