Skip to content

UseFieldNameByDefault doesn't support acronyms #295

@Baselkh

Description

@Baselkh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions