优化handleSliceString
This commit is contained in:
		@ -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)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							err error
 | 
				
			||||||
 | 
							res []any
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
	if rule.SliceConfig.Mode == consts.DataSliceModelMarshal { // json序列化之后的
 | 
						if rule.SliceConfig.Mode == consts.DataSliceModelMarshal { // json序列化之后的
 | 
				
			||||||
		var (
 | 
					 | 
				
			||||||
			err error
 | 
					 | 
				
			||||||
			res []any
 | 
					 | 
				
			||||||
		)
 | 
					 | 
				
			||||||
		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)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user