imports -> Importer
This commit is contained in:
parent
771d7f2334
commit
10f129a610
12 changed files with 183 additions and 8 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
package imports
|
package importer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package imports
|
package importer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
1
importer/csv/csv.go
Normal file
1
importer/csv/csv.go
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
package csv
|
||||||
20
importer/from/source.go
Normal file
20
importer/from/source.go
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
package from
|
||||||
|
|
||||||
|
// Source 导入文件接口
|
||||||
|
type Source interface {
|
||||||
|
Data(page int, size int) []map[string]interface{}
|
||||||
|
Columns() []Column
|
||||||
|
Bind(mapping Mapping)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Column 源数据列
|
||||||
|
type Column struct {
|
||||||
|
Name string
|
||||||
|
Type string
|
||||||
|
Col int
|
||||||
|
Row int
|
||||||
|
Pos string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mapping 数值映射表
|
||||||
|
type Mapping struct{}
|
||||||
92
importer/importer.go
Normal file
92
importer/importer.go
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
package importer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/yaoapp/kun/exception"
|
||||||
|
"github.com/yaoapp/xiang/config"
|
||||||
|
"github.com/yaoapp/xiang/importer/from"
|
||||||
|
"github.com/yaoapp/xiang/share"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Importers 导入器
|
||||||
|
var Importers = map[string]*Importer{}
|
||||||
|
|
||||||
|
// Load 加载导入器
|
||||||
|
func Load(cfg config.Config) {
|
||||||
|
LoadFrom(filepath.Join(cfg.Root, "imports"), "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadFrom 从特定目录加载
|
||||||
|
func LoadFrom(dir string, prefix string) {
|
||||||
|
if share.DirNotExists(dir) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
share.Walk(dir, ".json", func(root, filename string) {
|
||||||
|
var importer Importer
|
||||||
|
name := prefix + share.SpecName(root, filename)
|
||||||
|
content := share.ReadFile(filename)
|
||||||
|
err := jsoniter.Unmarshal(content, &importer)
|
||||||
|
if err != nil {
|
||||||
|
exception.New("%s 导入配置错误. %s", 400, name, err.Error()).Ctx(filename).Throw()
|
||||||
|
}
|
||||||
|
Importers[name] = &importer
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select 选择已加载导入器
|
||||||
|
func Select(name string) *Importer {
|
||||||
|
im, has := Importers[name]
|
||||||
|
if !has {
|
||||||
|
exception.New("导入配置: %s 尚未加载", 400, name).Throw()
|
||||||
|
}
|
||||||
|
return im
|
||||||
|
}
|
||||||
|
|
||||||
|
// AutoMapping 根据文件信息获取字段映射表
|
||||||
|
func (imp *Importer) AutoMapping(src from.Source) *from.Mapping {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preview 预览数据
|
||||||
|
func (imp *Importer) Preview(src from.Source, page int, size int) []map[string]interface{} {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DataSetting 预览数据表格配置
|
||||||
|
func (imp *Importer) DataSetting(src from.Source) []map[string]interface{} {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MappingSetting 预览映射数据表格配置
|
||||||
|
func (imp *Importer) MappingSetting(src from.Source) []map[string]interface{} {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fingerprint 导入文件指纹
|
||||||
|
func (imp *Importer) Fingerprint(src from.Source) string {
|
||||||
|
keys := []string{}
|
||||||
|
columns := src.Columns()
|
||||||
|
for _, col := range columns {
|
||||||
|
keys = append(keys, fmt.Sprintf("%s|%s", col.Name, col.Type))
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
hash := sha256.New()
|
||||||
|
hash.Write([]byte(strings.Join(keys, "")))
|
||||||
|
return fmt.Sprintf("%x", hash.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveAsTemplate 保存为映射模板
|
||||||
|
func (imp *Importer) SaveAsTemplate(src from.Source) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run 运行导入
|
||||||
|
func (imp *Importer) Run() {}
|
||||||
|
|
||||||
|
// Start 运行导入(异步)
|
||||||
|
func (imp *Importer) Start() {}
|
||||||
20
importer/importer_test.go
Normal file
20
importer/importer_test.go
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
package importer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/yaoapp/xiang/importer/xlsx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoad(t *testing.T) {
|
||||||
|
LoadFrom("not a path", "404.")
|
||||||
|
assert.IsType(t, &Importer{}, Select("manu"))
|
||||||
|
}
|
||||||
|
func TestFingerprint(t *testing.T) {
|
||||||
|
file := xlsx.Open()
|
||||||
|
imp := Select("manu")
|
||||||
|
fp := imp.Fingerprint(file)
|
||||||
|
fmt.Println(fp)
|
||||||
|
}
|
||||||
20
importer/init_test.go
Normal file
20
importer/init_test.go
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
package importer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/yaoapp/xiang/config"
|
||||||
|
"github.com/yaoapp/xiang/model"
|
||||||
|
"github.com/yaoapp/xiang/query"
|
||||||
|
"github.com/yaoapp/xiang/share"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
share.DBConnect(config.Conf.Database)
|
||||||
|
model.Load(config.Conf)
|
||||||
|
query.Load(config.Conf)
|
||||||
|
Load(config.Conf)
|
||||||
|
code := m.Run()
|
||||||
|
os.Exit(code)
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package imports
|
package importer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package imports
|
package importer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package imports
|
package importer
|
||||||
|
|
||||||
// PreviewAuto 一直显示
|
// PreviewAuto 一直显示
|
||||||
const PreviewAuto = "auto"
|
const PreviewAuto = "auto"
|
||||||
|
|
@ -9,8 +9,8 @@ const PreviewAlways = "always"
|
||||||
// PreviewNever 从不显示
|
// PreviewNever 从不显示
|
||||||
const PreviewNever = "never"
|
const PreviewNever = "never"
|
||||||
|
|
||||||
// Imports 数据导入
|
// Importer 数据导入器
|
||||||
type Imports struct {
|
type Importer struct {
|
||||||
Title string `json:"title,omitempty"` // 导入名称
|
Title string `json:"title,omitempty"` // 导入名称
|
||||||
Process string `json:"process"` // 处理器名称
|
Process string `json:"process"` // 处理器名称
|
||||||
Columns []Column `json:"columns"` // 字段列表
|
Columns []Column `json:"columns"` // 字段列表
|
||||||
23
importer/xlsx/xlsx.go
Normal file
23
importer/xlsx/xlsx.go
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
package xlsx
|
||||||
|
|
||||||
|
import "github.com/yaoapp/xiang/importer/from"
|
||||||
|
|
||||||
|
// Xlsx xlsx file
|
||||||
|
type Xlsx struct{}
|
||||||
|
|
||||||
|
// Open 打开 Xlsx 文件
|
||||||
|
func Open() *Xlsx {
|
||||||
|
return &Xlsx{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data 读取数据
|
||||||
|
func (xlsx *Xlsx) Data(page int, size int) []map[string]interface{} {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Columns 读取列
|
||||||
|
func (xlsx *Xlsx) Columns() []from.Column { return nil }
|
||||||
|
|
||||||
|
// Bind 绑定映射表
|
||||||
|
func (xlsx *Xlsx) Bind(mapping from.Mapping) {
|
||||||
|
}
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
package imports
|
|
||||||
Loading…
Add table
Reference in a new issue