2024-06-21 12:27:56 +00:00
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
2024-06-24 09:39:17 +00:00
|
|
|
"errors"
|
2024-06-24 09:37:05 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2024-06-21 12:27:56 +00:00
|
|
|
"github.com/spf13/viper/internal/encoding/dotenv"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2024-06-24 09:39:17 +00:00
|
|
|
// TODO: consider adding specific errors for not found scenarios
|
2024-06-22 10:43:44 +00:00
|
|
|
|
2024-06-21 12:27:56 +00:00
|
|
|
// EncoderRegistry returns an [Encoder] for a given format.
|
2024-06-22 10:43:44 +00:00
|
|
|
//
|
2024-06-24 09:41:23 +00:00
|
|
|
// Format is case-insensitive.
|
|
|
|
//
|
2024-06-24 09:39:17 +00:00
|
|
|
// [EncoderRegistry] returns an error if no [Encoder] is registered for the format.
|
2024-06-21 12:27:56 +00:00
|
|
|
type EncoderRegistry interface {
|
2024-06-22 10:43:44 +00:00
|
|
|
Encoder(format string) (Encoder, error)
|
2024-06-21 12:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecoderRegistry returns an [Decoder] for a given format.
|
2024-06-22 10:43:44 +00:00
|
|
|
//
|
2024-06-24 09:41:23 +00:00
|
|
|
// Format is case-insensitive.
|
|
|
|
//
|
2024-06-24 09:39:17 +00:00
|
|
|
// [DecoderRegistry] returns an error if no [Decoder] is registered for the format.
|
2024-06-21 12:27:56 +00:00
|
|
|
type DecoderRegistry interface {
|
2024-06-22 10:43:44 +00:00
|
|
|
Decoder(format string) (Decoder, error)
|
2024-06-21 12:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// [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) {
|
2024-06-24 09:53:35 +00:00
|
|
|
v.encoderRegistry = r
|
2024-06-21 12:27:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDecoderRegistry sets a custom [DecoderRegistry].
|
|
|
|
func WithDecoderRegistry(r DecoderRegistry) Option {
|
|
|
|
return optionFunc(func(v *Viper) {
|
2024-06-24 09:53:35 +00:00
|
|
|
v.decoderRegistry = r
|
2024-06-21 12:27:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithCodecRegistry sets a custom [EncoderRegistry] and [DecoderRegistry].
|
|
|
|
func WithCodecRegistry(r CodecRegistry) Option {
|
|
|
|
return optionFunc(func(v *Viper) {
|
2024-06-24 09:53:35 +00:00
|
|
|
v.encoderRegistry = r
|
|
|
|
v.decoderRegistry = r
|
2024-06-21 12:27:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type codecRegistry struct {
|
|
|
|
v *Viper
|
|
|
|
}
|
|
|
|
|
2024-06-22 10:43:44 +00:00
|
|
|
func (r codecRegistry) Encoder(format string) (Encoder, error) {
|
|
|
|
encoder, ok := r.codec(format)
|
|
|
|
if !ok {
|
2024-06-24 09:39:17 +00:00
|
|
|
return nil, errors.New("encoder not found for this format")
|
2024-06-22 10:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return encoder, nil
|
2024-06-21 12:27:56 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 10:43:44 +00:00
|
|
|
func (r codecRegistry) Decoder(format string) (Decoder, error) {
|
|
|
|
decoder, ok := r.codec(format)
|
|
|
|
if !ok {
|
2024-06-24 09:39:17 +00:00
|
|
|
return nil, errors.New("decoder not found for this format")
|
2024-06-22 10:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return decoder, nil
|
2024-06-21 12:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r codecRegistry) codec(format string) (Codec, bool) {
|
2024-06-24 09:41:23 +00:00
|
|
|
switch strings.ToLower(format) {
|
2024-06-21 12:27:56 +00:00
|
|
|
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
|
|
|
|
}
|
2024-06-24 09:37:05 +00:00
|
|
|
|
2024-06-24 09:53:35 +00:00
|
|
|
// DefaultCodecRegistry is a simple implementation of [CodecRegistry] that allows registering custom [Codec]s.
|
2024-06-24 09:37:05 +00:00
|
|
|
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].
|
2024-06-24 09:53:35 +00:00
|
|
|
//
|
|
|
|
// Format is case-insensitive.
|
2024-06-24 09:37:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-24 09:53:35 +00:00
|
|
|
// Encoder implements the [EncoderRegistry] interface.
|
|
|
|
//
|
|
|
|
// Format is case-insensitive.
|
2024-06-24 09:37:05 +00:00
|
|
|
func (r *DefaultCodecRegistry) Encoder(format string) (Encoder, error) {
|
|
|
|
encoder, ok := r.codec(format)
|
|
|
|
if !ok {
|
2024-06-24 09:39:17 +00:00
|
|
|
return nil, errors.New("encoder not found for this format")
|
2024-06-24 09:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return encoder, nil
|
|
|
|
}
|
|
|
|
|
2024-06-24 09:53:35 +00:00
|
|
|
// Decoder implements the [DecoderRegistry] interface.
|
|
|
|
//
|
|
|
|
// Format is case-insensitive.
|
2024-06-24 09:37:05 +00:00
|
|
|
func (r *DefaultCodecRegistry) Decoder(format string) (Decoder, error) {
|
|
|
|
decoder, ok := r.codec(format)
|
|
|
|
if !ok {
|
2024-06-24 09:39:17 +00:00
|
|
|
return nil, errors.New("decoder not found for this format")
|
2024-06-24 09:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return decoder, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultCodecRegistry) codec(format string) (Codec, bool) {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
2024-06-24 09:41:23 +00:00
|
|
|
format = strings.ToLower(format)
|
|
|
|
|
2024-06-24 09:37:05 +00:00
|
|
|
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
|
|
|
|
}
|