From 6a917d338ae6cd64d0cd3a279f17d74ac3842517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 4 Nov 2024 17:06:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81xml=20body=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 3 ++- go.sum | 6 ++++++ request/parse_body/execute.go | 1 + request/parse_body/xml.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index be1adf2..0105eb7 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21.0 toolchain go1.23.1 require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3 git.zhangdeman.cn/zhangdeman/exception v0.0.0-20240930081343-1e7f84ed8465 git.zhangdeman.cn/zhangdeman/logger v0.0.0-20240725055115-98eb52ae307a git.zhangdeman.cn/zhangdeman/network v0.0.0-20230925112156-f0eb86dd2442 @@ -53,6 +53,7 @@ require ( github.com/mssola/user_agent v0.6.0 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/sbabiv/xml2map v1.2.1 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/tidwall/gjson v1.18.0 // indirect github.com/tidwall/match v1.1.1 // indirect diff --git a/go.sum b/go.sum index fdc7212..16ce8ba 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,10 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241015035056-b6f4a6512763 h1:aG59Aj git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241015035056-b6f4a6512763/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 h1:TPAYdTKKUjgxtCnK38d1Tb4teyQp1C7wYHPdR32yZtM= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104081823-ab1d691f309d h1:LvQIKDKQ6NNgX/3l3A7tukOFcUeTzC1kEZSeq8eIdUU= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104081823-ab1d691f309d/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3 h1:BiAlBJ+DuRs/xD7nDQD2JT8Oc+V+0Uwt36qZwdXGvzI= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI= git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda/go.mod h1:dT0rmHcJ9Z9IqWeMIt7YzR88nKkNV2V3dfG0j9Q6lK0= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0= @@ -120,6 +124,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sbabiv/xml2map v1.2.1 h1:1lT7t0hhUvXZCkdxqtq4n8/ZCnwLWGq4rDuDv5XOoFE= +github.com/sbabiv/xml2map v1.2.1/go.mod h1:2TPoAfcaM7+Sd4iriPvzyntb2mx7GY+kkQpB/GQa/eo= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/request/parse_body/execute.go b/request/parse_body/execute.go index bc023c4..fee7d20 100644 --- a/request/parse_body/execute.go +++ b/request/parse_body/execute.go @@ -22,6 +22,7 @@ func init() { adaptorList := []abstract.RequestBodyParseAdaptor{ JsonAdaptor{}, FormUrlEncode{}, + XmlAdaptor{}, } for _, itemAdaptor := range adaptorList { Register(itemAdaptor) diff --git a/request/parse_body/xml.go b/request/parse_body/xml.go index 676d6c7..584b0a0 100644 --- a/request/parse_body/xml.go +++ b/request/parse_body/xml.go @@ -6,3 +6,37 @@ // // Date : 2024-10-22 16:42 package parse_body + +import ( + "bytes" + "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/serialize" + "github.com/gin-gonic/gin" + "github.com/sbabiv/xml2map" +) + +type XmlAdaptor struct { + base +} + +func (x XmlAdaptor) Parse(ctx *gin.Context, receiver any) ([]byte, error) { + unmarshalFunc := x.Unmarshal() + if nil == unmarshalFunc { + unmarshalFunc = serialize.Xml.UnmarshalWithNumber + } + return x.DoParse(ctx, receiver, unmarshalFunc) +} + +func (x XmlAdaptor) Unmarshal() func(sourceData []byte, receiver any) error { + return func(sourceData []byte, receiver any) error { + res, err := xml2map.NewDecoder(bytes.NewReader(sourceData)).Decode() + if nil != err { + return err + } + return serialize.JSON.Transition(res, receiver) + } +} + +func (x XmlAdaptor) ContentType() string { + return consts.MimeTypeXml +}