github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/binding/xml.go (about) 1 // Copyright 2014 Manu Martinez-Almeida. 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 "encoding/xml" 10 "github.com/hellobchain/newcryptosm/http" 11 "io" 12 ) 13 14 type xmlBinding struct{} 15 16 func (xmlBinding) Name() string { 17 return "xml" 18 } 19 20 func (xmlBinding) Bind(req *http.Request, obj interface{}) error { 21 return decodeXML(req.Body, obj) 22 } 23 24 func (xmlBinding) BindBody(body []byte, obj interface{}) error { 25 return decodeXML(bytes.NewReader(body), obj) 26 } 27 func decodeXML(r io.Reader, obj interface{}) error { 28 decoder := xml.NewDecoder(r) 29 if err := decoder.Decode(obj); err != nil { 30 return err 31 } 32 return validate(obj) 33 }