-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from trheyi/main
[add] bind model
- Loading branch information
Showing
5 changed files
with
147 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,67 @@ | ||
package form | ||
|
||
import ( | ||
jsoniter "github.com/json-iterator/go" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/yaoapp/gou" | ||
"github.com/yaoapp/yao/widgets/field" | ||
) | ||
|
||
// BindModel bind model | ||
func (fields *FieldsDSL) BindModel(m *gou.Model) { | ||
func (fields *FieldsDSL) BindModel(m *gou.Model) error { | ||
|
||
fields.formMap = map[string]field.ColumnDSL{} | ||
|
||
trans, err := field.ModelTransform() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, col := range m.Columns { | ||
data := col.Map() | ||
formField, err := trans.Form(col.Type, data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// append columns | ||
if _, has := fields.formMap[formField.Key]; !has { | ||
fields.Form[formField.Key] = *formField | ||
fields.formMap[col.Name] = fields.Form[formField.Key] | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Xgen trans to xgen setting | ||
func (fields *FieldsDSL) Xgen() (map[string]interface{}, error) { | ||
func (fields *FieldsDSL) Xgen(layout *LayoutDSL) (map[string]interface{}, error) { | ||
res := map[string]interface{}{} | ||
data, err := jsoniter.Marshal(fields) | ||
if err != nil { | ||
return nil, err | ||
} | ||
forms := map[string]interface{}{} | ||
messages := []string{} | ||
if layout.Form != nil && layout.Form.Sections != nil { | ||
|
||
err = jsoniter.Unmarshal(data, &res) | ||
if err != nil { | ||
return nil, err | ||
layout.listColumns(func(path string, f Column) { | ||
name := f.Name | ||
field, has := fields.Form[name] | ||
if !has { | ||
if strings.HasPrefix(f.Name, "::") { | ||
name = fmt.Sprintf("$L(%s)", strings.TrimPrefix(f.Name, "::")) | ||
if field, has = fields.Form[name]; has { | ||
forms[name] = field.Map() | ||
return | ||
} | ||
} | ||
messages = append(messages, fmt.Sprintf("fields.form.%s not found, checking %s", f.Name, path)) | ||
} | ||
forms[name] = field.Map() | ||
}, "", nil) | ||
} | ||
|
||
if len(messages) > 0 { | ||
return nil, fmt.Errorf(strings.Join(messages, ";\n")) | ||
} | ||
res["form"] = forms | ||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters