完成数据过主干逻辑,细节健壮性有待继续完善 #4
| @ -105,6 +105,10 @@ func (df *DataFilter) Filter() (string, error) { | ||||
| 			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) | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	} | ||||
|  | ||||
| @ -85,3 +85,58 @@ func TestDataFilter_getDataAsSlice(t *testing.T) { | ||||
| 	fmt.Println(df.getDataAsSlice(string(byteData), []string{"list", "age", "list_test", "a"})) | ||||
| 	fmt.Println(df.getDataAsSlice(string(byteData), []string{"list", "age", "list_test", "b"})) | ||||
| } | ||||
|  | ||||
| // TestDataFilter_sourceArrAndMapSingle map 非数组,数据源数组 | ||||
| // | ||||
| // Author : zhangdeman001@ke.com<张德满> | ||||
| // | ||||
| // Date : 11:24 2023/9/2 | ||||
| func TestDataFilter_sourceArrAndMapSingle(t *testing.T) { | ||||
| 	source := map[string]interface{}{ | ||||
| 		"list": []interface{}{ | ||||
| 			map[string]interface{}{ | ||||
| 				"name": "1", | ||||
| 				"age":  "2", | ||||
| 				"list_test": []interface{}{ | ||||
| 					map[string]interface{}{ | ||||
| 						"a": "a", | ||||
| 						"b": "b", | ||||
| 						"c": map[string]interface{}{ | ||||
| 							"a": "1", | ||||
| 						}, | ||||
| 						"d": []int{1, 2, 3}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			map[string]interface{}{ | ||||
| 				"name": "3", | ||||
| 				"age":  "4", | ||||
| 				"list_test": []interface{}{ | ||||
| 					map[string]interface{}{ | ||||
| 						"a": "a1", | ||||
| 						"b": "b1", | ||||
| 						"c": map[string]interface{}{ | ||||
| 							"a": "a", | ||||
| 						}, | ||||
| 						"d": []int{1, 2, 3}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			map[string]interface{}{ | ||||
| 				"name": "5", | ||||
| 				"age":  "6", | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	df := &DataFilter{ | ||||
| 		source: serialize.JSON.MarshalForString(source), | ||||
| 		filterRule: []*FilterDataRule{ | ||||
| 			{SourceKey: "list.[].list_test.[].a", MapKey: "a_list", DefaultValue: "[]", WithDefault: true}, | ||||
| 			{SourceKey: "list.[].list_test.[].b", MapKey: "b_list", DefaultValue: "[]", WithDefault: true}, | ||||
| 			{SourceKey: "list.[].list_test.[].c", MapKey: "c_list", DefaultValue: "[]", WithDefault: true}, | ||||
| 			{SourceKey: "list.[].list_test.[].d", MapKey: "d_list", DefaultValue: "[]", WithDefault: true}, | ||||
| 		}, | ||||
| 		filterOption: &FilterOption{DebugModel: true}, | ||||
| 	} | ||||
| 	fmt.Println(df.Filter()) | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user