From 61c101d025fc5a33c6730190721efbac6c5b6143 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 25 Mar 2023 18:39:16 +0800 Subject: [PATCH] [add] migrate widgets.form --- widgets/form/api.go | 2 +- widgets/form/fields.go | 4 ++ widgets/form/form.go | 97 +++++++++++++++++++++--------------- widgets/form/form_test.go | 53 ++++++++++---------- widgets/form/process_test.go | 82 ++++++++++++++++++++++-------- 5 files changed, 149 insertions(+), 89 deletions(-) diff --git a/widgets/form/api.go b/widgets/form/api.go index e763690b..cbe2de0b 100644 --- a/widgets/form/api.go +++ b/widgets/form/api.go @@ -213,6 +213,6 @@ func exportAPI() error { } // load apis - _, err = api.Load(string(source), "widgets.form") + _, err = api.LoadSource(".yao", source, "widgets.form") return err } diff --git a/widgets/form/fields.go b/widgets/form/fields.go index f9cbe289..f0b3b620 100644 --- a/widgets/form/fields.go +++ b/widgets/form/fields.go @@ -27,6 +27,10 @@ func (fields *FieldsDSL) BindModel(m *model.Model) error { return err } + if fields.Form == nil { + fields.Form = field.Columns{} + } + // append columns if _, has := fields.Form[formField.Key]; !has { fields.Form[formField.Key] = *formField diff --git a/widgets/form/form.go b/widgets/form/form.go index 1cc0386c..5ec4fdfa 100644 --- a/widgets/form/form.go +++ b/widgets/form/form.go @@ -6,13 +6,13 @@ import ( "strings" jsoniter "github.com/json-iterator/go" + "github.com/yaoapp/gou/application" "github.com/yaoapp/gou/process" "github.com/yaoapp/kun/exception" "github.com/yaoapp/kun/log" "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/share" "github.com/yaoapp/yao/widgets/component" - "github.com/yaoapp/yao/widgets/environment" "github.com/yaoapp/yao/widgets/field" ) @@ -74,31 +74,18 @@ func LoadAndExport(cfg config.Config) error { // Load load task 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{} - err := share.Walk(dir, ".json", func(root, filename string) { - id := prefix + share.ID(root, filename) - data, err := environment.ReadFile(filename) - if err != nil { + exts := []string{"*.form.yao", "*.form.json", "*.form.jsonc"} + err := application.App.Walk("forms", func(root, file string, isdir bool) error { + if isdir { + return nil + } + if err := LoadFile(root, file); err != nil { messages = append(messages, err.Error()) - return } - err = LoadData(data, id, filepath.Dir(dir)) - if err != nil { - messages = append(messages, err.Error()) - } - }) + return nil + }, exts...) if len(messages) > 0 { return fmt.Errorf(strings.Join(messages, ";\n")) @@ -107,31 +94,55 @@ func LoadFrom(dir string, prefix string) error { 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 func LoadID(id string, root string) error { - dirs := strings.Split(id, ".") - name := fmt.Sprintf("%s.form.json", dirs[len(dirs)-1]) - elems := []string{root} - elems = append(elems, dirs[0:len(dirs)-1]...) - 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()) + + file := filepath.Join("forms", share.File(id, ".form.yao")) + if exists, _ := application.App.Exists(file); exists { + return LoadFile("forms", file) } - 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 -func LoadData(data []byte, id string, root string) error { - dsl := New(id) +func (dsl *DSL) parse(id string, root string) error { + 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 { dsl.Action = &ActionDSL{} } @@ -146,7 +157,7 @@ func LoadData(data []byte, id string, root string) error { } // Bind model / store / table / ... - err = dsl.Bind() + err := dsl.Bind() if err != nil { 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 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 { dsl.Layout = &LayoutDSL{Form: &ViewLayoutDSL{}} } diff --git a/widgets/form/form_test.go b/widgets/form/form_test.go index 5f965ce4..cb2db57f 100644 --- a/widgets/form/form_test.go +++ b/widgets/form/form_test.go @@ -1,23 +1,24 @@ package form import ( + "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/yaoapp/yao/config" - "github.com/yaoapp/yao/fs" - "github.com/yaoapp/yao/i18n" - "github.com/yaoapp/yao/model" - "github.com/yaoapp/yao/script" - "github.com/yaoapp/yao/share" + "github.com/yaoapp/yao/flow" + "github.com/yaoapp/yao/test" + "github.com/yaoapp/yao/widgets/app" "github.com/yaoapp/yao/widgets/expression" "github.com/yaoapp/yao/widgets/field" "github.com/yaoapp/yao/widgets/table" - "github.com/yaoapp/yao/widgets/test" ) func TestLoad(t *testing.T) { + test.Prepare(t, config.Conf) + defer test.Clean() prepare(t) + err := Load(config.Conf) if err != nil { t.Fatal(err) @@ -25,31 +26,28 @@ func TestLoad(t *testing.T) { 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) { - // runtime.Load(config.Conf) - err := test.LoadEngine(language...) + // load flows + err := flow.Load(config.Conf) if err != nil { t.Fatal(err) } - i18n.Load(config.Conf) - share.DBConnect(config.Conf.DB) // removed later - - // 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) + // load app + err = app.LoadAndExport(config.Conf) if err != nil { t.Fatal(err) } @@ -72,6 +70,11 @@ func prepare(t *testing.T, language ...string) { t.Fatal(err) } + err = Load(config.Conf) + if err != nil { + t.Fatal(err) + } + // export err = Export() if err != nil { diff --git a/widgets/form/process_test.go b/widgets/form/process_test.go index 3ba8271b..7e7e3fe9 100644 --- a/widgets/form/process_test.go +++ b/widgets/form/process_test.go @@ -16,12 +16,15 @@ import ( "github.com/yaoapp/kun/maps" "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/helper" - q "github.com/yaoapp/yao/query" + "github.com/yaoapp/yao/test" ) func TestProcessFind(t *testing.T) { - load(t) + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) @@ -36,7 +39,11 @@ func TestProcessFind(t *testing.T) { } func TestProcessSave(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) args := []interface{}{"pet", map[string]interface{}{ @@ -65,7 +72,11 @@ func TestProcessSave(t *testing.T) { } func TestProcessCreate(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) args := []interface{}{"pet", map[string]interface{}{ @@ -95,7 +106,11 @@ func TestProcessCreate(t *testing.T) { } func TestProcessUpdate(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) args := []interface{}{"pet", 1, map[string]interface{}{ @@ -123,7 +138,11 @@ func TestProcessUpdate(t *testing.T) { } func TestProcessDelete(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) args := []interface{}{"pet", 1} @@ -138,7 +157,11 @@ func TestProcessDelete(t *testing.T) { } func TestProcessComponent(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) args := []interface{}{ @@ -163,7 +186,11 @@ func TestProcessComponent(t *testing.T) { } func TestProcessComponentError(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) args := []interface{}{ @@ -177,7 +204,11 @@ func TestProcessComponentError(t *testing.T) { } func TestProcessUpload(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) args := []interface{}{ @@ -198,7 +229,11 @@ func TestProcessUpload(t *testing.T) { } func TestProcessDownload(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) @@ -222,7 +257,11 @@ func TestProcessDownload(t *testing.T) { } func TestProcessSetting(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) args := []interface{}{"pet"} @@ -239,7 +278,11 @@ func TestProcessSetting(t *testing.T) { } func TestProcessXgen(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(t) testData(t) args := []interface{}{"pet"} @@ -256,7 +299,11 @@ func TestProcessXgen(t *testing.T) { } func TestProcessXgenWithPermissions(t *testing.T) { - load(t) + + test.Prepare(t, config.Conf) + defer test.Clean() + + prepare(t) clear(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) { pet := model.Select("pet") err := pet.Insert(