ilyakaznacheev / cleanenv

✨Clean and minimalistic environment configuration reader for Golang
MIT License
1.67k stars 116 forks source link

Didn't skip unexported field #105

Closed saddit closed 1 year ago

saddit commented 2 years ago

@ilyakaznacheev

panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

goroutine 1 [running]:
reflect.valueInterface({0x95f8a0?, 0xc00055c248?, 0xabea8f?}, 0xa?)
        E:/software/dev_core/go1.18.1/src/reflect/value.go:1441 +0xd8
reflect.Value.Interface(...)
        E:/software/dev_core/go1.18.1/src/reflect/value.go:1430
github.com/ilyakaznacheev/cleanenv.readStructMetadata({0x9c7900?, 0xc00055c1c0?})

        E:/text/code-project/GolangProject/pkg/mod/github.com/ilyakaznacheev/cleanenv@v1.3.0/cleanenv.go:274 +0x771
github.com/ilyakaznacheev/cleanenv.readEnvVars({0x9c7900, 0xc00055c1c0}, 0x0)

        E:/text/code-project/GolangProject/pkg/mod/github.com/ilyakaznacheev/cleanenv@v1.3.0/cleanenv.go:333 +0x45
github.com/ilyakaznacheev/cleanenv.ReadConfig({0xace493?, 0xc00041f9a8?}, {0x9c7900, 0xc00055c1c0})

        E:/text/code-project/GolangProject/pkg/mod/github.com/ilyakaznacheev/cleanenv@v1.3.0/cleanenv.go:95 +0x48

It doesn't skip unexported field

Originally posted by @838239178 in https://github.com/ilyakaznacheev/cleanenv/issues/68#issuecomment-1279943050

Example

package main

import (
    "github.com/ilyakaznacheev/cleanenv"
)

type Children struct {
    name string `yaml:"name" env:"name"`
}

type Config struct {
    private Children `yaml:"private" env-prefix:"private"`
    Public string `yaml:"public" env:"public"`
}

func main()  {
    var conf Config
    if err := cleanenv.ReadConfig("config.yml", &conf); err != nil {
        panic(err)
    }
}
saddit commented 2 years ago

YAML

public: abc
saddit commented 2 years ago

I suggest to add '-' as ignore tag

saddit commented 2 years ago

This function doesn't check whether field can interface or can't

// readStructMetadata reads structure metadata (types, tags, etc.)
func readStructMetadata(cfgRoot interface{}) ([]structMeta, error)
ilyakaznacheev commented 2 years ago

Thanks, I'll check