完成数据过主干逻辑,细节健壮性有待继续完善 #4
							
								
								
									
										37
									
								
								tool/gabs.go
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								tool/gabs.go
									
									
									
									
									
								
							| @ -19,10 +19,6 @@ import ( | ||||
| 	"github.com/tidwall/sjson" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| 	virtualRoot = "__virtual__root" | ||||
| ) | ||||
|  | ||||
| // FilterOption 过滤选项 | ||||
| // | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| @ -58,6 +54,11 @@ func NewDataFilter(source string, filterRule []*FilterDataRule, filterOption *Fi | ||||
| 		filterOption.LogInstance = os.Stdout | ||||
| 		log.SetOutput(filterOption.LogInstance) | ||||
| 	} | ||||
| 	// 去除末尾的 .[] | ||||
| 	for _, item := range filterRule { | ||||
| 		item.MapKey = strings.TrimRight(item.MapKey, ".[]") | ||||
| 		item.SourceKey = strings.TrimRight(item.SourceKey, ".[]") | ||||
| 	} | ||||
| 	return &DataFilter{ | ||||
| 		source:        source, | ||||
| 		filterRule:    filterRule, | ||||
| @ -97,6 +98,7 @@ func (df *DataFilter) Filter() (string, error) { | ||||
| 			if err = df.setKV(itemRule); nil != err { | ||||
| 				return "", err | ||||
| 			} | ||||
| 			continue | ||||
| 		} | ||||
| 		sourcePathArr := df.getArrPathList(itemRule.SourceKey) | ||||
| 		mapPathArr := df.getArrPathList(itemRule.MapKey) | ||||
| @ -104,11 +106,19 @@ func (df *DataFilter) Filter() (string, error) { | ||||
| 			df.logPrint(logLevelFatal, "映射的层级深度大于数据源深度", "source_path => "+itemRule.SourceKey, "map_path => "+itemRule.MapKey) | ||||
| 			return "", fmt.Errorf("映射的层级深度大于数据源深度, source_path => %v map_path => %v", itemRule.SourceKey, itemRule.MapKey) | ||||
| 		} | ||||
|  | ||||
| 		// 映射至非数组字段 | ||||
| 		if !mapIsArr { | ||||
| 			if err = df.setValue(itemRule.MapKey, df.getDataAsSlice(df.source, df.getArrPathList(itemRule.SourceKey))); nil != err { | ||||
| 				df.logPrint(logLevelFatal, "映射非数组, 数据源为数组, 设置失败", "source_path => "+itemRule.SourceKey, "map_path => "+itemRule.MapKey, " err => "+err.Error()) | ||||
| 				return "", fmt.Errorf("映射的层级深度大于数据源深度, source_path => %v map_path => %v", itemRule.SourceKey, itemRule.MapKey) | ||||
| 			} | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		if len(mapPathArr) == len(sourcePathArr) { | ||||
| 			// 数组深度一致 | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 	} | ||||
| @ -124,8 +134,27 @@ func (df *DataFilter) isArrPath(path string) bool { | ||||
| 	return strings.Contains(path, "[]") | ||||
| } | ||||
|  | ||||
| // setSameDeepArr 设置同深度的数组 | ||||
| // | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 12:19 2023/9/2 | ||||
| func (df *DataFilter) setSameDeepArr(sourceVal string, sourcePath string, mapPath string) error { | ||||
| 	sourcePathArr := df.getArrPathList(sourcePath) | ||||
| 	mapPathArr := df.getArrPathList(mapPath) | ||||
| 	for idx, itemSourcePath := range sourcePathArr { | ||||
| 		sourceValueArr := gjson.Get(sourceVal, sourcePath).Array() | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // a.[].b.[].c.[].d | ||||
| // e.[].f.[].g | ||||
| // getDataAsSlice 抽取制定深度,生成list | ||||
| // | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 12:17 2023/9/2 | ||||
| func (df *DataFilter) getDataAsSlice(sourceData string, pathList []string) []interface{} { | ||||
| 	//fmt.Println(sourceData, pathList) | ||||
| 	result := make([]interface{}, 0) | ||||
|  | ||||
		Reference in New Issue
	
	Block a user