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

Possible to populate fields discretely for a slice of structs? #169

Open
jlucktay opened this issue Mar 4, 2020 · 4 comments
Open

Possible to populate fields discretely for a slice of structs? #169

jlucktay opened this issue Mar 4, 2020 · 4 comments

Comments

@jlucktay
Copy link

jlucktay commented Mar 4, 2020

Hi there, and thanks very much for envconfig, we love it!
One thing I'm not clear on whether or not is possible, and it has come up as a bit of a blocker for us, is say we have a slice of structs as one field in our base config struct, can we set different values on the fields of different structs in the slice?
For example:

type FirstLevelConfig struct {
  FieldOne []SecondLevelConfig
}

type SecondLevelConfig struct {
  FieldTwo string
}

Can we put something like PREFIX_FIELDONE_0_FIELDTWO="string on first struct" and PREFIX_FIELDONE_1_FIELDTWO="string on second struct" into the environment, call .Process("PREFIX", &FirstLevelConfig) and end up with two structs?

colega added a commit to colega/envconfig that referenced this issue Mar 19, 2020
Solves kelseyhightower#169

Enables population of a slice of structs, in the most expected way:

    MYAPP_SLICE_0_FOO=foo
    MYAPP_SLICE_0_BAR=bar
    MYAPP_SLICE_1_FOO=foo
    MYAPP_SLICE_1_BAR=bar

Would fill a slice of []struct{Foo, Bar string}

Usage printout also supports this slices, printing them as a single

    MYAPP_SLICE_[N]_FOO
    MYAPP_SLICE_[N]_BAR

Variables to be filled, which should be understood as slices with
numerical indexes.
colega added a commit to colega/envconfig that referenced this issue Mar 19, 2020
Solves kelseyhightower#169

Enables population of a slice of structs, in the most expected way:

    MYAPP_SLICE_0_FOO=foo
    MYAPP_SLICE_0_BAR=bar
    MYAPP_SLICE_1_FOO=foo
    MYAPP_SLICE_1_BAR=bar

Would fill a slice of []struct{Foo, Bar string}

Usage printout also supports this slices, printing them as a single

    MYAPP_SLICE_[N]_FOO
    MYAPP_SLICE_[N]_BAR

Variables to be filled, which should be understood as slices with
numerical indexes.
colega added a commit to colega/envconfig that referenced this issue Apr 27, 2020
Solves kelseyhightower#169

Enables population of a slice of structs, in the most expected way:

    MYAPP_SLICE_0_FOO=foo
    MYAPP_SLICE_0_BAR=bar
    MYAPP_SLICE_1_FOO=foo
    MYAPP_SLICE_1_BAR=bar

Would fill a slice of []struct{Foo, Bar string}

Usage printout also supports this slices, printing them as a single

    MYAPP_SLICE_[N]_FOO
    MYAPP_SLICE_[N]_BAR

Variables to be filled, which should be understood as slices with
numerical indexes.
colega added a commit to colega/envconfig that referenced this issue Apr 27, 2020
Solves kelseyhightower#169

Enables population of a slice of structs, in the most expected way:

    MYAPP_SLICE_0_FOO=foo
    MYAPP_SLICE_0_BAR=bar
    MYAPP_SLICE_1_FOO=foo
    MYAPP_SLICE_1_BAR=bar

Would fill a slice of []struct{Foo, Bar string}

Usage printout also supports this slices, printing them as a single

    MYAPP_SLICE_[N]_FOO
    MYAPP_SLICE_[N]_BAR

Variables to be filled, which should be understood as slices with
numerical indexes.
@MaerF0x0
Copy link

MaerF0x0 commented Jun 11, 2020

Could you implement Setter and do it yourself?

something like:

type cfg struct {
   Stuff StuffSlice
}

type StuffSlice []SecondLevelConfig

func (ss *StuffSlice) Set(data string) error {
    return json.Unmarshal([]byte(data), &ss) 
}

EDIT: This doesnt fit your desired env var format, but does allow slice of structs

@colega
Copy link

colega commented Jun 11, 2020

That doesn't work well with big structs and is not directly compatible with multiple env vars.

I solved this here: #170 and merged it into my fork: colega#1

@itayd
Copy link

itayd commented Feb 9, 2021

hi, would love to see #170 merged - is there any progress planned on that?

@mohammadne
Copy link

any progress ?

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

5 participants