Debian Codegraph @

parsers/toml/toml.go

raw ◀ ▶
1 Debian patch touch this file
1 // Package toml implements a koanf.Parser that parses TOML bytes as conf maps.
2 package toml
3
4 import (
5 "github.com/pelletier/go-toml/v2"
6 )
7
8 // TOML implements a TOML parser.
9 type TOML struct{}
10
11 // Parser returns a TOML Parser.
12 func Parser() *TOML {
13 return &TOML{}
14 }
15
16 // Unmarshal parses the given TOML bytes.
17 func (p *TOML) Unmarshal(b []byte) (map[string]any, error) {
18 var outMap map[string]any
19
20 if err := toml.Unmarshal(b, &outMap); err != nil {
21 return nil, err
22 }
23
•24 if len(outMap) == 0 {
25 return nil, nil
26 }
27
28 return outMap, nil
29 }
30
31 // Marshal marshals the given config map to TOML bytes.
32 func (p *TOML) Marshal(o map[string]any) ([]byte, error) {
•33 if len(o) == 0 {
34 return nil, nil
35 }
36
37 out, err := toml.Marshal(&o)
38 if err != nil {
39 return nil, err
40 }
41
42 return out, nil
43 }