kelseyhightower/envconfig — pull request #3 by adrianduke (2014-07-09 22:41:41+00:00)
Title: [Feature] Custom Decoders

So I needed to implement an additional config provider for my app via env vars and stumbled across this short and sweet library, but I needed a little extra in the form of custom decoding of the env var value itself (think slices of data or even references to other env vars).

In my particular case I had a variable number of hashes I needed to get into my app as a single struct field and couldn't find a way to do it easily with the current code base so I extended it with the concept of custom decoders.

Effectively you register a decoder for a field name before you call `Process()`, you can register as many decoders as you like and you can also clear them out once you finish (as the storage mechanism is a package level var):

```Go
type Config struct {
Keys []string
}

envconfig.RegisterDecoder("Keys", func(value string, fieldValue, struc reflect.Value) error {
...logic...
})
defer envconfig.ClearDecoders()
```

In this case it is registered to the `Keys` struct field. You are given access to the current env var value, struct field and the struct being operated on. This should allow for maximum flexibility for a custom decoder.

The decoder func could be turned into a named type but I thought it was shorter for the caller to use an anonymous func. This can be changed though.

Thanks,
Adrian