feat:优化导入数据结构

This commit is contained in:
henry
2022-04-15 09:13:57 +08:00
parent 9295c5a7c0
commit cb4e02e6e3
2 changed files with 13 additions and 10 deletions

View File

@ -224,20 +224,21 @@ func (this *Excel) Save(path string) error {
} }
// Analysis 解析 // Analysis 解析
func (this *Excel) Analysis(src interface{}) []interface{} { // @dist标识对象
if src == nil { func (this *Excel) Analysis(dist interface{}) []interface{} {
if dist == nil {
return nil return nil
} }
out := make([]interface{}, 0) out := make([]interface{}, len(this.body))
// 原指针 // 原指针
_src := reflect.TypeOf(src) _dist := reflect.TypeOf(dist)
// 指针类型获取真正type需要调用Elem // 指针类型获取真正type需要调用Elem
if _src.Kind() == reflect.Ptr { if _dist.Kind() == reflect.Ptr {
_src = _src.Elem() _dist = _dist.Elem()
} }
for _, val := range this.body { for key, val := range this.body {
tRef := reflect.New(_src) tRef := reflect.New(_dist)
fieldNum := tRef.Elem().NumField() fieldNum := tRef.Elem().NumField()
@ -271,7 +272,7 @@ func (this *Excel) Analysis(src interface{}) []interface{} {
} }
} }
} }
out = append(out, tRef.Interface()) out[key] = tRef.Interface()
} }
return out return out
} }

View File

@ -44,9 +44,11 @@ func TestNewExcelImport(t *testing.T) {
} }
data := excel.Analysis(&Data{}) data := excel.Analysis(&Data{})
t.Log(len(data))
for _, v := range data { for _, v := range data {
_data, ok := v.(*Data) _data, ok := v.(*Data)
t.Log(ok) t.Log(ok)
t.Log(_data.Name) t.Log(_data)
} }
} }