You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
901 B
43 lines
901 B
11 months ago
|
package mapping
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"github.com/pelletier/go-toml/v2"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
// UnmarshalTomlBytes unmarshals TOML bytes into the given v.
|
||
|
func UnmarshalTomlBytes(content []byte, v any, opts ...UnmarshalOption) error {
|
||
|
b, err := TomlToJson(content)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return UnmarshalJsonBytes(b, v, opts...)
|
||
|
}
|
||
|
|
||
|
// UnmarshalTomlReader unmarshals TOML from the given io.Reader into the given v.
|
||
|
func UnmarshalTomlReader(r io.Reader, v any, opts ...UnmarshalOption) error {
|
||
|
b, err := io.ReadAll(r)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return UnmarshalTomlBytes(b, v, opts...)
|
||
|
}
|
||
|
|
||
|
func TomlToJson(data []byte) ([]byte, error) {
|
||
|
var val any
|
||
|
if err := toml.NewDecoder(bytes.NewReader(data)).Decode(&val); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
var buf bytes.Buffer
|
||
|
if err := json.NewEncoder(&buf).Encode(val); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return buf.Bytes(), nil
|
||
|
}
|