diff --git a/widgets/field/column.go b/widgets/field/column.go index f58128de..7ffd6322 100644 --- a/widgets/field/column.go +++ b/widgets/field/column.go @@ -1,10 +1,41 @@ package field import ( + "fmt" + "io" + + jsoniter "github.com/json-iterator/go" "github.com/yaoapp/yao/widgets/component" "github.com/yaoapp/yao/widgets/expression" + "golang.org/x/crypto/md4" ) +// UnmarshalJSON for json UnmarshalJSON +func (column *ColumnDSL) UnmarshalJSON(data []byte) error { + var alias aliasColumnDSL + err := jsoniter.Unmarshal(data, &alias) + if err != nil { + return err + } + + *column = ColumnDSL(alias) + column.ID, err = column.Hash() + if err != nil { + return err + } + + return nil +} + +// Hash hash value +func (column ColumnDSL) Hash() (string, error) { + h := md4.New() + origin := fmt.Sprintf("%#v", column) + // fmt.Println(origin) + io.WriteString(h, origin) + return fmt.Sprintf("%x", h.Sum(nil)), nil +} + // Replace replace with data func (column ColumnDSL) Replace(data map[string]interface{}) (*ColumnDSL, error) { new := column @@ -72,6 +103,7 @@ func (column *ColumnDSL) Clone() *ColumnDSL { // Map cast to map[string]inteface{} func (column ColumnDSL) Map() map[string]interface{} { res := map[string]interface{}{ + "id": column.ID, "bind": column.Bind, } diff --git a/widgets/field/filter.go b/widgets/field/filter.go index 21736c06..d2cc4f11 100644 --- a/widgets/field/filter.go +++ b/widgets/field/filter.go @@ -1,10 +1,41 @@ package field import ( + "fmt" + "io" + + jsoniter "github.com/json-iterator/go" "github.com/yaoapp/yao/widgets/component" "github.com/yaoapp/yao/widgets/expression" + "golang.org/x/crypto/md4" ) +// UnmarshalJSON for json UnmarshalJSON +func (filter *FilterDSL) UnmarshalJSON(data []byte) error { + var alias aliasFilterDSL + err := jsoniter.Unmarshal(data, &alias) + if err != nil { + return err + } + + *filter = FilterDSL(alias) + filter.ID, err = filter.Hash() + if err != nil { + return err + } + + return nil +} + +// Hash hash value +func (filter FilterDSL) Hash() (string, error) { + h := md4.New() + origin := fmt.Sprintf("%#v", filter) + // fmt.Println(origin) + io.WriteString(h, origin) + return fmt.Sprintf("%x", h.Sum(nil)), nil +} + // Replace replace with data func (filter FilterDSL) Replace(data map[string]interface{}) (*FilterDSL, error) { new := filter @@ -42,7 +73,9 @@ func (filter *FilterDSL) Clone() *FilterDSL { // Map cast to map[string]inteface{} func (filter FilterDSL) Map() map[string]interface{} { + res := map[string]interface{}{ + "id": filter.ID, "bind": filter.Bind, } diff --git a/widgets/field/types.go b/widgets/field/types.go index 7bde21ca..5145da32 100644 --- a/widgets/field/types.go +++ b/widgets/field/types.go @@ -18,6 +18,7 @@ type CloudProps map[string]component.CloudPropsDSL // ColumnDSL the field column dsl type ColumnDSL struct { + ID string `json:"id,omitempty"` Key string `json:"key,omitempty"` Bind string `json:"bind,omitempty"` Link string `json:"link,omitempty"` @@ -25,13 +26,18 @@ type ColumnDSL struct { Edit *component.DSL `json:"edit,omitempty"` } +type aliasColumnDSL ColumnDSL + // FilterDSL the field filter dsl type FilterDSL struct { + ID string `json:"id,omitempty"` Key string `json:"key,omitempty"` Bind string `json:"bind,omitempty"` Edit *component.DSL `json:"edit,omitempty"` } +type aliasFilterDSL FilterDSL + // Compute the compute filed type Compute string