File: toml.go

package info (click to toggle)
golang-github-knadh-koanf 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 808 kB
  • sloc: sh: 31; makefile: 14
file content (34 lines) | stat: -rw-r--r-- 715 bytes parent folder | download
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
// Package toml implements a koanf.Parser that parses TOML bytes as conf maps.
package toml

import (
	"bytes"

	"github.com/pelletier/go-toml"
)

// TOML implements a TOML parser.
type TOML struct{}

// Parser returns a TOML Parser.
func Parser() *TOML {
	return &TOML{}
}

// Unmarshal parses the given TOML bytes.
func (p *TOML) Unmarshal(b []byte) (map[string]interface{}, error) {
	r, err := toml.LoadReader(bytes.NewBuffer(b))
	if err != nil {
		return nil, err
	}
	return r.ToMap(), err
}

// Marshal marshals the given config map to TOML bytes.
func (p *TOML) Marshal(o map[string]interface{}) ([]byte, error) {
	out, err := toml.TreeFromMap(o)
	if err != nil {
		return nil, err
	}
	return out.Marshal()
}