mirror of
https://github.com/hanzoai/gateway.git
synced 2026-10-30 15:04:44 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package xml
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/clbanning/mxj/v2"
|
|
"github.com/hanzoai/gateway/v2/internal/lura/encoding"
|
|
"golang.org/x/net/html/charset"
|
|
)
|
|
|
|
// Register registers the xml decoder
|
|
func Register() error {
|
|
return encoding.GetRegister().Register(Name, NewDecoder)
|
|
}
|
|
|
|
// Name is the key for the xml encoding
|
|
const Name = "xml"
|
|
|
|
// NewDecoder return the right XML decoder
|
|
func NewDecoder(isCollection bool) func(io.Reader, *map[string]any) error {
|
|
if isCollection {
|
|
return CollectionDecoder
|
|
}
|
|
return Decoder
|
|
}
|
|
|
|
// Decoder implements the Decoder interface
|
|
func Decoder(r io.Reader, v *map[string]any) error {
|
|
mxj.XmlCharsetReader = charset.NewReaderLabel
|
|
mv, err := mxj.NewMapXmlReader(xmlReader{r: r})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*v = mv
|
|
return nil
|
|
}
|
|
|
|
// CollectionDecoder implements the Decoder interface over a collection
|
|
func CollectionDecoder(r io.Reader, v *map[string]any) error {
|
|
mxj.XmlCharsetReader = charset.NewReaderLabel
|
|
mv, err := mxj.NewMapXmlReader(xmlReader{r: r})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*(v) = map[string]any{"collection": mv}
|
|
return nil
|
|
}
|
|
|
|
type xmlReader struct {
|
|
r io.Reader
|
|
}
|
|
|
|
func (x xmlReader) Read(p []byte) (n int, err error) {
|
|
n, err = x.r.Read(p)
|
|
|
|
if err != io.EOF {
|
|
return n, err
|
|
}
|
|
|
|
if len(p) == n {
|
|
return n, nil
|
|
}
|
|
|
|
p[n] = ([]byte("\n"))[0]
|
|
return n + 1, err
|
|
}
|