AutoMapping

This commit is contained in:
Max 2022-01-17 15:43:46 +08:00
parent 952a0e9440
commit d572b8599f
3 changed files with 26 additions and 15 deletions

View file

@ -9,7 +9,6 @@ import (
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/kun/exception" "github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/utils"
"github.com/yaoapp/xiang/config" "github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/importer/from" "github.com/yaoapp/xiang/importer/from"
"github.com/yaoapp/xiang/share" "github.com/yaoapp/xiang/share"
@ -56,6 +55,7 @@ func (imp *Importer) AutoMapping(src from.Source) *Mapping {
mapping := &Mapping{ mapping := &Mapping{
Columns: []*Binding{}, Columns: []*Binding{},
AutoMatching: true, AutoMatching: true,
TemplateMatching: false,
Sheet: sourceInspect.SheetName, Sheet: sourceInspect.SheetName,
ColStart: sourceInspect.ColStart, ColStart: sourceInspect.ColStart,
RowStart: sourceInspect.RowStart, RowStart: sourceInspect.RowStart,
@ -83,8 +83,7 @@ func (imp *Importer) AutoMapping(src from.Source) *Mapping {
mapping.Columns = append(mapping.Columns, &binding) mapping.Columns = append(mapping.Columns, &binding)
} }
utils.Dump(mapping) return mapping
return nil
} }
// MappingPreview 预览字段映射关系 // MappingPreview 预览字段映射关系

View file

@ -5,7 +5,6 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/yaoapp/kun/utils"
"github.com/yaoapp/xiang/config" "github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/importer/xlsx" "github.com/yaoapp/xiang/importer/xlsx"
) )
@ -30,5 +29,17 @@ func TestAutoMappingSimple(t *testing.T) {
imp := Select("order") imp := Select("order")
mapping := imp.AutoMapping(file) mapping := imp.AutoMapping(file)
utils.Dump(mapping) assert.Equal(t, true, mapping.AutoMatching)
assert.Equal(t, false, mapping.TemplateMatching)
assert.Equal(t, 1, mapping.ColStart)
assert.Equal(t, 1, mapping.RowStart)
assert.Equal(t, 10, len(mapping.Columns))
assert.Equal(t, len(imp.Columns), len(mapping.Columns))
for i, col := range mapping.Columns {
dst := imp.Columns[i].ToMap()
assert.Equal(t, dst["name"], col.Field)
assert.Equal(t, dst["label"], col.Label)
assert.Equal(t, dst["rules"], col.Rules)
assert.NotEmpty(t, col.Axis)
}
} }

View file

@ -46,6 +46,7 @@ type Mapping struct {
RowStart int `json:"rowStart"` // 第一行的位置 RowStart int `json:"rowStart"` // 第一行的位置
Columns []*Binding `json:"data"` // 字段数据列表 Columns []*Binding `json:"data"` // 字段数据列表
AutoMatching bool `json:"autoMatching"` // 是否自动匹配 AutoMatching bool `json:"autoMatching"` // 是否自动匹配
TemplateMatching bool `json:"templateMatching"` // 是否通过已传模板匹配
} }
// Binding 数据绑定 // Binding 数据绑定