toml.go 704 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2022 Gin Core Team. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. import (
  6. "bytes"
  7. "io"
  8. "net/http"
  9. "github.com/pelletier/go-toml/v2"
  10. )
  11. type tomlBinding struct{}
  12. func (tomlBinding) Name() string {
  13. return "toml"
  14. }
  15. func (tomlBinding) Bind(req *http.Request, obj any) error {
  16. return decodeToml(req.Body, obj)
  17. }
  18. func (tomlBinding) BindBody(body []byte, obj any) error {
  19. return decodeToml(bytes.NewReader(body), obj)
  20. }
  21. func decodeToml(r io.Reader, obj any) error {
  22. decoder := toml.NewDecoder(r)
  23. if err := decoder.Decode(obj); err != nil {
  24. return err
  25. }
  26. return decoder.Decode(obj)
  27. }