mesh聚合请求增加返回值构建与解析

This commit is contained in:
白茶清欢 2025-03-31 21:47:47 +08:00
parent b33afff40c
commit 7ebc610c2e

View File

@ -10,13 +10,14 @@ package mesh
import (
"context"
"fmt"
"strings"
"sync"
"git.zhangdeman.cn/gateway/validate"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/network/httpclient"
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
"git.zhangdeman.cn/zhangdeman/serialize"
"strings"
"sync"
)
func Request(req *RequestConfig, receiver any) *Response {
@ -79,6 +80,37 @@ func (c *client) Request() *Response {
return c.resp
}
// 请求成功, 构建返回结果
respByte := serialize.JSON.MarshalForByteIgnoreError(c.resp.AliasResultTable)
fieldList := make([]validate.StructField, 0)
for _, item := range c.reqCfg.ResultRule {
fieldList = append(fieldList, validate.StructField{
JsonTag: "",
Type: item.DataType,
Required: false,
RuleList: nil,
DefaultValue: "",
SourcePath: fmt.Sprintf("%v.%v.%v", item.RequestAlias, item.RequestResultLocation.String(), item.RequestResultPath),
TargetPath: fmt.Sprintf("%v.%v", item.Location, item.DataPath),
Errmsg: "",
})
}
resByte, err := validate.Run(respByte, fieldList)
if nil != err {
c.resp.ErrorCode = "-500"
c.resp.ErrorMessage = err.Error()
return c.resp
}
c.resp.Raw = respByte // 原始返回数据
serialize.JSON.UnmarshalWithNumberIgnoreError(resByte, &c.resp.DataMap) // map结果
if nil != c.receiver {
// 解析到receiver
if err = serialize.JSON.UnmarshalWithNumber(resByte, c.receiver); nil != err {
c.resp.ErrorCode = "-500"
c.resp.ErrorMessage = err.Error()
return c.resp
}
}
return c.resp
}