-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New encoding layer #1869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New encoding layer #1869
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
158a721
feat(encoding): expose new encoding layer
sagikazarmark 1c55e0d
feat(encoding): return an error when no codec is found
sagikazarmark 628cae5
feat(encoding): add default codec registry
sagikazarmark 545f25d
feat(encoding): drop not found sentinel errors for now
sagikazarmark 40dcd26
feat(encoding): make format case-insensitive
sagikazarmark d89dcbd
test(encoding): add tests for codec registry implementation
sagikazarmark 30e1a60
refactor(encoding): drop old codec registries
sagikazarmark 6429344
refactor(encoding): rename new codec registries
sagikazarmark c2ab99b
docs(encoding): add docs to codec registry
sagikazarmark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| package viper | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/spf13/viper/internal/encoding/dotenv" | ||
| "github.com/spf13/viper/internal/encoding/hcl" | ||
| "github.com/spf13/viper/internal/encoding/ini" | ||
| "github.com/spf13/viper/internal/encoding/javaproperties" | ||
| "github.com/spf13/viper/internal/encoding/json" | ||
| "github.com/spf13/viper/internal/encoding/toml" | ||
| "github.com/spf13/viper/internal/encoding/yaml" | ||
| ) | ||
|
|
||
| // Encoder encodes Viper's internal data structures into a byte representation. | ||
| // It's primarily used for encoding a map[string]any into a file format. | ||
| type Encoder interface { | ||
| Encode(v map[string]any) ([]byte, error) | ||
| } | ||
|
|
||
| // Decoder decodes the contents of a byte slice into Viper's internal data structures. | ||
| // It's primarily used for decoding contents of a file into a map[string]any. | ||
| type Decoder interface { | ||
| Decode(b []byte, v map[string]any) error | ||
| } | ||
|
|
||
| // Codec combines [Encoder] and [Decoder] interfaces. | ||
| type Codec interface { | ||
| Encoder | ||
| Decoder | ||
| } | ||
|
|
||
| // TODO: consider adding specific errors for not found scenarios | ||
|
|
||
| // EncoderRegistry returns an [Encoder] for a given format. | ||
| // | ||
| // Format is case-insensitive. | ||
| // | ||
| // [EncoderRegistry] returns an error if no [Encoder] is registered for the format. | ||
| type EncoderRegistry interface { | ||
| Encoder(format string) (Encoder, error) | ||
| } | ||
|
|
||
| // DecoderRegistry returns an [Decoder] for a given format. | ||
| // | ||
| // Format is case-insensitive. | ||
| // | ||
| // [DecoderRegistry] returns an error if no [Decoder] is registered for the format. | ||
| type DecoderRegistry interface { | ||
| Decoder(format string) (Decoder, error) | ||
| } | ||
|
|
||
| // [CodecRegistry] combines [EncoderRegistry] and [DecoderRegistry] interfaces. | ||
| type CodecRegistry interface { | ||
| EncoderRegistry | ||
| DecoderRegistry | ||
| } | ||
|
|
||
| // WithEncoderRegistry sets a custom [EncoderRegistry]. | ||
| func WithEncoderRegistry(r EncoderRegistry) Option { | ||
| return optionFunc(func(v *Viper) { | ||
| v.encoderRegistry = r | ||
| }) | ||
| } | ||
|
|
||
| // WithDecoderRegistry sets a custom [DecoderRegistry]. | ||
| func WithDecoderRegistry(r DecoderRegistry) Option { | ||
| return optionFunc(func(v *Viper) { | ||
| v.decoderRegistry = r | ||
| }) | ||
| } | ||
|
|
||
| // WithCodecRegistry sets a custom [EncoderRegistry] and [DecoderRegistry]. | ||
| func WithCodecRegistry(r CodecRegistry) Option { | ||
| return optionFunc(func(v *Viper) { | ||
| v.encoderRegistry = r | ||
| v.decoderRegistry = r | ||
| }) | ||
| } | ||
|
|
||
| type codecRegistry struct { | ||
| v *Viper | ||
| } | ||
|
|
||
| func (r codecRegistry) Encoder(format string) (Encoder, error) { | ||
| encoder, ok := r.codec(format) | ||
| if !ok { | ||
| return nil, errors.New("encoder not found for this format") | ||
| } | ||
|
|
||
| return encoder, nil | ||
| } | ||
|
|
||
| func (r codecRegistry) Decoder(format string) (Decoder, error) { | ||
| decoder, ok := r.codec(format) | ||
| if !ok { | ||
| return nil, errors.New("decoder not found for this format") | ||
| } | ||
|
|
||
| return decoder, nil | ||
| } | ||
|
|
||
| func (r codecRegistry) codec(format string) (Codec, bool) { | ||
| switch strings.ToLower(format) { | ||
| case "yaml", "yml": | ||
| return yaml.Codec{}, true | ||
|
|
||
| case "json": | ||
| return json.Codec{}, true | ||
|
|
||
| case "toml": | ||
| return toml.Codec{}, true | ||
|
|
||
| case "hcl", "tfvars": | ||
| return hcl.Codec{}, true | ||
|
|
||
| case "ini": | ||
| return ini.Codec{ | ||
| KeyDelimiter: r.v.keyDelim, | ||
| LoadOptions: r.v.iniLoadOptions, | ||
| }, true | ||
|
|
||
| case "properties", "props", "prop": // Note: This breaks writing a properties file. | ||
| return &javaproperties.Codec{ | ||
| KeyDelimiter: v.keyDelim, | ||
| }, true | ||
|
|
||
| case "dotenv", "env": | ||
| return &dotenv.Codec{}, true | ||
| } | ||
|
|
||
| return nil, false | ||
| } | ||
|
|
||
| // DefaultCodecRegistry is a simple implementation of [CodecRegistry] that allows registering custom [Codec]s. | ||
| type DefaultCodecRegistry struct { | ||
| codecs map[string]Codec | ||
|
|
||
| mu sync.RWMutex | ||
| once sync.Once | ||
| } | ||
|
|
||
| // NewCodecRegistry returns a new [CodecRegistry], ready to accept custom [Codec]s. | ||
| func NewCodecRegistry() *DefaultCodecRegistry { | ||
| r := &DefaultCodecRegistry{} | ||
|
|
||
| r.init() | ||
|
|
||
| return r | ||
| } | ||
|
|
||
| func (r *DefaultCodecRegistry) init() { | ||
| r.once.Do(func() { | ||
| r.codecs = map[string]Codec{} | ||
| }) | ||
| } | ||
|
|
||
| // RegisterCodec registers a custom [Codec]. | ||
| // | ||
| // Format is case-insensitive. | ||
| func (r *DefaultCodecRegistry) RegisterCodec(format string, codec Codec) error { | ||
| r.init() | ||
|
|
||
| r.mu.Lock() | ||
| defer r.mu.Unlock() | ||
|
|
||
| r.codecs[strings.ToLower(format)] = codec | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Encoder implements the [EncoderRegistry] interface. | ||
| // | ||
| // Format is case-insensitive. | ||
| func (r *DefaultCodecRegistry) Encoder(format string) (Encoder, error) { | ||
| encoder, ok := r.codec(format) | ||
| if !ok { | ||
| return nil, errors.New("encoder not found for this format") | ||
| } | ||
|
|
||
| return encoder, nil | ||
| } | ||
|
|
||
| // Decoder implements the [DecoderRegistry] interface. | ||
| // | ||
| // Format is case-insensitive. | ||
| func (r *DefaultCodecRegistry) Decoder(format string) (Decoder, error) { | ||
| decoder, ok := r.codec(format) | ||
| if !ok { | ||
| return nil, errors.New("decoder not found for this format") | ||
| } | ||
|
|
||
| return decoder, nil | ||
| } | ||
|
|
||
| func (r *DefaultCodecRegistry) codec(format string) (Codec, bool) { | ||
| r.mu.Lock() | ||
| defer r.mu.Unlock() | ||
|
|
||
| format = strings.ToLower(format) | ||
|
|
||
| if r.codecs != nil { | ||
| codec, ok := r.codecs[format] | ||
| if ok { | ||
| return codec, true | ||
| } | ||
| } | ||
|
|
||
| switch format { | ||
| case "yaml", "yml": | ||
| return yaml.Codec{}, true | ||
|
|
||
| case "json": | ||
| return json.Codec{}, true | ||
|
|
||
| case "toml": | ||
| return toml.Codec{}, true | ||
|
|
||
| case "dotenv", "env": | ||
| return &dotenv.Codec{}, true | ||
| } | ||
|
|
||
| return nil, false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package viper | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type codec struct{} | ||
|
|
||
| func (codec) Encode(_ map[string]any) ([]byte, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (codec) Decode(_ []byte, _ map[string]any) error { | ||
| return nil | ||
| } | ||
|
|
||
| func TestDefaultCodecRegistry(t *testing.T) { | ||
| t.Run("OK", func(t *testing.T) { | ||
| registry := NewCodecRegistry() | ||
|
|
||
| c := codec{} | ||
|
|
||
| err := registry.RegisterCodec("myformat", c) | ||
| require.NoError(t, err) | ||
|
|
||
| encoder, err := registry.Encoder("myformat") | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, c, encoder) | ||
|
|
||
| decoder, err := registry.Decoder("myformat") | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, c, decoder) | ||
| }) | ||
|
|
||
| t.Run("CodecNotFound", func(t *testing.T) { | ||
| registry := NewCodecRegistry() | ||
|
|
||
| _, err := registry.Encoder("myformat") | ||
| require.Error(t, err) | ||
|
|
||
| _, err = registry.Decoder("myformat") | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("FormatIsCaseInsensitive", func(t *testing.T) { | ||
| registry := NewCodecRegistry() | ||
|
|
||
| c := codec{} | ||
|
|
||
| err := registry.RegisterCodec("MYFORMAT", c) | ||
| require.NoError(t, err) | ||
|
|
||
| { | ||
| encoder, err := registry.Encoder("myformat") | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, c, encoder) | ||
| } | ||
|
|
||
| { | ||
| encoder, err := registry.Encoder("MYFORMAT") | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, c, encoder) | ||
| } | ||
|
|
||
| { | ||
| decoder, err := registry.Decoder("myformat") | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, c, decoder) | ||
| } | ||
|
|
||
| { | ||
| decoder, err := registry.Decoder("MYFORMAT") | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, c, decoder) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("OverrideDefault", func(t *testing.T) { | ||
| registry := NewCodecRegistry() | ||
|
|
||
| c := codec{} | ||
|
|
||
| err := registry.RegisterCodec("yaml", c) | ||
| require.NoError(t, err) | ||
|
|
||
| encoder, err := registry.Encoder("yaml") | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, c, encoder) | ||
|
|
||
| decoder, err := registry.Decoder("yaml") | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, c, decoder) | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.