Skip to content

Commit

Permalink
拆分处理器目录
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Feb 9, 2022
1 parent 0429961 commit 292b10f
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 15 deletions.
2 changes: 2 additions & 0 deletions engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/yaoapp/xiang/page"
"github.com/yaoapp/xiang/plugin"
"github.com/yaoapp/xiang/query"
"github.com/yaoapp/xiang/script"
"github.com/yaoapp/xiang/server"
"github.com/yaoapp/xiang/share"
"github.com/yaoapp/xiang/table"
Expand All @@ -40,6 +41,7 @@ func Load(cfg config.Config) {
query.Load(cfg) // 加载数据分析引擎

share.Load(cfg) // 加载共享库 lib
script.Load(cfg) // 加载JS处理器 script
model.Load(cfg) // 加载数据模型 model
flow.Load(cfg) // 加载业务逻辑 Flow
plugin.Load(cfg) // 加载业务插件 plugin
Expand Down
42 changes: 42 additions & 0 deletions script/script.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package script

import (
"fmt"
"path/filepath"

"github.com/yaoapp/gou"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/share"
)

// Load 加载共享库
func Load(cfg config.Config) error {
if share.BUILDIN {
return LoadBuildIn("scripts")
}
return LoadFrom(filepath.Join(cfg.Root, "scripts"))
}

// LoadBuildIn 从制品中读取
func LoadBuildIn(dir string) error {
return nil
}

// LoadFrom 从特定目录加载共享库
func LoadFrom(dir string) error {

if share.DirNotExists(dir) {
return fmt.Errorf("%s does not exists", dir)
}

// 加载共享脚本
err := share.Walk(dir, ".js", func(root, filename string) {
name := share.SpecName(root, filename)
err := gou.Yao.Load(filename, name)
if err != nil {
log.Error("加载脚本失败 %s", err.Error())
}
})
return err
}
20 changes: 20 additions & 0 deletions script/script_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package script

import (
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou"
)

func init() {
rootLib := path.Join(os.Getenv("YAO_DEV"), "/tests/scripts")
LoadFrom(rootLib)
}
func TestScript(t *testing.T) {
res, err := gou.Yao.New("time", "hello").Call("world")
assert.Nil(t, err)
assert.Equal(t, "name:world", res)
}
31 changes: 23 additions & 8 deletions share/importable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,44 @@ import (
"github.com/yaoapp/gou"
"github.com/yaoapp/gou/query/share"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/xlog"
)

// Libs 共享库
var Libs = map[string]map[string]interface{}{}

// Load 加载共享库
func Load(cfg config.Config) {
LoadFrom(filepath.Join(cfg.Root, "libs"))
func Load(cfg config.Config) error {
if BUILDIN {
return LoadBuildIn("libs")
}
return LoadFrom(filepath.Join(cfg.Root, "libs"))
}

// LoadBuildIn 从制品中读取
func LoadBuildIn(dir string) error {
return nil
}

// LoadFrom 从特定目录加载共享库
func LoadFrom(dir string) {
func LoadFrom(dir string) error {

if DirNotExists(dir) {
return
return fmt.Errorf("%s does not exists", dir)
}

// 加载共享数据
Walk(dir, ".json", func(root, filename string) {
err := Walk(dir, ".json", func(root, filename string) {
name := SpecName(root, filename)
content := ReadFile(filename)
libs := map[string]map[string]interface{}{}
err := jsoniter.Unmarshal(content, &libs)
if err != nil {
exception.New("共享数据结构异常 %s", 400, err).Throw()
log.Error("加载脚本失败 %s", err.Error())
return
}
for key, lib := range libs {
key := fmt.Sprintf("%s.%s", name, key)
Expand All @@ -47,14 +57,19 @@ func LoadFrom(dir string) {
}
})

if err != nil {
return err
}

// 加载共享脚本
Walk(dir, ".js", func(root, filename string) {
err = Walk(dir, ".js", func(root, filename string) {
name := SpecName(root, filename)
err := gou.Yao.Load(filename, name)
if err != nil {
xlog.Printf("加载脚本失败 %s", err.Error())
log.Error("加载脚本失败 %s", err.Error())
}
})
return err
}

// UnmarshalJSON Column 字段JSON解析
Expand Down
7 changes: 0 additions & 7 deletions share/importable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou"
)

func init() {
Expand Down Expand Up @@ -64,9 +63,3 @@ func TestAPI(t *testing.T) {
jsoniter.Unmarshal([]byte(content), &api)
assert.Equal(t, []interface{}{nil, nil, float64(10)}, api.Default)
}

func TestScript(t *testing.T) {
res, err := gou.Yao.New("time", "hello").Call("world")
assert.Nil(t, err)
assert.Equal(t, "name:world", res)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 292b10f

Please sign in to comment.