支持先format, 在验证 #4
24
validate.go
24
validate.go
@ -20,7 +20,6 @@ import (
|
|||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
"strings"
|
"strings"
|
||||||
jsonFilter "git.zhangdeman.cn/zhangdeman/json_filter"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var validatorInstance *validator.Validate
|
var validatorInstance *validator.Validate
|
||||||
@ -36,23 +35,8 @@ func init() {
|
|||||||
//
|
//
|
||||||
// Date : 15:12 2025/3/18
|
// Date : 15:12 2025/3/18
|
||||||
func Run(sourceData []byte, fieldList []StructField) ([]byte, error) {
|
func Run(sourceData []byte, fieldList []StructField) ([]byte, error) {
|
||||||
mapRuleList := make([]jsonFilter.MapRule, 0)
|
|
||||||
for _, itemParam := range fieldList {
|
|
||||||
mapRuleList = append(mapRuleList, jsonFilter.MapRule{
|
|
||||||
SourcePath: itemParam.SourcePath,
|
|
||||||
TargetPath: itemParam.TargetPath,
|
|
||||||
Required: false,
|
|
||||||
DataType: itemParam.Type,
|
|
||||||
DefaultValue: itemParam.DefaultValue,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
filterInstance := jsonFilter.NewFilter(string(sourceData), mapRuleList)
|
|
||||||
if err := filterInstance.Deal(); nil != err {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
formatByteData := filterInstance.Byte()
|
|
||||||
handleInstance := &handle{
|
handleInstance := &handle{
|
||||||
sourceData: formatByteData,
|
sourceData: sourceData,
|
||||||
fieldList: fieldList,
|
fieldList: fieldList,
|
||||||
parentFieldTable: map[string]bool{},
|
parentFieldTable: map[string]bool{},
|
||||||
}
|
}
|
||||||
@ -118,7 +102,11 @@ func (h *handle) Run() ([]byte, error) {
|
|||||||
// 支持嵌套结构体
|
// 支持嵌套结构体
|
||||||
fieldTag := h.generateTag(field)
|
fieldTag := h.generateTag(field)
|
||||||
// 这里需要设置为对应类型的零值就行, 此处传入值的目的只是为了确认数据类型
|
// 这里需要设置为对应类型的零值就行, 此处传入值的目的只是为了确认数据类型
|
||||||
h.dynamicStruct.AddField(field.JsonTag, "", consts.GetDataTypeDefaultValue(field.Type), fieldTag, false)
|
path := field.TargetPath
|
||||||
|
if path == "" {
|
||||||
|
path = field.JsonTag
|
||||||
|
}
|
||||||
|
h.dynamicStruct.AddField(path, "", consts.GetDataTypeDefaultValue(field.Type), fieldTag, false)
|
||||||
}
|
}
|
||||||
val := h.dynamicStruct.Build().New()
|
val := h.dynamicStruct.Build().New()
|
||||||
if err := serialize.JSON.UnmarshalWithNumber([]byte(h.formatVal), &val); nil != err {
|
if err := serialize.JSON.UnmarshalWithNumber([]byte(h.formatVal), &val); nil != err {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user