xml.go (676B)
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 "io" 11 "net/http" 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 any) error { 21 return decodeXML(req.Body, obj) 22 } 23 24 func (xmlBinding) BindBody(body []byte, obj any) error { 25 return decodeXML(bytes.NewReader(body), obj) 26 } 27 func decodeXML(r io.Reader, obj any) error { 28 decoder := xml.NewDecoder(r) 29 if err := decoder.Decode(obj); err != nil { 30 return err 31 } 32 return validate(obj) 33 }