tomlunmarshaler.go 657 B

1234567891011121314151617181920212223242526272829
  1. package mapping
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "github.com/pelletier/go-toml/v2"
  7. )
  8. // UnmarshalTomlBytes unmarshals TOML bytes into the given v.
  9. func UnmarshalTomlBytes(content []byte, v interface{}) error {
  10. return UnmarshalTomlReader(bytes.NewReader(content), v)
  11. }
  12. // UnmarshalTomlReader unmarshals TOML from the given io.Reader into the given v.
  13. func UnmarshalTomlReader(r io.Reader, v interface{}) error {
  14. var val interface{}
  15. if err := toml.NewDecoder(r).Decode(&val); err != nil {
  16. return err
  17. }
  18. var buf bytes.Buffer
  19. if err := json.NewEncoder(&buf).Encode(val); err != nil {
  20. return err
  21. }
  22. return UnmarshalJsonReader(&buf, v)
  23. }