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

Non-struct pointer values cannot be used #235

Closed
mrsimonemms opened this issue Aug 31, 2022 · 4 comments
Closed

Non-struct pointer values cannot be used #235

mrsimonemms opened this issue Aug 31, 2022 · 4 comments
Labels

Comments

@mrsimonemms
Copy link

When using a non-struct pointer value, we cannot pass in existing data.

package main

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

	"github.com/caarlos0/env/v6"
	"k8s.io/utils/pointer"
)

type Test struct {
//	Bool *bool `env:"TEST"`
	Str *string `env:"TEST"`
}
type ComplexConfig struct {
	Foo Test `envPrefix:"FOO_"`
	Bar Test `envPrefix:"BAR_"`
	D   Test `envPrefix:"D_"`
}

func main() {
	os.Setenv("FOO_TEST", "1")
	os.Setenv("BAR_TEST", "0")

	cfg := ComplexConfig{
		D: Test{
			Str: pointer.String("hello"),
//			Bool: pointer.Bool(true),
		},
	}
	err := env.Parse(&cfg)
	if err != nil {
		log.Fatal(err)
	}
	confBytes, _ := json.Marshal(cfg)
	fmt.Printf("%s", confBytes)
}

Result

env: env: expected a pointer to a Struct

It works fine if the cfg variable had nothing set for D (ie, cfg := ComplexConfig{}):

{"Foo":{"Str":"1"},"Bar":{"Str":"0"},"D":{"Str":null}}

The use-case for this is to use it with our Installer to fill in the configuration

@caarlos0 caarlos0 added the bug label Aug 31, 2022
@akutuev
Copy link
Contributor

akutuev commented Sep 1, 2022

@caarlos0 are you going to take a look or can I try to investigate?

@caarlos0
Copy link
Owner

caarlos0 commented Sep 1, 2022

feel free to try, I don't know when I'll have the time to look into it

@caarlos0
Copy link
Owner

fixed in #236

@mrsimonemms
Copy link
Author

Thanks @caarlos0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants