spf13--viper/internal/encoding/toml/codec.go
Mark Sagi-Kazar 4b307cc0f3 feat(encoding)!: accept a map in the encoder interface
This interface is specific to encoding data from Viper's internal,
so it's okay to make it Viper specific.

BREAKING CHANGE: the encoder interface now accepts a map instead of an interface

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
2021-12-15 22:31:11 +01:00

37 lines
617 B
Go

package toml
import (
"github.com/pelletier/go-toml"
)
// Codec implements the encoding.Encoder and encoding.Decoder interfaces for TOML encoding.
type Codec struct{}
func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
t, err := toml.TreeFromMap(v)
if err != nil {
return nil, err
}
s, err := t.ToTomlString()
if err != nil {
return nil, err
}
return []byte(s), nil
}
func (Codec) Decode(b []byte, v map[string]interface{}) error {
tree, err := toml.LoadBytes(b)
if err != nil {
return err
}
tmap := tree.ToMap()
for key, value := range tmap {
v[key] = value
}
return nil
}