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

Env prefixes doesn't work with array #298

Open
dongjiang1989 opened this issue Mar 26, 2024 · 4 comments
Open

Env prefixes doesn't work with array #298

dongjiang1989 opened this issue Mar 26, 2024 · 4 comments

Comments

@dongjiang1989
Copy link

ref:

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"

	"github.com/caarlos0/env/v10"
)

type Test struct {
	Str string `env:"TEST"`
}
type ComplexConfig struct {
	Foos  []Test `envPrefix:"FOO_"`
	Clean Test   `envPrefix:"BAR_"`
}

func main() {
	os.Setenv("FOO_TEST", "kek")
	os.Setenv("FOO_0_TEST", "kek0")
	os.Setenv("FOO_1_TEST", "kek1")
	os.Setenv("BAR_TEST", "lel")

	cfg := ComplexConfig{}
	err := env.Parse(&cfg)
	if err != nil {
		log.Fatal(err)
	}
	confBytes, _ := json.Marshal(cfg)
	fmt.Printf("%s", confBytes)
}

output:

{"Foos":null,"Clean":{"Str":"lel"}}
@akutuev
Copy link
Contributor

akutuev commented Mar 26, 2024

@caarlos0 let me know if you'd like to make this change, happy to help

@caarlos0
Copy link
Owner

on the top of my head I can't think of a reason why not... if you wanna try pls go for it 🙏

@dongjiang1989
Copy link
Author

@akutuev Will you add pr for this feat? I can help

@akutuev
Copy link
Contributor

akutuev commented Mar 31, 2024

@akutuev Will you add pr for this feat? I can help

Hi, no worries, I'm planning to take a look very soon, thanks

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

No branches or pull requests

3 participants