From a258dfa787f7aacd257aafea4fa3d03f8cd0f795 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 3 Nov 2024 07:53:25 +0800 Subject: [PATCH] Refactor component property handling and add deep copy functionality --- widgets/component/component.go | 29 ++++++++++++++++++++++++++++- widgets/dashboard/dashboard_test.go | 3 +++ widgets/dashboard/process_test.go | 17 +++++++---------- widgets/table/process_test.go | 20 +++++++------------- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/widgets/component/component.go b/widgets/component/component.go index 43142f77..68617723 100644 --- a/widgets/component/component.go +++ b/widgets/component/component.go @@ -105,7 +105,7 @@ func (dsl *DSL) Parse() { for key, val := range BackendOnlyProps[t] { if dsl.Props.Has(key) { for k, v := range val { - dsl.Props[k] = v + dsl.Props[k] = dsl.copy(v) } } } @@ -127,3 +127,30 @@ func (dsl *DSL) Clone() *DSL { } return &new } + +// Copy the component properties +func (dsl *DSL) copy(v interface{}) interface{} { + var res interface{} = nil + switch v.(type) { + case map[string]interface{}: + // Clone the map + new := map[string]interface{}{} + for k1, v1 := range v.(map[string]interface{}) { + new[k1] = v1 + } + res = new + + case []interface{}: + // Clone the array + new := []interface{}{} + for _, v1 := range v.([]interface{}) { + new = append(new, dsl.copy(v1)) + } + res = new + + default: + res = v + } + + return res +} diff --git a/widgets/dashboard/dashboard_test.go b/widgets/dashboard/dashboard_test.go index d9d7b947..1a4e6cf4 100644 --- a/widgets/dashboard/dashboard_test.go +++ b/widgets/dashboard/dashboard_test.go @@ -8,6 +8,7 @@ import ( "github.com/yaoapp/yao/flow" "github.com/yaoapp/yao/i18n" "github.com/yaoapp/yao/test" + "github.com/yaoapp/yao/widgets/component" ) func TestLoad(t *testing.T) { @@ -40,4 +41,6 @@ func prepare(t *testing.T, language ...string) { if err != nil { t.Fatal(err) } + + component.Export() } diff --git a/widgets/dashboard/process_test.go b/widgets/dashboard/process_test.go index 8ac38a9c..cd597ce5 100644 --- a/widgets/dashboard/process_test.go +++ b/widgets/dashboard/process_test.go @@ -1,7 +1,6 @@ package dashboard import ( - "fmt" "net/url" "testing" @@ -10,9 +9,9 @@ import ( "github.com/yaoapp/gou/process" "github.com/yaoapp/gou/session" "github.com/yaoapp/kun/any" - "github.com/yaoapp/kun/maps" "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/test" + "github.com/yaoapp/yao/widgets/component" ) func TestProcessData(t *testing.T) { @@ -33,12 +32,12 @@ func TestProcessComponent(t *testing.T) { test.Prepare(t, config.Conf) defer test.Clean() prepare(t) + testData(t) args := []interface{}{ "workspace", "fields.filter.状态.edit.props.xProps", "remote", - map[string]interface{}{"select": []string{"name", "status"}, "limit": 2}, } res, err := process.New("yao.dashboard.Component", args...).Exec() @@ -46,15 +45,13 @@ func TestProcessComponent(t *testing.T) { t.Fatal(err) } - fmt.Println(res) - - pets, ok := res.([]maps.MapStr) + pets, ok := res.([]component.Option) assert.True(t, ok) assert.Equal(t, 2, len(pets)) - assert.Equal(t, "Cookie", pets[0]["name"]) - assert.Equal(t, "checked", pets[0]["status"]) - assert.Equal(t, "Baby", pets[1]["name"]) - assert.Equal(t, "checked", pets[1]["status"]) + assert.Equal(t, "Cookie", pets[0].Label) + assert.Equal(t, "checked", pets[0].Value) + assert.Equal(t, "Baby", pets[1].Label) + assert.Equal(t, "checked", pets[1].Value) args = []interface{}{ "workspace", diff --git a/widgets/table/process_test.go b/widgets/table/process_test.go index 1f7c283c..883e0886 100644 --- a/widgets/table/process_test.go +++ b/widgets/table/process_test.go @@ -17,6 +17,7 @@ import ( "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/helper" "github.com/yaoapp/yao/test" + "github.com/yaoapp/yao/widgets/component" ) func TestProcessSearch(t *testing.T) { @@ -364,13 +365,6 @@ func TestProcessComponent(t *testing.T) { "pet", "fields.filter.状态.edit.props.xProps", "remote", - map[string]interface{}{ - "model": "pet", - "label": "name", - "value": "status", - "wheres[]": `{"column":"id","op":"ge","value":0}`, - "limit": "2", - }, } res, err := process.New("yao.table.Component", args...).Exec() @@ -378,13 +372,13 @@ func TestProcessComponent(t *testing.T) { t.Fatal(err) } - pets, ok := res.([]map[string]interface{}) + pets, ok := res.([]component.Option) assert.True(t, ok) assert.Equal(t, 2, len(pets)) - assert.Equal(t, "Cookie", pets[0]["label"]) - assert.Equal(t, "checked", pets[0]["value"]) - assert.Equal(t, "Baby", pets[1]["label"]) - assert.Equal(t, "checked", pets[1]["value"]) + assert.Equal(t, "Cookie", pets[0].Label) + assert.Equal(t, "checked", pets[0].Value) + assert.Equal(t, "Baby", pets[1].Label) + assert.Equal(t, "checked", pets[1].Value) } func TestProcessComponentError(t *testing.T) { @@ -525,7 +519,7 @@ func TestProcessXgenWithPermissions(t *testing.T) { session.Global().Set("__permissions", map[string]interface{}{ "tables.pet": []string{ "8ca9bdf0fa2cbc8f1018f8566ed6ab5e", // fields.table.消费金额 - "f03f1ae60c46dd6cdeda87b919a51d7e", // fields.filter.状态 + "c5b1f06582e1dff3ac6d16822fdadd54", // fields.filter.状态 "b1483ade34cd51261817558114e74e3f", // filter.actions[0] 添加宠物 "e6a67850312980e8372e550c5b361097", // operation.actions[0] 查看 },