Envopts#33
Conversation
|
so are Because from what I can tell, this helps with the ability to deal with config vars that are styled differently, and does it through an array of processor functions |
|
It's a good idea to have to those options. But what about adding this functionality to the others backends ? |
|
Hello. I think that those options must be separate for each backend (different types for different backends). That's why I added this type inside env backend package. |
|
Why should we have a different type for each backend ? This is a common use case so I think that we can have a common type . |
|
Hey, guys. |
…t found for old logic compatibility
rogpeppe
left a comment
There was a problem hiding this comment.
Thanks for suggesting this. It's a nice idea, but the interaction between opts and the existing logic becomes quite involved, and also, when the options pattern is used, usually, adding extra of the same option is idempotent, but that's not the case here, as env.NewBackend(env.WithPrefix("X"), env.WithPrefix("X")) will add two "X" prefixes, not just one.
I'm wondering whether this PR is worth it. If you want your own custom key transformation logic, it seems like it would be pretty easy to roll your own backend with backend.Func.
We'll consider our options here.
| } | ||
|
|
||
| // WithPrefix adds preffix for searching env variable | ||
| func WithPrefix(preffix string) opt { |
| }) | ||
| } | ||
|
|
||
| // WithPrefix adds preffix for searching env variable |
There was a problem hiding this comment.
// WithPrefix is a backend option that causes the given prefix to be added to all
// configuration names before looking them up with os.Getenv.
?
|
|
||
| // WithPrefix adds preffix for searching env variable | ||
| func WithPrefix(preffix string) opt { | ||
| if !strings.HasSuffix(preffix, "_") { |
There was a problem hiding this comment.
I don't think it's worth bothering with this. It makes the logic more complex and harder to explain, and it's quite possible someone might want to add a prefix without any intervening underscore.
| // If the key is not found, this backend tries again by turning any kebabcase key to snakecase and | ||
| // lowercase letters to uppercase. | ||
| func NewBackend() backend.Backend { | ||
| func NewBackend(fns ...opt) backend.Backend { |
There was a problem hiding this comment.
Technically I'm afraid this is a backwardly incompatible change, as someone might have assigned the function to a variable of type func() backend.Backend.
Also, I don't think it's a good idea in general to have unexported functions that use unexported types (it makes the godoc hard to understand and means you can't declare variables of those types).
|
|
||
| ```sh | ||
| go get -u github.com/heetch/confita | ||
| go get -u github.com/karantin2020/confita |
There was a problem hiding this comment.
This looks like a leftover from another fork.
| func NewBackend(fns ...opt) backend.Backend { | ||
| return backend.Func("env", func(ctx context.Context, key string) ([]byte, error) { | ||
| val, ok := os.LookupEnv(key) | ||
| key = strings.Replace(key, "-", "_", -1) |
There was a problem hiding this comment.
This is a backwardly incompatible change. The original logic would succeed if an environment variable exists with exactly the same name, but it won't any longer (I'm surprised the tests pass actually - we should fix that).
This set of commits add configuration functionality to anv backend.
Func WithPreffix allows to add preffix to env var.
Func ToUpper uppercases vars