mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
By passing the Gin context to bindings, custom validators can take advantage of the information in the context.
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package binding
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/xml"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
type xmlBinding struct{}
|
|
|
|
func (xmlBinding) Name() string {
|
|
return "xml"
|
|
}
|
|
|
|
func (b xmlBinding) Bind(req *http.Request, obj interface{}) error {
|
|
return b.BindContext(context.Background(), req, obj)
|
|
}
|
|
|
|
func (xmlBinding) BindContext(ctx context.Context, req *http.Request, obj interface{}) error {
|
|
return decodeXML(ctx, req.Body, obj)
|
|
}
|
|
|
|
func (b xmlBinding) BindBody(body []byte, obj interface{}) error {
|
|
return b.BindBodyContext(context.Background(), body, obj)
|
|
}
|
|
|
|
func (xmlBinding) BindBodyContext(ctx context.Context, body []byte, obj interface{}) error {
|
|
return decodeXML(ctx, bytes.NewReader(body), obj)
|
|
}
|
|
|
|
func decodeXML(ctx context.Context, r io.Reader, obj interface{}) error {
|
|
decoder := xml.NewDecoder(r)
|
|
if err := decoder.Decode(obj); err != nil {
|
|
return err
|
|
}
|
|
return validateContext(ctx, obj)
|
|
}
|