From 7320b7a5b6f5b629dce5afff54c0088630db3f25 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 1 Nov 2022 16:52:04 +0800 Subject: [PATCH] [add] widget compute $C(id) --- widgets/chart/compute.go | 16 ++++++++-------- widgets/component/compute.go | 1 + widgets/compute/compute.go | 20 +++++++++++--------- widgets/form/compute.go | 8 ++++---- widgets/table/compute.go | 16 ++++++++-------- widgets/table/compute_test.go | 6 +++--- 6 files changed, 35 insertions(+), 32 deletions(-) diff --git a/widgets/chart/compute.go b/widgets/chart/compute.go index 6e3b4b56..0d4001a4 100644 --- a/widgets/chart/compute.go +++ b/widgets/chart/compute.go @@ -7,23 +7,23 @@ import ( "github.com/yaoapp/yao/widgets/field" ) -func (dsl *DSL) getField() func(string) (*field.ColumnDSL, string, error) { - return func(name string) (*field.ColumnDSL, string, error) { +func (dsl *DSL) getField() func(string) (*field.ColumnDSL, string, string, error) { + return func(name string) (*field.ColumnDSL, string, string, error) { field, has := dsl.Fields.Chart[name] if !has { - return nil, "fields.chart", fmt.Errorf("fields.chart.%s does not exist", name) + return nil, "fields.chart", dsl.ID, fmt.Errorf("fields.chart.%s does not exist", name) } - return &field, "fields.chart", nil + return &field, "fields.chart", dsl.ID, nil } } -func (dsl *DSL) getFilter() func(string) (*field.FilterDSL, string, error) { - return func(name string) (*field.FilterDSL, string, error) { +func (dsl *DSL) getFilter() func(string) (*field.FilterDSL, string, string, error) { + return func(name string) (*field.FilterDSL, string, string, error) { field, has := dsl.Fields.Filter[name] if !has { - return nil, "fields.filter", fmt.Errorf("fields.filter.%s does not exist", name) + return nil, "fields.filter", dsl.ID, fmt.Errorf("fields.filter.%s does not exist", name) } - return &field, "fields.filter", nil + return &field, "fields.filter", dsl.ID, nil } } diff --git a/widgets/component/compute.go b/widgets/component/compute.go index b53f261e..82ba4656 100644 --- a/widgets/component/compute.go +++ b/widgets/component/compute.go @@ -15,6 +15,7 @@ var defaults = []CArg{ {IsExp: true, key: "value", value: nil}, {IsExp: true, key: "props", value: nil}, {IsExp: true, key: "type", value: nil}, + {IsExp: true, key: "id", value: nil}, } // Value compute value diff --git a/widgets/compute/compute.go b/widgets/compute/compute.go index 551a5a8b..5e672f50 100644 --- a/widgets/compute/compute.go +++ b/widgets/compute/compute.go @@ -14,7 +14,7 @@ import ( var views = map[string]bool{"find": true, "get": true, "search": true, "data": true} // ComputeEdit edit compute edit -func (c *Computable) ComputeEdit(name string, process *gou.Process, args []interface{}, getField func(string) (*field.ColumnDSL, string, error)) error { +func (c *Computable) ComputeEdit(name string, process *gou.Process, args []interface{}, getField func(string) (*field.ColumnDSL, string, string, error)) error { namer := strings.Split(strings.ToLower(name), ".") name = namer[len(namer)-1] @@ -86,7 +86,7 @@ func (c *Computable) ComputeEdit(name string, process *gou.Process, args []inter } // EditRow edit row -func (c *Computable) editRow(process *gou.Process, res map[string]interface{}, getField func(string) (*field.ColumnDSL, string, error)) error { +func (c *Computable) editRow(process *gou.Process, res map[string]interface{}, getField func(string) (*field.ColumnDSL, string, string, error)) error { messages := []string{} row := maps.MapOf(res).Dot() @@ -94,12 +94,13 @@ func (c *Computable) editRow(process *gou.Process, res map[string]interface{}, g for key := range res { if computes, has := c.Computes.Edit[key]; has { unit := computes[0] - field, path, err := getField(unit.Name) + field, path, id, err := getField(unit.Name) if err != nil { messages = append(messages, err.Error()) continue } + data.Set("id", id) data.Set("value", res[key]) data.Merge(any.MapOf(field.Edit.Map()).MapStrAny.Dot()) new, err := field.Edit.Compute.Value(data, process.Sid, process.Global) @@ -119,7 +120,7 @@ func (c *Computable) editRow(process *gou.Process, res map[string]interface{}, g } // EditRows edit row -func (c *Computable) editRows(process *gou.Process, columns []string, res [][]interface{}, getField func(string) (*field.ColumnDSL, string, error)) error { +func (c *Computable) editRows(process *gou.Process, columns []string, res [][]interface{}, getField func(string) (*field.ColumnDSL, string, string, error)) error { messages := []string{} keys := map[string]int{} @@ -155,7 +156,7 @@ func (c *Computable) editRows(process *gou.Process, columns []string, res [][]in } // ComputeView view view -func (c *Computable) ComputeView(name string, process *gou.Process, res interface{}, getField func(string) (*field.ColumnDSL, string, error)) error { +func (c *Computable) ComputeView(name string, process *gou.Process, res interface{}, getField func(string) (*field.ColumnDSL, string, string, error)) error { namer := strings.Split(strings.ToLower(name), ".") name = namer[len(namer)-1] @@ -178,7 +179,7 @@ func (c *Computable) ComputeView(name string, process *gou.Process, res interfac } // ViewRows viewrows -func (c *Computable) viewRows(name string, process *gou.Process, res interface{}, getField func(string) (*field.ColumnDSL, string, error)) error { +func (c *Computable) viewRows(name string, process *gou.Process, res interface{}, getField func(string) (*field.ColumnDSL, string, string, error)) error { switch res.(type) { case []interface{}: @@ -212,7 +213,7 @@ func (c *Computable) viewRows(name string, process *gou.Process, res interface{} } // ViewRow row -func (c *Computable) viewRow(name string, process *gou.Process, res map[string]interface{}, getField func(string) (*field.ColumnDSL, string, error)) error { +func (c *Computable) viewRow(name string, process *gou.Process, res map[string]interface{}, getField func(string) (*field.ColumnDSL, string, string, error)) error { messages := []string{} row := maps.MapOf(res).Dot() @@ -234,13 +235,14 @@ func (c *Computable) viewRow(name string, process *gou.Process, res map[string]i for key, computes := range c.Computes.View { unit := computes[0] - field, path, err := getField(unit.Name) + field, path, id, err := getField(unit.Name) if err != nil { messages = append(messages, err.Error()) continue } data.Set("value", res[key]) + data.Set("id", id) data.Merge(any.MapOf(field.View.Map()).MapStrAny.Dot()) new, err := field.View.Compute.Value(data, process.Sid, process.Global) if err != nil { @@ -259,6 +261,6 @@ func (c *Computable) viewRow(name string, process *gou.Process, res map[string]i } // ComputeFilter filter -func (c *Computable) ComputeFilter(name string, process *gou.Process, args []interface{}, getFilter func(string) (*field.FilterDSL, string, error)) error { +func (c *Computable) ComputeFilter(name string, process *gou.Process, args []interface{}, getFilter func(string) (*field.FilterDSL, string, string, error)) error { return nil } diff --git a/widgets/form/compute.go b/widgets/form/compute.go index fd2fa78a..ee33db8d 100644 --- a/widgets/form/compute.go +++ b/widgets/form/compute.go @@ -7,13 +7,13 @@ import ( "github.com/yaoapp/yao/widgets/field" ) -func (dsl *DSL) getField() func(string) (*field.ColumnDSL, string, error) { - return func(name string) (*field.ColumnDSL, string, error) { +func (dsl *DSL) getField() func(string) (*field.ColumnDSL, string, string, error) { + return func(name string) (*field.ColumnDSL, string, string, error) { field, has := dsl.Fields.Form[name] if !has { - return nil, "fields.table", fmt.Errorf("fields.table.%s does not exist", name) + return nil, "fields.table", dsl.ID, fmt.Errorf("fields.table.%s does not exist", name) } - return &field, "fields.table", nil + return &field, "fields.table", dsl.ID, nil } } diff --git a/widgets/table/compute.go b/widgets/table/compute.go index b26f6f69..aadf67c0 100644 --- a/widgets/table/compute.go +++ b/widgets/table/compute.go @@ -7,23 +7,23 @@ import ( "github.com/yaoapp/yao/widgets/field" ) -func (dsl *DSL) getField() func(string) (*field.ColumnDSL, string, error) { - return func(name string) (*field.ColumnDSL, string, error) { +func (dsl *DSL) getField() func(string) (*field.ColumnDSL, string, string, error) { + return func(name string) (*field.ColumnDSL, string, string, error) { field, has := dsl.Fields.Table[name] if !has { - return nil, "fields.table", fmt.Errorf("fields.table.%s does not exist", name) + return nil, "fields.table", dsl.ID, fmt.Errorf("fields.table.%s does not exist", name) } - return &field, "fields.table", nil + return &field, "fields.table", dsl.ID, nil } } -func (dsl *DSL) getFilter() func(string) (*field.FilterDSL, string, error) { - return func(name string) (*field.FilterDSL, string, error) { +func (dsl *DSL) getFilter() func(string) (*field.FilterDSL, string, string, error) { + return func(name string) (*field.FilterDSL, string, string, error) { field, has := dsl.Fields.Filter[name] if !has { - return nil, "fields.filter", fmt.Errorf("fields.filter.%s does not exist", name) + return nil, "fields.filter", dsl.ID, fmt.Errorf("fields.filter.%s does not exist", name) } - return &field, "fields.filter", nil + return &field, "fields.filter", dsl.ID, nil } } diff --git a/widgets/table/compute_test.go b/widgets/table/compute_test.go index 0c692c67..890ced89 100644 --- a/widgets/table/compute_test.go +++ b/widgets/table/compute_test.go @@ -39,7 +39,7 @@ func TestComputeFind(t *testing.T) { t.Fatal(err) } data := any.Of(res).MapStr().Dot() - assert.Equal(t, "cat::Cookie-checked", data.Get("name_view")) + assert.Equal(t, "cat::Cookie-checked-compute", data.Get("name_view")) } func TestComputeGet(t *testing.T) { @@ -57,7 +57,7 @@ func TestComputeGet(t *testing.T) { } arr := any.Of(res).CArray() data := any.Of(arr[0]).MapStr().Dot() - assert.Equal(t, "cat::Cookie-checked", data.Get("name_view")) + assert.Equal(t, "cat::Cookie-checked-compute", data.Get("name_view")) } @@ -74,7 +74,7 @@ func TestComputeSearch(t *testing.T) { t.Fatal(err) } data := any.Of(res).MapStr().Dot() - assert.Equal(t, "cat::Cookie-checked", data.Get("data.0.name_view")) + assert.Equal(t, "cat::Cookie-checked-compute", data.Get("data.0.name_view")) } func TestComputeSave(t *testing.T) {