diff --git a/go.mod b/go.mod index b079a16..0b986d3 100644 --- a/go.mod +++ b/go.mod @@ -5,15 +5,17 @@ go 1.21.0 toolchain go1.23.1 require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7 + 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 - git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1 + git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e + git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241013112816-e3de2835a971 github.com/gin-gonic/gin v1.10.0 github.com/go-playground/validator/v10 v10.22.1 github.com/mcuadros/go-defaults v1.2.0 github.com/pkg/errors v0.9.1 + github.com/sbabiv/xml2map v1.2.1 github.com/stretchr/testify v1.9.0 go.uber.org/zap v1.27.0 ) @@ -22,18 +24,17 @@ require ( git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda // indirect git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect - git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd // indirect git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e // indirect git.zhangdeman.cn/zhangdeman/websocket v0.0.0-20240723075210-85feada512b2 // indirect github.com/BurntSushi/toml v1.4.0 // indirect github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect - github.com/bytedance/sonic v1.12.3 // indirect - github.com/bytedance/sonic/loader v0.2.0 // indirect + github.com/bytedance/sonic v1.12.4 // indirect + github.com/bytedance/sonic/loader v0.2.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 // indirect - github.com/gabriel-vasile/mimetype v1.4.5 // indirect + github.com/gabriel-vasile/mimetype v1.4.6 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-ini/ini v1.67.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -54,18 +55,18 @@ require ( github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect - github.com/tidwall/gjson v1.17.3 // indirect + github.com/tidwall/gjson v1.18.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect go.uber.org/multierr v1.11.0 // indirect - golang.org/x/arch v0.10.0 // indirect - golang.org/x/crypto v0.27.0 // indirect - golang.org/x/net v0.29.0 // indirect - golang.org/x/sys v0.25.0 // indirect - golang.org/x/text v0.18.0 // indirect - google.golang.org/protobuf v1.34.2 // indirect + golang.org/x/arch v0.11.0 // indirect + golang.org/x/crypto v0.28.0 // indirect + golang.org/x/net v0.30.0 // indirect + golang.org/x/sys v0.26.0 // indirect + golang.org/x/text v0.19.0 // indirect + google.golang.org/protobuf v1.35.1 // indirect gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index db08b0a..c3d4640 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,13 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7 h1:tyCPCMK+68PZ0axZylQHitMVp1d5mzNr9/YqMHXqo+A= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241015035056-b6f4a6512763 h1:aG59AjZAn/Y+smWoa6l6HQB+yh/Rpml79EvmfjEb6Ks= +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= @@ -15,21 +23,31 @@ git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQ git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd/go.mod h1:6+7whkCmb4sJDIfH3HxNuXRveaM0gCCNWd2uXZqNtIE= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558 h1:ZVJyyDKfYeA3TsN8UOi4IprkouK4wIIfCKe+F9byLWA= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558/go.mod h1:/9eicss/Dt9tp2jwZ/4cXDqDKo/Dez+HuT5/NGdqW+s= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e h1:A045F67AMSqFKGD9kk2uLa+6c/zpmW8vjjSRmSsdjPs= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0= git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI= git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI= git.zhangdeman.cn/zhangdeman/websocket v0.0.0-20240723075210-85feada512b2 h1:P2kuhU2TFWk9mPbJlZCK6FMDVS7S3NGafWKD4eNqKiI= git.zhangdeman.cn/zhangdeman/websocket v0.0.0-20240723075210-85feada512b2/go.mod h1:7KaMpQmWgiNLpEkaV7oDtep7geI1f/VoCoPVcyvMjAE= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1 h1:LYw8NJeWcOiyrGjH9weyxnaMit94MlIngL+uskbLjtw= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1/go.mod h1:+2qNxuRsfyfOvXk9HNwn+CmyPmmhhrQm/eIi1FDU1jw= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241013112816-e3de2835a971 h1:vCkhZliMy2H+OqhcIyhPYQcH13wFuYb9LUeAu71ToNA= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241013112816-e3de2835a971/go.mod h1:+2qNxuRsfyfOvXk9HNwn+CmyPmmhhrQm/eIi1FDU1jw= github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= github.com/bytedance/sonic v1.12.3 h1:W2MGa7RCU1QTeYRTPE3+88mVC0yXmsRQRChiyVocVjU= github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= +github.com/bytedance/sonic v1.12.4 h1:9Csb3c9ZJhfUWeMtpCDCq6BUoH5ogfDFLUgQ/jG+R0k= +github.com/bytedance/sonic v1.12.4/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.1 h1:1GgorWTqf12TA8mma4DDSbaQigE2wOgQo7iCjjJv3+E= +github.com/bytedance/sonic/loader v0.2.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= @@ -42,6 +60,8 @@ github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbY github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4= github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= +github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc= +github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= @@ -108,6 +128,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= @@ -122,6 +144,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94= github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= +github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= @@ -139,20 +163,32 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/arch v0.10.0 h1:S3huipmSclq3PJMNe76NGwkBR504WFkQ5dhzWzP8ZW8= golang.org/x/arch v0.10.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/arch v0.11.0 h1:KXV8WWKCXm6tRpLirl2szsO5j/oOODwZf4hATmGVNs4= +golang.org/x/arch v0.11.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= +golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= +golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= +golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= +google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/init.go b/init.go new file mode 100644 index 0000000..5a03c6a --- /dev/null +++ b/init.go @@ -0,0 +1,12 @@ +// Package gin ... +// +// Description : gin ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 17:01 +package gin + +import ( + _ "git.zhangdeman.cn/zhangdeman/gin/request/parse_body" +) diff --git a/request/abstract/parse_body.go b/request/abstract/parse_body.go new file mode 100644 index 0000000..2b4a497 --- /dev/null +++ b/request/abstract/parse_body.go @@ -0,0 +1,26 @@ +// Package abstract ... +// +// Description : abstract ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:38 +package abstract + +import ( + "github.com/gin-gonic/gin" +) + +// RequestBodyParseAdaptor 解析请求body的接口适配器约束 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:39 2024/10/22 +type RequestBodyParseAdaptor interface { + // Parse 解析Body数据,解析结果会反序列化至 receiver , 同时, 会以 map 结构返回 + Parse(ctx *gin.Context, receiver any) ([]byte, error) + // Unmarshal 自定义反序列化的方法, 为 nil 则使用内置的序列化方式 + Unmarshal() func(sourceData []byte, receiver any) error + // ContentType 当前适配器用与解析何种type + ContentType() string +} diff --git a/request/parse_body/base.go b/request/parse_body/base.go new file mode 100644 index 0000000..792fa8f --- /dev/null +++ b/request/parse_body/base.go @@ -0,0 +1,46 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:51 +package parse_body + +import ( + "bytes" + "github.com/gin-gonic/gin" + "io" +) + +type base struct { +} + +// Unmarshal 反序列化方法,可以不指定 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:54 2024/10/22 +func (b base) Unmarshal() func(sourceData []byte, receiver any) error { + return nil +} + +// DoParse 解析 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:08 2024/10/22 +func (b base) DoParse(ctx *gin.Context, receiver any, unmarshalFunc func(sourceData []byte, receiver any) error) ([]byte, error) { + data, err := io.ReadAll(ctx.Request.Body) + if nil != err { + return nil, err + } + ctx.Request.Body = io.NopCloser(bytes.NewBuffer(data)) + if nil == unmarshalFunc || nil == receiver { + return data, nil + } + if err := unmarshalFunc(data, receiver); nil != err { + return nil, err + } + return data, err +} diff --git a/request/parse_body/execute.go b/request/parse_body/execute.go new file mode 100644 index 0000000..fee7d20 --- /dev/null +++ b/request/parse_body/execute.go @@ -0,0 +1,70 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:43 +package parse_body + +import ( + "errors" + "git.zhangdeman.cn/zhangdeman/gin/request/abstract" + "github.com/gin-gonic/gin" + "strings" +) + +var ( + requestBodyParseAdaptorTable = map[string]abstract.RequestBodyParseAdaptor{} +) + +func init() { + adaptorList := []abstract.RequestBodyParseAdaptor{ + JsonAdaptor{}, + FormUrlEncode{}, + XmlAdaptor{}, + } + for _, itemAdaptor := range adaptorList { + Register(itemAdaptor) + } +} + +// Register 注册适配器实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:45 2024/10/22 +func Register(adaptor abstract.RequestBodyParseAdaptor) { + if nil == adaptor { + return + } + requestBodyParseAdaptorTable[adaptor.ContentType()] = adaptor +} + +// Execute 解析请求BODY数据 +func Execute(ctx *gin.Context, receiver any) ([]byte, error) { + contentType := strings.ToLower(strings.ReplaceAll(ctx.ContentType(), " ", "")) + // 裁剪出真实的类型,之所以截取,是因为 content_type 中可能还包含编码信息, 如 : application/json;charset=utf8 + contentTypeArr := strings.Split(contentType, ";") + contentType = contentTypeArr[0] + if _, exist := requestBodyParseAdaptorTable[contentType]; !exist { + return nil, errors.New(contentType + " : adaptor not found") + } + if parseResult, err := requestBodyParseAdaptorTable[contentType].Parse(ctx, receiver); nil != err { + return nil, err + } else { + return parseResult, err + } +} + +// ExecuteForMap 高层级包装,表单解析为map +func ExecuteForMap(ctx *gin.Context) (map[string]any, error) { + var ( + err error + result map[string]any + ) + if _, err = Execute(ctx, &result); nil != err { + return nil, err + } + return result, nil +} diff --git a/request/parse_body/form_url_encode.go b/request/parse_body/form_url_encode.go new file mode 100644 index 0000000..a456279 --- /dev/null +++ b/request/parse_body/form_url_encode.go @@ -0,0 +1,44 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:43 +package parse_body + +import ( + "encoding/json" + "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/serialize" + "github.com/gin-gonic/gin" +) + +type FormUrlEncode struct { + base +} + +func (f FormUrlEncode) Parse(ctx *gin.Context, receiver any) ([]byte, error) { + if err := ctx.Request.ParseForm(); nil != err { + return nil, err + } + body := map[string]string{} + for paramName, itemParam := range ctx.Request.PostForm { + if len(itemParam) > 0 { + body[paramName] = itemParam[0] + } else { + body[paramName] = "" + } + } + byteData, _ := json.Marshal(body) + if nil != receiver { + if err := serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != receiver { + return nil, err + } + } + return byteData, nil +} + +func (f FormUrlEncode) ContentType() string { + return consts.MimeTypeXWWWFormUrlencoded +} diff --git a/request/parse_body/json.go b/request/parse_body/json.go new file mode 100644 index 0000000..cb2301c --- /dev/null +++ b/request/parse_body/json.go @@ -0,0 +1,49 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:42 +package parse_body + +import ( + "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/serialize" + "github.com/gin-gonic/gin" +) + +type JsonAdaptor struct { + base +} + +// Parse 解析json请求体 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:13 2024/10/22 +func (j JsonAdaptor) Parse(ctx *gin.Context, receiver any) ([]byte, error) { + unmarshalFunc := j.Unmarshal() + if nil == unmarshalFunc { + unmarshalFunc = serialize.JSON.UnmarshalWithNumber + } + return j.DoParse(ctx, receiver, unmarshalFunc) +} + +// Unmarshal 指定解析方法 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:56 2024/10/22 +func (j JsonAdaptor) Unmarshal() func(sourceData []byte, receiver any) error { + return serialize.JSON.UnmarshalWithNumber +} + +// ContentType 请求类型固定返回json +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:59 2024/10/22 +func (j JsonAdaptor) ContentType() string { + return consts.MimeTypeJson +} diff --git a/request/parse_body/xml.go b/request/parse_body/xml.go new file mode 100644 index 0000000..584b0a0 --- /dev/null +++ b/request/parse_body/xml.go @@ -0,0 +1,42 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// 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 +} diff --git a/request/parse_body/yml.go b/request/parse_body/yml.go new file mode 100644 index 0000000..8a18ca3 --- /dev/null +++ b/request/parse_body/yml.go @@ -0,0 +1,8 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:43 +package parse_body diff --git a/request/wrapper.go b/request/wrapper.go index 9e149cb..85cc7f1 100644 --- a/request/wrapper.go +++ b/request/wrapper.go @@ -8,11 +8,9 @@ package request import ( - "bytes" - "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/gin/request/parse_body" "git.zhangdeman.cn/zhangdeman/wrapper" "github.com/gin-gonic/gin" - "io" "strings" ) @@ -140,28 +138,8 @@ func (wh *wrapperHandle) GetDomain(ctx *gin.Context) string { // Date : 11:25 2024/7/26 func (wh *wrapperHandle) ParseBody(ctx *gin.Context) map[string]any { body := map[string]any{} - contentType := wh.GetContentType(ctx, "") - if strings.Contains(contentType, consts.MimeTypeXWWWFormUrlencoded) { // form请求 - if err := ctx.Request.ParseForm(); nil != err { - return body - } - for paramName, itemParam := range ctx.Request.PostForm { - if len(itemParam) > 0 { - body[paramName] = itemParam[0] - } else { - body[paramName] = "" - } - } - return body - } - - if strings.Contains(contentType, consts.MimeTypeJson) || nil != ctx.Request.Body { // json请求 或者非 json请求 存在 body - data, err := io.ReadAll(ctx.Request.Body) - if nil != err { - return body - } - ctx.Request.Body = io.NopCloser(bytes.NewBuffer(data)) - return body + if _, err := parse_body.Execute(ctx, &body); nil != err { + return map[string]any{} } return body