[add] migrate widgets.list

This commit is contained in:
Max 2023-03-25 21:45:04 +08:00
parent ed48fa468a
commit 3fb186f5e0
4 changed files with 65 additions and 68 deletions

View file

@ -169,6 +169,6 @@ func exportAPI() error {
} }
// load apis // load apis
_, err = api.Load(string(source), "widgets.list") _, err = api.LoadSource("<widget.list>.yao", source, "widgets.list")
return err return err
} }

View file

@ -6,13 +6,13 @@ import (
"strings" "strings"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou/application"
"github.com/yaoapp/gou/process" "github.com/yaoapp/gou/process"
"github.com/yaoapp/kun/exception" "github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log" "github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share" "github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/widgets/component" "github.com/yaoapp/yao/widgets/component"
"github.com/yaoapp/yao/widgets/environment"
"github.com/yaoapp/yao/widgets/field" "github.com/yaoapp/yao/widgets/field"
) )
@ -66,31 +66,18 @@ func LoadAndExport(cfg config.Config) error {
// Load load task // Load load task
func Load(cfg config.Config) error { func Load(cfg config.Config) error {
var root = filepath.Join(cfg.Root, "lists")
return LoadFrom(root, "")
}
// LoadFrom load from dir
func LoadFrom(dir string, prefix string) error {
if share.DirNotExists(dir) {
return fmt.Errorf("%s does not exists", dir)
}
messages := []string{} messages := []string{}
err := share.Walk(dir, ".json", func(root, filename string) { exts := []string{"*.yao", "*.json", "*.jsonc"}
id := prefix + share.ID(root, filename) err := application.App.Walk("lists", func(root, file string, isdir bool) error {
data, err := environment.ReadFile(filename) if isdir {
if err != nil { return nil
}
if err := LoadFile(root, file); err != nil {
messages = append(messages, err.Error()) messages = append(messages, err.Error())
return
} }
err = LoadData(data, id, filepath.Dir(dir)) return nil
if err != nil { }, exts...)
messages = append(messages, err.Error())
}
})
if len(messages) > 0 { if len(messages) > 0 {
return fmt.Errorf(strings.Join(messages, ";\n")) return fmt.Errorf(strings.Join(messages, ";\n"))
@ -99,30 +86,52 @@ func LoadFrom(dir string, prefix string) error {
return err return err
} }
// LoadFile load table dsl by file
func LoadFile(root string, file string) error {
id := share.ID(root, file)
data, err := application.App.Read(file)
if err != nil {
return err
}
dsl := New(id)
err = application.Parse(file, data, dsl)
if err != nil {
return fmt.Errorf("[%s] %s", id, err.Error())
}
err = dsl.parse(id, root)
if err != nil {
return err
}
Lists[id] = dsl
return nil
}
// LoadID load via id // LoadID load via id
func LoadID(id string, root string) error { func LoadID(id string, root string) error {
dirs := strings.Split(id, ".") file := filepath.Join("lists", share.File(id, ".yao"))
name := fmt.Sprintf("%s.list.json", dirs[len(dirs)-1]) if exists, _ := application.App.Exists(file); exists {
elems := []string{root} return LoadFile("lists", file)
elems = append(elems, dirs[0:len(dirs)-1]...)
elems = append(elems, "lists", name)
filename := filepath.Join(elems...)
data, err := environment.ReadFile(filename)
if err != nil {
return fmt.Errorf("[List] LoadID %s root=%s %s", id, root, err.Error())
} }
return LoadData(data, id, root)
file = filepath.Join("lists", share.File(id, ".jsonc"))
if exists, _ := application.App.Exists(file); exists {
return LoadFile("lists", file)
}
file = filepath.Join("lists", share.File(id, ".json"))
if exists, _ := application.App.Exists(file); exists {
return LoadFile("lists", file)
}
return fmt.Errorf("list %s not found", id)
} }
// LoadData load via data // LoadData load via data
func LoadData(data []byte, id string, root string) error { func (dsl *DSL) parse(id string, root string) error {
dsl := New(id)
dsl.Root = root
err := jsoniter.Unmarshal(data, dsl)
if err != nil {
return fmt.Errorf("[List] LoadData %s %s", id, err.Error())
}
if dsl.Action == nil { if dsl.Action == nil {
dsl.Action = &ActionDSL{} dsl.Action = &ActionDSL{}
@ -138,7 +147,7 @@ func LoadData(data []byte, id string, root string) error {
} }
// Bind model / store / list / ... // Bind model / store / list / ...
err = dsl.Bind() err := dsl.Bind()
if err != nil { if err != nil {
return fmt.Errorf("[List] LoadData Bind %s %s", id, err.Error()) return fmt.Errorf("[List] LoadData Bind %s %s", id, err.Error())
} }

View file

@ -7,16 +7,15 @@ import (
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/fs" "github.com/yaoapp/yao/fs"
"github.com/yaoapp/yao/i18n" "github.com/yaoapp/yao/i18n"
"github.com/yaoapp/yao/model" "github.com/yaoapp/yao/test"
"github.com/yaoapp/yao/script"
"github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/widgets/expression" "github.com/yaoapp/yao/widgets/expression"
"github.com/yaoapp/yao/widgets/field" "github.com/yaoapp/yao/widgets/field"
"github.com/yaoapp/yao/widgets/table" "github.com/yaoapp/yao/widgets/table"
"github.com/yaoapp/yao/widgets/test"
) )
func TestLoad(t *testing.T) { func TestLoad(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
prepare(t) prepare(t)
err := Load(config.Conf) err := Load(config.Conf)
if err != nil { if err != nil {
@ -27,29 +26,10 @@ func TestLoad(t *testing.T) {
func prepare(t *testing.T, language ...string) { func prepare(t *testing.T, language ...string) {
// runtime.Load(config.Conf)
err := test.LoadEngine(language...)
if err != nil {
t.Fatal(err)
}
i18n.Load(config.Conf) i18n.Load(config.Conf)
share.DBConnect(config.Conf.DB) // removed later
// load fs // load fs
err = fs.Load(config.Conf) err := fs.Load(config.Conf)
if err != nil {
t.Fatal(err)
}
// load scripts
err = script.Load(config.Conf)
if err != nil {
t.Fatal(err)
}
// load models
err = model.Load(config.Conf)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -11,10 +11,13 @@ import (
"github.com/yaoapp/gou/session" "github.com/yaoapp/gou/session"
"github.com/yaoapp/kun/any" "github.com/yaoapp/kun/any"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
q "github.com/yaoapp/yao/query" "github.com/yaoapp/yao/test"
) )
func TestProcessSetting(t *testing.T) { func TestProcessSetting(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
load(t) load(t)
clear(t) clear(t)
testData(t) testData(t)
@ -30,6 +33,9 @@ func TestProcessSetting(t *testing.T) {
} }
func TestProcessXgen(t *testing.T) { func TestProcessXgen(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
load(t) load(t)
clear(t) clear(t)
testData(t) testData(t)
@ -45,6 +51,9 @@ func TestProcessXgen(t *testing.T) {
} }
func TestProcessXgenWithPermissions(t *testing.T) { func TestProcessXgenWithPermissions(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
load(t) load(t)
clear(t) clear(t)
testData(t) testData(t)
@ -85,7 +94,6 @@ func load(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
q.Load(config.Conf)
} }
func testData(t *testing.T) { func testData(t *testing.T) {