2022-09-28 18:30:40 +01:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2021-08-12 20:03:24 +01:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package binding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type xmlBinding struct{}
|
|
|
|
|
|
|
|
func (xmlBinding) Name() string {
|
|
|
|
return "xml"
|
|
|
|
}
|
|
|
|
|
2022-09-28 18:30:40 +01:00
|
|
|
func (xmlBinding) Bind(req *http.Request, obj any) error {
|
2021-08-12 20:03:24 +01:00
|
|
|
return decodeXML(req.Body, obj)
|
|
|
|
}
|
|
|
|
|
2022-09-28 18:30:40 +01:00
|
|
|
func (xmlBinding) BindBody(body []byte, obj any) error {
|
2021-08-12 20:03:24 +01:00
|
|
|
return decodeXML(bytes.NewReader(body), obj)
|
|
|
|
}
|
2022-09-28 18:30:40 +01:00
|
|
|
func decodeXML(r io.Reader, obj any) error {
|
2021-08-12 20:03:24 +01:00
|
|
|
decoder := xml.NewDecoder(r)
|
|
|
|
if err := decoder.Decode(obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return validate(obj)
|
|
|
|
}
|