From 30069763a8a259cc1aaea272758be3ce6bf6be0a Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 24 Jan 2022 00:22:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BC=95=E6=93=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 1 + importer/importer.go | 15 +++++++++------ share/importable.go | 2 +- share/importable_test.go | 2 +- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index c7800eca..1e0d88ed 100644 --- a/go.mod +++ b/go.mod @@ -47,6 +47,7 @@ require ( golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1 // indirect google.golang.org/grpc v1.42.0 // indirect + rogchap.com/v8go v0.7.0 // indirect ) // go env -w GOPRIVATE=github.com/yaoapp/* diff --git a/go.sum b/go.sum index 6ae6b30c..9f41a34e 100644 --- a/go.sum +++ b/go.sum @@ -886,6 +886,7 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= +rogchap.com/v8go v0.7.0 h1:kgjbiO4zE5itA962ze6Hqmbs4HgZbGzmueCXsZtremg= rogchap.com/v8go v0.7.0/go.mod h1:MxgP3pL2MW4dpme/72QRs8sgNMmM0pRc8DPhcuLWPAs= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= diff --git a/importer/importer.go b/importer/importer.go index cda93f7f..4e9e294e 100644 --- a/importer/importer.go +++ b/importer/importer.go @@ -143,8 +143,11 @@ func (imp *Importer) DataClean(data [][]interface{}, bindings []*Binding) ([]str success := true for i, binding := range bindings { // 调用字段清洗处理器 for _, rule := range binding.Rules { - if !DataValidate(row, row[i], rule) { + update, ok := DataValidate(row, row[i], rule) + if !ok { success = false + } else { + row = update } } } @@ -157,23 +160,23 @@ func (imp *Importer) DataClean(data [][]interface{}, bindings []*Binding) ([]str } // DataValidate 数值校验 -func DataValidate(row []interface{}, value interface{}, rule string) bool { +func DataValidate(row []interface{}, value interface{}, rule string) ([]interface{}, bool) { process, err := gou.ProcessOf(rule, value, row) if err != nil { xlog.Printf("DataValidate: %s %s", rule, err.Error()) - return true + return row, true } res, err := process.Exec() if err != nil { xlog.Printf("DataValidate: %s %s", rule, err.Error()) - return true + return row, true } if update, ok := res.([]interface{}); ok { row = update - return true + return update, true } - return false + return row, false } // DataPreview 预览数据 diff --git a/share/importable.go b/share/importable.go index a333859c..d09f982b 100644 --- a/share/importable.go +++ b/share/importable.go @@ -49,7 +49,7 @@ func LoadFrom(dir string) { // 加载共享脚本 Walk(dir, ".js", func(root, filename string) { name := SpecName(root, filename) - err := gou.JavaScriptVM.Load(filename, name) + err := gou.Yao.Load(filename, name) if err != nil { xlog.Printf("加载脚本失败 %s", err.Error()) } diff --git a/share/importable_test.go b/share/importable_test.go index 9d0b6e09..6102400e 100644 --- a/share/importable_test.go +++ b/share/importable_test.go @@ -66,7 +66,7 @@ func TestAPI(t *testing.T) { } func TestScript(t *testing.T) { - res, err := gou.JavaScriptVM.Run("time", "hello", "world") + res, err := gou.Yao.New("time", "hello").Call("world") assert.Nil(t, err) assert.Equal(t, "name:world", res) }