Refactor component property handling and add deep copy functionality

This commit is contained in:
Max 2024-11-03 07:53:25 +08:00
parent bb717a938a
commit a258dfa787
4 changed files with 45 additions and 24 deletions

View file

@ -105,7 +105,7 @@ func (dsl *DSL) Parse() {
for key, val := range BackendOnlyProps[t] { for key, val := range BackendOnlyProps[t] {
if dsl.Props.Has(key) { if dsl.Props.Has(key) {
for k, v := range val { 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 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
}

View file

@ -8,6 +8,7 @@ import (
"github.com/yaoapp/yao/flow" "github.com/yaoapp/yao/flow"
"github.com/yaoapp/yao/i18n" "github.com/yaoapp/yao/i18n"
"github.com/yaoapp/yao/test" "github.com/yaoapp/yao/test"
"github.com/yaoapp/yao/widgets/component"
) )
func TestLoad(t *testing.T) { func TestLoad(t *testing.T) {
@ -40,4 +41,6 @@ func prepare(t *testing.T, language ...string) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
component.Export()
} }

View file

@ -1,7 +1,6 @@
package dashboard package dashboard
import ( import (
"fmt"
"net/url" "net/url"
"testing" "testing"
@ -10,9 +9,9 @@ import (
"github.com/yaoapp/gou/process" "github.com/yaoapp/gou/process"
"github.com/yaoapp/gou/session" "github.com/yaoapp/gou/session"
"github.com/yaoapp/kun/any" "github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/test" "github.com/yaoapp/yao/test"
"github.com/yaoapp/yao/widgets/component"
) )
func TestProcessData(t *testing.T) { func TestProcessData(t *testing.T) {
@ -33,12 +32,12 @@ func TestProcessComponent(t *testing.T) {
test.Prepare(t, config.Conf) test.Prepare(t, config.Conf)
defer test.Clean() defer test.Clean()
prepare(t) prepare(t)
testData(t)
args := []interface{}{ args := []interface{}{
"workspace", "workspace",
"fields.filter.状态.edit.props.xProps", "fields.filter.状态.edit.props.xProps",
"remote", "remote",
map[string]interface{}{"select": []string{"name", "status"}, "limit": 2},
} }
res, err := process.New("yao.dashboard.Component", args...).Exec() res, err := process.New("yao.dashboard.Component", args...).Exec()
@ -46,15 +45,13 @@ func TestProcessComponent(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
fmt.Println(res) pets, ok := res.([]component.Option)
pets, ok := res.([]maps.MapStr)
assert.True(t, ok) assert.True(t, ok)
assert.Equal(t, 2, len(pets)) assert.Equal(t, 2, len(pets))
assert.Equal(t, "Cookie", pets[0]["name"]) assert.Equal(t, "Cookie", pets[0].Label)
assert.Equal(t, "checked", pets[0]["status"]) assert.Equal(t, "checked", pets[0].Value)
assert.Equal(t, "Baby", pets[1]["name"]) assert.Equal(t, "Baby", pets[1].Label)
assert.Equal(t, "checked", pets[1]["status"]) assert.Equal(t, "checked", pets[1].Value)
args = []interface{}{ args = []interface{}{
"workspace", "workspace",

View file

@ -17,6 +17,7 @@ import (
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/helper" "github.com/yaoapp/yao/helper"
"github.com/yaoapp/yao/test" "github.com/yaoapp/yao/test"
"github.com/yaoapp/yao/widgets/component"
) )
func TestProcessSearch(t *testing.T) { func TestProcessSearch(t *testing.T) {
@ -364,13 +365,6 @@ func TestProcessComponent(t *testing.T) {
"pet", "pet",
"fields.filter.状态.edit.props.xProps", "fields.filter.状态.edit.props.xProps",
"remote", "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() res, err := process.New("yao.table.Component", args...).Exec()
@ -378,13 +372,13 @@ func TestProcessComponent(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
pets, ok := res.([]map[string]interface{}) pets, ok := res.([]component.Option)
assert.True(t, ok) assert.True(t, ok)
assert.Equal(t, 2, len(pets)) assert.Equal(t, 2, len(pets))
assert.Equal(t, "Cookie", pets[0]["label"]) assert.Equal(t, "Cookie", pets[0].Label)
assert.Equal(t, "checked", pets[0]["value"]) assert.Equal(t, "checked", pets[0].Value)
assert.Equal(t, "Baby", pets[1]["label"]) assert.Equal(t, "Baby", pets[1].Label)
assert.Equal(t, "checked", pets[1]["value"]) assert.Equal(t, "checked", pets[1].Value)
} }
func TestProcessComponentError(t *testing.T) { func TestProcessComponentError(t *testing.T) {
@ -525,7 +519,7 @@ func TestProcessXgenWithPermissions(t *testing.T) {
session.Global().Set("__permissions", map[string]interface{}{ session.Global().Set("__permissions", map[string]interface{}{
"tables.pet": []string{ "tables.pet": []string{
"8ca9bdf0fa2cbc8f1018f8566ed6ab5e", // fields.table.消费金额 "8ca9bdf0fa2cbc8f1018f8566ed6ab5e", // fields.table.消费金额
"f03f1ae60c46dd6cdeda87b919a51d7e", // fields.filter.状态 "c5b1f06582e1dff3ac6d16822fdadd54", // fields.filter.状态
"b1483ade34cd51261817558114e74e3f", // filter.actions[0] 添加宠物 "b1483ade34cd51261817558114e74e3f", // filter.actions[0] 添加宠物
"e6a67850312980e8372e550c5b361097", // operation.actions[0] 查看 "e6a67850312980e8372e550c5b361097", // operation.actions[0] 查看
}, },