Skip to content

Commit

Permalink
imports -> Importer
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Jan 14, 2022
1 parent 771d7f2 commit 10f129a
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 8 deletions.
2 changes: 1 addition & 1 deletion imports/column.go → importer/column.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package imports
package importer

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion imports/column_test.go → importer/column_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package imports
package importer

import (
"testing"
Expand Down
1 change: 1 addition & 0 deletions importer/csv/csv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package csv
20 changes: 20 additions & 0 deletions importer/from/source.go
Original file line number Diff line number Diff line change
@@ -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 changes: 92 additions & 0 deletions importer/importer.go
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions importer/importer_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions importer/init_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion imports/option.go → importer/option.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package imports
package importer

import (
jsoniter "github.com/json-iterator/go"
Expand Down
2 changes: 1 addition & 1 deletion imports/option_test.go → importer/option_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package imports
package importer

import (
"testing"
Expand Down
6 changes: 3 additions & 3 deletions imports/types.go → importer/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package imports
package importer

// PreviewAuto 一直显示
const PreviewAuto = "auto"
Expand All @@ -9,8 +9,8 @@ const PreviewAlways = "always"
// PreviewNever 从不显示
const PreviewNever = "never"

// Imports 数据导入
type Imports struct {
// Importer 数据导入器
type Importer struct {
Title string `json:"title,omitempty"` // 导入名称
Process string `json:"process"` // 处理器名称
Columns []Column `json:"columns"` // 字段列表
Expand Down
23 changes: 23 additions & 0 deletions importer/xlsx/xlsx.go
Original file line number Diff line number Diff line change
@@ -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 change: 0 additions & 1 deletion imports/imports.go

This file was deleted.

0 comments on commit 10f129a

Please sign in to comment.