forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
183 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package imports | ||
package importer | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package imports | ||
package importer | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package csv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package imports | ||
package importer | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
This file was deleted.
Oops, something went wrong.