|
| 1 | +# Update Log |
| 2 | + |
| 3 | +**This document details any major updates required to use new features or improvements in Viper.** |
| 4 | + |
| 5 | +## v1.20.x |
| 6 | + |
| 7 | +### New file searching API |
| 8 | + |
| 9 | +Viper now includes a new file searching API that allows users to customize how Viper looks for config files. |
| 10 | + |
| 11 | +Viper accepts a custom [`Finder`](https://pkg.go.dev/github.com/spf13/viper#Finder) interface implementation: |
| 12 | + |
| 13 | +```go |
| 14 | +// Finder looks for files and directories in an [afero.Fs] filesystem. |
| 15 | +type Finder interface { |
| 16 | + Find(fsys afero.Fs) ([]string, error) |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +It is supposed to return a list of paths to config files. |
| 21 | + |
| 22 | +The default implementation uses [github.com/sagikazarmark/locafero](https://github.com/sagikazarmark/locafero) under the hood. |
| 23 | + |
| 24 | +You can supply your own implementation using `WithFinder`: |
| 25 | + |
| 26 | +```go |
| 27 | +v := viper.NewWithOptions( |
| 28 | + viper.WithFinder(&MyFinder{}), |
| 29 | +) |
| 30 | +``` |
| 31 | + |
| 32 | +For more information, check out the [Finder examples](https://pkg.go.dev/github.com/spf13/viper#Finder) |
| 33 | +and the [documentation](https://pkg.go.dev/github.com/sagikazarmark/locafero) for the locafero package. |
| 34 | + |
| 35 | +### New encoding API |
| 36 | + |
| 37 | +Viper now allows customizing the encoding layer by providing an API for encoding and decoding configuration data: |
| 38 | + |
| 39 | +```go |
| 40 | +// Encoder encodes Viper's internal data structures into a byte representation. |
| 41 | +// It's primarily used for encoding a map[string]any into a file format. |
| 42 | +type Encoder interface { |
| 43 | + Encode(v map[string]any) ([]byte, error) |
| 44 | +} |
| 45 | + |
| 46 | +// Decoder decodes the contents of a byte slice into Viper's internal data structures. |
| 47 | +// It's primarily used for decoding contents of a file into a map[string]any. |
| 48 | +type Decoder interface { |
| 49 | + Decode(b []byte, v map[string]any) error |
| 50 | +} |
| 51 | + |
| 52 | +// Codec combines [Encoder] and [Decoder] interfaces. |
| 53 | +type Codec interface { |
| 54 | + Encoder |
| 55 | + Decoder |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +By default, Viper includes the following codecs: |
| 60 | + |
| 61 | +- JSON |
| 62 | +- TOML |
| 63 | +- YAML |
| 64 | +- Dotenv |
| 65 | + |
| 66 | +The rest of the codecs are moved to [github.com/go-viper/encoding](https://github.com/go-viper/encoding) |
| 67 | + |
| 68 | +Customizing the encoding layer is possible by providing a custom registry of codecs: |
| 69 | + |
| 70 | +- [Encoder](https://pkg.go.dev/github.com/spf13/viper#Encoder) -> [EncoderRegistry](https://pkg.go.dev/github.com/spf13/viper#EncoderRegistry) |
| 71 | +- [Decoder](https://pkg.go.dev/github.com/spf13/viper#Decoder) -> [DecoderRegistry](https://pkg.go.dev/github.com/spf13/viper#DecoderRegistry) |
| 72 | +- [Codec](https://pkg.go.dev/github.com/spf13/viper#Codec) -> [CodecRegistry](https://pkg.go.dev/github.com/spf13/viper#CodecRegistry) |
| 73 | + |
| 74 | +You can supply the registry of codecs to Viper using the appropriate `With*Registry` function: |
| 75 | + |
| 76 | +```go |
| 77 | +codecRegistry := viper.NewCodecRegistry() |
| 78 | + |
| 79 | +codecRegistry.RegisterCodec("myformat", &MyCodec{}) |
| 80 | + |
| 81 | +v := viper.NewWithOptions( |
| 82 | + viper.WithCodecRegistry(codecRegistry), |
| 83 | +) |
| 84 | +``` |
| 85 | + |
| 86 | +### BREAKING: HCL, Java properties, INI removed from core |
| 87 | + |
| 88 | +In order to reduce third-party dependencies, Viper dropped support for the following formats from the core: |
| 89 | + |
| 90 | +- HCL |
| 91 | +- Java properties |
| 92 | +- INI |
| 93 | + |
| 94 | +You can still use these formats though by importing them from [github.com/go-viper/encoding](https://github.com/go-viper/encoding): |
| 95 | + |
| 96 | +```go |
| 97 | +import ( |
| 98 | + "github.com/go-viper/encoding/hcl" |
| 99 | + "github.com/go-viper/encoding/javaproperties" |
| 100 | + "github.com/go-viper/encoding/ini" |
| 101 | +) |
| 102 | + |
| 103 | +codecRegistry := viper.NewCodecRegistry() |
| 104 | + |
| 105 | +{ |
| 106 | + codec := hcl.Codec{} |
| 107 | + |
| 108 | + codecRegistry.RegisterCodec("hcl", codec) |
| 109 | + codecRegistry.RegisterCodec("tfvars", codec) |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +{ |
| 114 | + codec := &javaproperties.Codec{} |
| 115 | + |
| 116 | + codecRegistry.RegisterCodec("properties", codec) |
| 117 | + codecRegistry.RegisterCodec("props", codec) |
| 118 | + codecRegistry.RegisterCodec("prop", codec) |
| 119 | +} |
| 120 | + |
| 121 | +codecRegistry.RegisterCodec("ini", ini.Codec{}) |
| 122 | + |
| 123 | +v := viper.NewWithOptions( |
| 124 | + viper.WithCodecRegistry(codecRegistry), |
| 125 | +) |
| 126 | +``` |
0 commit comments