解决类型反射NPE问题

This commit is contained in:
白茶清欢 2022-01-27 14:23:41 +08:00
parent 19813fa9ee
commit 51808626a9
2 changed files with 6 additions and 3 deletions

View File

@ -129,7 +129,11 @@ func (df *DataFilter) UserItemToSlice() {
// //
// Date : 2022/1/23 12:45 AM // Date : 2022/1/23 12:45 AM
func (df *DataFilter) getValueType(valueResult gjson.Result) string { func (df *DataFilter) getValueType(valueResult gjson.Result) string {
dataType := reflect.TypeOf(valueResult.Value()).String() dataTypeVal := reflect.TypeOf(valueResult.Value())
if nil == dataTypeVal {
return "NIL"
}
dataType := dataTypeVal.String()
if strings.Contains(dataType, "int") { if strings.Contains(dataType, "int") {
return "int64" return "int64"
} }
@ -243,7 +247,6 @@ func (df *DataFilter) getArrayData(source string, pathArr []string) []gjson.Resu
resultList := make([]gjson.Result, 0) resultList := make([]gjson.Result, 0)
dataList := gjson.Get(source, pathArr[0]).Array() dataList := gjson.Get(source, pathArr[0]).Array()
for idx := 0; idx < len(dataList); idx++ { for idx := 0; idx < len(dataList); idx++ {
// sourceData := gjson.Get(source, dataList[idx].String()).String()
resultList = append(resultList, df.getArrayData(dataList[idx].String(), pathArr[1:])...) resultList = append(resultList, df.getArrayData(dataList[idx].String(), pathArr[1:])...)
} }
return resultList return resultList

View File

@ -267,7 +267,7 @@ func TestDataFilterDiffArr(t *testing.T) {
rule := []*FilterDataRule{ rule := []*FilterDataRule{
// {SourceKey: "name", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"}, // {SourceKey: "name", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"},
{SourceKey: "table.[].user_list.[].name", MapKey: "user_list.[].detail.name", DefaultValue: "用户姓名默认值"}, {SourceKey: "table.[].user_list.[].name", MapKey: "user_list.[].detail.name", DefaultValue: "用户姓名默认值"},
{SourceKey: "table.[].user_list.[].age", MapKey: "user_list.[]detail.age", DefaultValue: "用户姓名默认值"}, {SourceKey: "table.[].user_list.[].age", MapKey: "user_list.[].detail.age", DefaultValue: "用户姓名默认值"},
} }
byteData, _ := json.Marshal(source) byteData, _ := json.Marshal(source)
filter := NewDataFilter(string(byteData), rule) filter := NewDataFilter(string(byteData), rule)