Skip to content

Non-struct pointer values cannot be used #235

@mrsimonemms

Description

@mrsimonemms

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions