[add] migrate widgets.form
This commit is contained in:
parent
f1f58cc99f
commit
61c101d025
5 changed files with 149 additions and 89 deletions
|
|
@ -213,6 +213,6 @@ func exportAPI() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// load apis
|
// load apis
|
||||||
_, err = api.Load(string(source), "widgets.form")
|
_, err = api.LoadSource("<widget.form>.yao", source, "widgets.form")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,10 @@ func (fields *FieldsDSL) BindModel(m *model.Model) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if fields.Form == nil {
|
||||||
|
fields.Form = field.Columns{}
|
||||||
|
}
|
||||||
|
|
||||||
// append columns
|
// append columns
|
||||||
if _, has := fields.Form[formField.Key]; !has {
|
if _, has := fields.Form[formField.Key]; !has {
|
||||||
fields.Form[formField.Key] = *formField
|
fields.Form[formField.Key] = *formField
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -74,31 +74,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, "forms")
|
|
||||||
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{"*.form.yao", "*.form.json", "*.form.jsonc"}
|
||||||
id := prefix + share.ID(root, filename)
|
err := application.App.Walk("forms", 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"))
|
||||||
|
|
@ -107,31 +94,55 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
Forms[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, ".")
|
|
||||||
name := fmt.Sprintf("%s.form.json", dirs[len(dirs)-1])
|
file := filepath.Join("forms", share.File(id, ".form.yao"))
|
||||||
elems := []string{root}
|
if exists, _ := application.App.Exists(file); exists {
|
||||||
elems = append(elems, dirs[0:len(dirs)-1]...)
|
return LoadFile("forms", file)
|
||||||
elems = append(elems, "forms", name)
|
|
||||||
filename := filepath.Join(elems...)
|
|
||||||
data, err := environment.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("[Form] LoadID %s root=%s %s", id, root, err.Error())
|
|
||||||
}
|
}
|
||||||
return LoadData(data, id, root)
|
|
||||||
|
file = filepath.Join("forms", share.File(id, ".form.jsonc"))
|
||||||
|
if exists, _ := application.App.Exists(file); exists {
|
||||||
|
return LoadFile("forms", file)
|
||||||
|
}
|
||||||
|
|
||||||
|
file = filepath.Join("forms", share.File(id, ".form.json"))
|
||||||
|
if exists, _ := application.App.Exists(file); exists {
|
||||||
|
return LoadFile("forms", file)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("form %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
|
dsl.Root = root
|
||||||
|
|
||||||
err := jsoniter.Unmarshal(data, dsl)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("[Form] LoadData %s %s", id, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if dsl.Action == nil {
|
if dsl.Action == nil {
|
||||||
dsl.Action = &ActionDSL{}
|
dsl.Action = &ActionDSL{}
|
||||||
}
|
}
|
||||||
|
|
@ -146,7 +157,7 @@ func LoadData(data []byte, id string, root string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind model / store / table / ...
|
// Bind model / store / table / ...
|
||||||
err = dsl.Bind()
|
err := dsl.Bind()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("[Form] LoadData Bind %s %s", id, err.Error())
|
return fmt.Errorf("[Form] LoadData Bind %s %s", id, err.Error())
|
||||||
}
|
}
|
||||||
|
|
@ -198,6 +209,10 @@ func MustGet(form interface{}) *DSL {
|
||||||
// Xgen trans to xgen setting
|
// Xgen trans to xgen setting
|
||||||
func (dsl *DSL) Xgen(data map[string]interface{}, excludes map[string]bool) (map[string]interface{}, error) {
|
func (dsl *DSL) Xgen(data map[string]interface{}, excludes map[string]bool) (map[string]interface{}, error) {
|
||||||
|
|
||||||
|
if dsl.Config == nil {
|
||||||
|
dsl.Config = map[string]interface{}{}
|
||||||
|
}
|
||||||
|
|
||||||
if dsl.Layout == nil {
|
if dsl.Layout == nil {
|
||||||
dsl.Layout = &LayoutDSL{Form: &ViewLayoutDSL{}}
|
dsl.Layout = &LayoutDSL{Form: &ViewLayoutDSL{}}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,24 @@
|
||||||
package form
|
package form
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
"github.com/yaoapp/yao/fs"
|
"github.com/yaoapp/yao/flow"
|
||||||
"github.com/yaoapp/yao/i18n"
|
"github.com/yaoapp/yao/test"
|
||||||
"github.com/yaoapp/yao/model"
|
"github.com/yaoapp/yao/widgets/app"
|
||||||
"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 {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
@ -25,31 +26,28 @@ func TestLoad(t *testing.T) {
|
||||||
assert.Equal(t, 10, len(Forms))
|
assert.Equal(t, 10, len(Forms))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadID(t *testing.T) {
|
||||||
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
prepare(t)
|
||||||
|
|
||||||
|
err := LoadID("pet", filepath.Join(config.Conf.Root))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func prepare(t *testing.T, language ...string) {
|
func prepare(t *testing.T, language ...string) {
|
||||||
|
|
||||||
// runtime.Load(config.Conf)
|
// load flows
|
||||||
err := test.LoadEngine(language...)
|
err := flow.Load(config.Conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
i18n.Load(config.Conf)
|
// load app
|
||||||
share.DBConnect(config.Conf.DB) // removed later
|
err = app.LoadAndExport(config.Conf)
|
||||||
|
|
||||||
// load fs
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
@ -72,6 +70,11 @@ func prepare(t *testing.T, language ...string) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = Load(config.Conf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
// export
|
// export
|
||||||
err = Export()
|
err = Export()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,15 @@ import (
|
||||||
"github.com/yaoapp/kun/maps"
|
"github.com/yaoapp/kun/maps"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
"github.com/yaoapp/yao/helper"
|
"github.com/yaoapp/yao/helper"
|
||||||
q "github.com/yaoapp/yao/query"
|
"github.com/yaoapp/yao/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProcessFind(t *testing.T) {
|
func TestProcessFind(t *testing.T) {
|
||||||
|
|
||||||
load(t)
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
|
|
||||||
|
|
@ -36,7 +39,11 @@ func TestProcessFind(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessSave(t *testing.T) {
|
func TestProcessSave(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
args := []interface{}{"pet", map[string]interface{}{
|
args := []interface{}{"pet", map[string]interface{}{
|
||||||
|
|
@ -65,7 +72,11 @@ func TestProcessSave(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessCreate(t *testing.T) {
|
func TestProcessCreate(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
args := []interface{}{"pet", map[string]interface{}{
|
args := []interface{}{"pet", map[string]interface{}{
|
||||||
|
|
@ -95,7 +106,11 @@ func TestProcessCreate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessUpdate(t *testing.T) {
|
func TestProcessUpdate(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
args := []interface{}{"pet", 1, map[string]interface{}{
|
args := []interface{}{"pet", 1, map[string]interface{}{
|
||||||
|
|
@ -123,7 +138,11 @@ func TestProcessUpdate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessDelete(t *testing.T) {
|
func TestProcessDelete(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
args := []interface{}{"pet", 1}
|
args := []interface{}{"pet", 1}
|
||||||
|
|
@ -138,7 +157,11 @@ func TestProcessDelete(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessComponent(t *testing.T) {
|
func TestProcessComponent(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
args := []interface{}{
|
args := []interface{}{
|
||||||
|
|
@ -163,7 +186,11 @@ func TestProcessComponent(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessComponentError(t *testing.T) {
|
func TestProcessComponentError(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
args := []interface{}{
|
args := []interface{}{
|
||||||
|
|
@ -177,7 +204,11 @@ func TestProcessComponentError(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessUpload(t *testing.T) {
|
func TestProcessUpload(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
args := []interface{}{
|
args := []interface{}{
|
||||||
|
|
@ -198,7 +229,11 @@ func TestProcessUpload(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessDownload(t *testing.T) {
|
func TestProcessDownload(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
|
|
||||||
|
|
@ -222,7 +257,11 @@ func TestProcessDownload(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessSetting(t *testing.T) {
|
func TestProcessSetting(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
args := []interface{}{"pet"}
|
args := []interface{}{"pet"}
|
||||||
|
|
@ -239,7 +278,11 @@ func TestProcessSetting(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessXgen(t *testing.T) {
|
func TestProcessXgen(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
args := []interface{}{"pet"}
|
args := []interface{}{"pet"}
|
||||||
|
|
@ -256,7 +299,11 @@ func TestProcessXgen(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessXgenWithPermissions(t *testing.T) {
|
func TestProcessXgenWithPermissions(t *testing.T) {
|
||||||
load(t)
|
|
||||||
|
test.Prepare(t, config.Conf)
|
||||||
|
defer test.Clean()
|
||||||
|
|
||||||
|
prepare(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
testData(t)
|
testData(t)
|
||||||
|
|
||||||
|
|
@ -295,15 +342,6 @@ func TestProcessXgenWithPermissions(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func load(t *testing.T) {
|
|
||||||
prepare(t)
|
|
||||||
err := Load(config.Conf)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
q.Load(config.Conf)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testData(t *testing.T) {
|
func testData(t *testing.T) {
|
||||||
pet := model.Select("pet")
|
pet := model.Select("pet")
|
||||||
err := pet.Insert(
|
err := pet.Insert(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue