Skip to content

Commit

Permalink
fix: add markdown/input/button component unmarshal case (#100)
Browse files Browse the repository at this point in the history
Co-authored-by: linlihong <[email protected]>
  • Loading branch information
linlih and linlihong authored Sep 10, 2024
1 parent 72c8f82 commit 5adf90a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions card/v2/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ func unmarshalComponent(data json.RawMessage) (Component, error) {
res := &ComponentColumnSet{}
err = json.Unmarshal(data, res)
return res, err
case "markdown":
res := &ComponentMarkdown{}
err = json.Unmarshal(data, res)
return res, err
case "input":
res := &ComponentInput{}
err = json.Unmarshal(data, res)
return res, err
case "button":
res := &ComponentButton{}
err = json.Unmarshal(data, res)
return res, err
}

return nil, nil
Expand Down
27 changes: 26 additions & 1 deletion card/v2/component_container_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ func Form(name string, elements ...Component) *ComponentForm {
//
//go:generate generate_set_attrs -type=ComponentForm
//go:generate generate_to_map -type=ComponentForm
//go:generate generate_iface_unmarshal -type=ComponentForm
type ComponentForm struct {
componentBase

// 表单容器的唯一标识。用于识别用户提交的数据属于哪个表单容器。在同一张卡片内, 该字段的值全局唯一。
Name string `json:"name,omitempty"`

// 表单容器的子节点。可内嵌其它容器类组件和展示、交互组件, 不支持内嵌表格、图表、和表单容器组件。
Elements []Component `json:"elements,omitempty"`
Elements []Component `json:"elements,omitempty" unmarshal:"unmarshalComponent"`
}

// MarshalJSON ...
Expand Down Expand Up @@ -68,3 +69,27 @@ func (r *ComponentForm) toMap() map[string]interface{} {
}
return res
}

// unmarshalComponentForm generated to unmarshal ComponentForm iface
type unmarshalComponentForm struct {
Name string `json:"name,omitempty"`
Elements []json.RawMessage `json:"elements,omitempty"`
}

// UnmarshalJSON generated to unmarshal ComponentForm iface
func (r *ComponentForm) UnmarshalJSON(bs []byte) error {
obj := new(unmarshalComponentForm)
err := json.Unmarshal(bs, obj)
if err != nil {
return err
}
r.Name = obj.Name
r.Elements = make([]Component, len(obj.Elements))
for i, v := range obj.Elements {
r.Elements[i], err = unmarshalComponent(v)
if err != nil {
return err
}
}
return nil
}

0 comments on commit 5adf90a

Please sign in to comment.