caarlos0 / env

A simple, zero-dependencies library to parse environment variables into structs

Home Page:https://pkg.go.dev/github.com/caarlos0/env/v11

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Env prefixes doesn't work with array

dongjiang1989 opened this issue · comments

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"}}

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

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

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

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

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

As I needed this feature too, I created a merge request to handle it. I will be grateful if you review it.

#312

The PR has fixed it. When can we expect a release?

just pushed v11.2.0

thanks everyone!