优化handleSliceString

This commit is contained in:
白茶清欢 2024-05-16 21:50:00 +08:00
parent 6a9339c7de
commit 67abdaeb7d

View File

@ -245,11 +245,11 @@ func handleSliceString(inputStr string, rule *define.FieldRule) ([]any, error) {
if rule.SliceConfig.Mode == consts.DataSliceModelReal { if rule.SliceConfig.Mode == consts.DataSliceModelReal {
return nil, fmt.Errorf("%v : data type expect real slice, but get string", rule.Path) return nil, fmt.Errorf("%v : data type expect real slice, but get string", rule.Path)
} }
if rule.SliceConfig.Mode == consts.DataSliceModelMarshal { // json序列化之后的
var ( var (
err error err error
res []any res []any
) )
if rule.SliceConfig.Mode == consts.DataSliceModelMarshal { // json序列化之后的
if err = serialize.JSON.UnmarshalWithNumber([]byte(inputStr), &res); nil != err { if err = serialize.JSON.UnmarshalWithNumber([]byte(inputStr), &res); nil != err {
return nil, fmt.Errorf("%v : data type expect marshal slice, but can not convert", rule.Path) return nil, fmt.Errorf("%v : data type expect marshal slice, but can not convert", rule.Path)
} }
@ -257,15 +257,15 @@ func handleSliceString(inputStr string, rule *define.FieldRule) ([]any, error) {
} }
if rule.SliceConfig.Mode == consts.DataSliceModelWithSplitChar { // 指定字符串切割 if rule.SliceConfig.Mode == consts.DataSliceModelWithSplitChar { // 指定字符串切割
strArr := strings.Split(inputStr, rule.SliceConfig.SplitChar) strArr := strings.Split(inputStr, rule.SliceConfig.SplitChar)
anyArr := make([]any, 0) res = make([]any, 0)
for _, item := range strArr { for _, item := range strArr {
if !rule.SliceConfig.DisableIgnoreEmpty && len(item) == 0 { if !rule.SliceConfig.DisableIgnoreEmpty && len(item) == 0 {
// 没禁用忽略空字符串 // 没禁用忽略空字符串
continue continue
} }
anyArr = append(anyArr, item) res = append(res, item)
} }
return anyArr, nil return res, nil
} }
return nil, fmt.Errorf("%v : data type is slice, but rule mode [%v] is not supported", rule.Path, rule.SliceConfig.Mode) return nil, fmt.Errorf("%v : data type is slice, but rule mode [%v] is not supported", rule.Path, rule.SliceConfig.Mode)
} }