summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/spf13/viper/internal/encoding/toml/codec.go
blob: 45fddc8b5959865cc88135b2fb47f3f1309386f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//go:build viper_toml1
// +build viper_toml1

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
}