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 decodeToml(r io.Reader, obj any) error {
  16. decoder := toml.NewDecoder(r)
  17. if err := decoder.Decode(obj); err != nil {
  18. return err
  19. }
  20. return decoder.Decode(obj)
  21. }
  22. func (tomlBinding) Bind(req *http.Request, obj any) error {
  23. return decodeToml(req.Body, obj)
  24. }
  25. func (tomlBinding) BindBody(body []byte, obj any) error {
  26. return decodeToml(bytes.NewReader(body), obj)
  27. }