toml.go (704B)
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 5 package binding 6 7 import ( 8 "bytes" 9 "io" 10 "net/http" 11 12 "github.com/pelletier/go-toml/v2" 13 ) 14 15 type tomlBinding struct{} 16 17 func (tomlBinding) Name() string { 18 return "toml" 19 } 20 21 func (tomlBinding) Bind(req *http.Request, obj any) error { 22 return decodeToml(req.Body, obj) 23 } 24 25 func (tomlBinding) BindBody(body []byte, obj any) error { 26 return decodeToml(bytes.NewReader(body), obj) 27 } 28 29 func decodeToml(r io.Reader, obj any) error { 30 decoder := toml.NewDecoder(r) 31 if err := decoder.Decode(obj); err != nil { 32 return err 33 } 34 return decoder.Decode(obj) 35 }