5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-12-28 03:15:39 +00:00
matterbridge/vendor/github.com/slack-go/slack/block_conv.go

453 lines
11 KiB
Go
Raw Normal View History

2019-09-07 20:46:58 +00:00
package slack
import (
"encoding/json"
"fmt"
2019-09-07 20:46:58 +00:00
)
type sumtype struct {
TypeVal string `json:"type"`
}
// MarshalJSON implements the Marshaller interface for Blocks so that any JSON
// marshalling is delegated and proper type determination can be made before marshal
func (b Blocks) MarshalJSON() ([]byte, error) {
bytes, err := json.Marshal(b.BlockSet)
if err != nil {
return nil, err
}
return bytes, nil
}
// UnmarshalJSON implements the Unmarshaller interface for Blocks, so that any JSON
// unmarshalling is delegated and proper type determination can be made before unmarshal
func (b *Blocks) UnmarshalJSON(data []byte) error {
var raw []json.RawMessage
if string(data) == "{}" {
return nil
}
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}
var blocks Blocks
for _, r := range raw {
s := sumtype{}
err := json.Unmarshal(r, &s)
if err != nil {
return err
}
var blockType string
if s.TypeVal != "" {
blockType = s.TypeVal
}
var block Block
switch blockType {
case "actions":
block = &ActionBlock{}
case "context":
block = &ContextBlock{}
case "divider":
block = &DividerBlock{}
2020-03-28 22:50:47 +00:00
case "file":
block = &FileBlock{}
2021-02-01 20:29:04 +00:00
case "header":
block = &HeaderBlock{}
2019-09-07 20:46:58 +00:00
case "image":
block = &ImageBlock{}
case "input":
block = &InputBlock{}
2021-12-11 23:05:15 +00:00
case "rich_text":
block = &RichTextBlock{}
case "rich_text_input":
block = &RichTextBlock{}
2019-09-07 20:46:58 +00:00
case "section":
block = &SectionBlock{}
case "video":
block = &VideoBlock{}
2019-09-07 20:46:58 +00:00
default:
2020-03-28 22:50:47 +00:00
block = &UnknownBlock{}
2019-09-07 20:46:58 +00:00
}
err = json.Unmarshal(r, block)
if err != nil {
return err
}
blocks.BlockSet = append(blocks.BlockSet, block)
}
*b = blocks
return nil
}
// UnmarshalJSON implements the Unmarshaller interface for InputBlock, so that any JSON
// unmarshalling is delegated and proper type determination can be made before unmarshal
func (b *InputBlock) UnmarshalJSON(data []byte) error {
type alias InputBlock
a := struct {
Element json.RawMessage `json:"element"`
*alias
}{
alias: (*alias)(b),
}
if err := json.Unmarshal(data, &a); err != nil {
return err
}
s := sumtype{}
if err := json.Unmarshal(a.Element, &s); err != nil {
return nil
}
var e BlockElement
switch s.TypeVal {
case "datepicker":
e = &DatePickerBlockElement{}
2021-02-01 20:29:04 +00:00
case "timepicker":
e = &TimePickerBlockElement{}
case "datetimepicker":
e = &DateTimePickerBlockElement{}
case "plain_text_input":
e = &PlainTextInputBlockElement{}
case "rich_text_input":
e = &RichTextInputBlockElement{}
2023-01-28 21:57:53 +00:00
case "email_text_input":
e = &EmailTextInputBlockElement{}
case "url_text_input":
e = &URLTextInputBlockElement{}
case "static_select", "external_select", "users_select", "conversations_select", "channels_select":
e = &SelectBlockElement{}
2020-05-23 22:06:21 +00:00
case "multi_static_select", "multi_external_select", "multi_users_select", "multi_conversations_select", "multi_channels_select":
e = &MultiSelectBlockElement{}
2020-09-04 21:29:13 +00:00
case "checkboxes":
e = &CheckboxGroupsBlockElement{}
case "overflow":
e = &OverflowBlockElement{}
case "radio_buttons":
e = &RadioButtonsBlockElement{}
2023-01-28 21:57:53 +00:00
case "number_input":
e = &NumberInputBlockElement{}
case "file_input":
e = &FileInputBlockElement{}
default:
return fmt.Errorf("unsupported block element type %v", s.TypeVal)
}
if err := json.Unmarshal(a.Element, e); err != nil {
return err
}
b.Element = e
return nil
}
2019-09-07 20:46:58 +00:00
// MarshalJSON implements the Marshaller interface for BlockElements so that any JSON
// marshalling is delegated and proper type determination can be made before marshal
func (b *BlockElements) MarshalJSON() ([]byte, error) {
bytes, err := json.Marshal(b.ElementSet)
if err != nil {
return nil, err
}
return bytes, nil
}
// UnmarshalJSON implements the Unmarshaller interface for BlockElements, so that any JSON
// unmarshalling is delegated and proper type determination can be made before unmarshal
func (b *BlockElements) UnmarshalJSON(data []byte) error {
var raw []json.RawMessage
if string(data) == "{}" {
return nil
}
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}
var blockElements BlockElements
for _, r := range raw {
s := sumtype{}
err := json.Unmarshal(r, &s)
if err != nil {
return err
}
var blockElementType string
if s.TypeVal != "" {
blockElementType = s.TypeVal
}
var blockElement BlockElement
switch blockElementType {
case "image":
blockElement = &ImageBlockElement{}
case "button":
blockElement = &ButtonBlockElement{}
case "overflow":
blockElement = &OverflowBlockElement{}
case "datepicker":
blockElement = &DatePickerBlockElement{}
case "timepicker":
blockElement = &TimePickerBlockElement{}
case "datetimepicker":
blockElement = &DateTimePickerBlockElement{}
case "plain_text_input":
blockElement = &PlainTextInputBlockElement{}
case "rich_text_input":
blockElement = &RichTextInputBlockElement{}
2023-01-28 21:57:53 +00:00
case "email_text_input":
blockElement = &EmailTextInputBlockElement{}
case "url_text_input":
blockElement = &URLTextInputBlockElement{}
case "checkboxes":
blockElement = &CheckboxGroupsBlockElement{}
2020-09-04 21:29:13 +00:00
case "radio_buttons":
blockElement = &RadioButtonsBlockElement{}
2019-09-07 20:46:58 +00:00
case "static_select", "external_select", "users_select", "conversations_select", "channels_select":
blockElement = &SelectBlockElement{}
2023-01-28 21:57:53 +00:00
case "number_input":
blockElement = &NumberInputBlockElement{}
2019-09-07 20:46:58 +00:00
default:
return fmt.Errorf("unsupported block element type %v", blockElementType)
2019-09-07 20:46:58 +00:00
}
err = json.Unmarshal(r, blockElement)
if err != nil {
return err
}
blockElements.ElementSet = append(blockElements.ElementSet, blockElement)
}
*b = blockElements
return nil
}
// MarshalJSON implements the Marshaller interface for Accessory so that any JSON
// marshalling is delegated and proper type determination can be made before marshal
func (a *Accessory) MarshalJSON() ([]byte, error) {
bytes, err := json.Marshal(toBlockElement(a))
if err != nil {
return nil, err
}
return bytes, nil
}
// UnmarshalJSON implements the Unmarshaller interface for Accessory, so that any JSON
// unmarshalling is delegated and proper type determination can be made before unmarshal
// Note: datetimepicker is not supported in Accessory
2019-09-07 20:46:58 +00:00
func (a *Accessory) UnmarshalJSON(data []byte) error {
var r json.RawMessage
if string(data) == "{\"accessory\":null}" {
return nil
}
err := json.Unmarshal(data, &r)
if err != nil {
return err
}
s := sumtype{}
err = json.Unmarshal(r, &s)
if err != nil {
return err
}
var blockElementType string
if s.TypeVal != "" {
blockElementType = s.TypeVal
}
switch blockElementType {
case "image":
element, err := unmarshalBlockElement(r, &ImageBlockElement{})
if err != nil {
return err
}
a.ImageElement = element.(*ImageBlockElement)
case "button":
element, err := unmarshalBlockElement(r, &ButtonBlockElement{})
if err != nil {
return err
}
a.ButtonElement = element.(*ButtonBlockElement)
case "overflow":
element, err := unmarshalBlockElement(r, &OverflowBlockElement{})
if err != nil {
return err
}
a.OverflowElement = element.(*OverflowBlockElement)
case "datepicker":
element, err := unmarshalBlockElement(r, &DatePickerBlockElement{})
if err != nil {
return err
}
a.DatePickerElement = element.(*DatePickerBlockElement)
2021-02-01 20:29:04 +00:00
case "timepicker":
element, err := unmarshalBlockElement(r, &TimePickerBlockElement{})
if err != nil {
return err
}
a.TimePickerElement = element.(*TimePickerBlockElement)
2020-03-28 22:50:47 +00:00
case "plain_text_input":
element, err := unmarshalBlockElement(r, &PlainTextInputBlockElement{})
if err != nil {
return err
}
a.PlainTextInputElement = element.(*PlainTextInputBlockElement)
case "rich_text_input":
element, err := unmarshalBlockElement(r, &RichTextInputBlockElement{})
if err != nil {
return err
}
a.RichTextInputElement = element.(*RichTextInputBlockElement)
2020-03-28 22:50:47 +00:00
case "radio_buttons":
element, err := unmarshalBlockElement(r, &RadioButtonsBlockElement{})
if err != nil {
return err
}
a.RadioButtonsElement = element.(*RadioButtonsBlockElement)
case "static_select", "external_select", "users_select", "conversations_select", "channels_select":
2019-09-07 20:46:58 +00:00
element, err := unmarshalBlockElement(r, &SelectBlockElement{})
if err != nil {
return err
}
a.SelectElement = element.(*SelectBlockElement)
2020-03-28 22:50:47 +00:00
case "multi_static_select", "multi_external_select", "multi_users_select", "multi_conversations_select", "multi_channels_select":
element, err := unmarshalBlockElement(r, &MultiSelectBlockElement{})
if err != nil {
return err
}
a.MultiSelectElement = element.(*MultiSelectBlockElement)
case "checkboxes":
element, err := unmarshalBlockElement(r, &CheckboxGroupsBlockElement{})
if err != nil {
return err
}
a.CheckboxGroupsBlockElement = element.(*CheckboxGroupsBlockElement)
2020-03-28 22:50:47 +00:00
default:
element, err := unmarshalBlockElement(r, &UnknownBlockElement{})
if err != nil {
return err
}
a.UnknownElement = element.(*UnknownBlockElement)
2019-09-07 20:46:58 +00:00
}
return nil
}
func unmarshalBlockElement(r json.RawMessage, element BlockElement) (BlockElement, error) {
err := json.Unmarshal(r, element)
if err != nil {
return nil, err
}
return element, nil
}
func toBlockElement(element *Accessory) BlockElement {
if element.ImageElement != nil {
return element.ImageElement
}
if element.ButtonElement != nil {
return element.ButtonElement
}
if element.OverflowElement != nil {
return element.OverflowElement
}
if element.DatePickerElement != nil {
return element.DatePickerElement
}
2021-02-01 20:29:04 +00:00
if element.TimePickerElement != nil {
return element.TimePickerElement
}
2020-03-28 22:50:47 +00:00
if element.PlainTextInputElement != nil {
return element.PlainTextInputElement
}
if element.RadioButtonsElement != nil {
return element.RadioButtonsElement
}
if element.CheckboxGroupsBlockElement != nil {
return element.CheckboxGroupsBlockElement
}
2019-09-07 20:46:58 +00:00
if element.SelectElement != nil {
return element.SelectElement
}
2020-03-28 22:50:47 +00:00
if element.MultiSelectElement != nil {
return element.MultiSelectElement
}
2019-09-07 20:46:58 +00:00
return nil
}
// MarshalJSON implements the Marshaller interface for ContextElements so that any JSON
// marshalling is delegated and proper type determination can be made before marshal
func (e *ContextElements) MarshalJSON() ([]byte, error) {
bytes, err := json.Marshal(e.Elements)
if err != nil {
return nil, err
}
return bytes, nil
}
// UnmarshalJSON implements the Unmarshaller interface for ContextElements, so that any JSON
// unmarshalling is delegated and proper type determination can be made before unmarshal
func (e *ContextElements) UnmarshalJSON(data []byte) error {
var raw []json.RawMessage
if string(data) == "{\"elements\":null}" {
return nil
}
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}
for _, r := range raw {
s := sumtype{}
err := json.Unmarshal(r, &s)
if err != nil {
return err
}
var contextElementType string
if s.TypeVal != "" {
contextElementType = s.TypeVal
}
switch contextElementType {
case PlainTextType, MarkdownType:
elem, err := unmarshalBlockObject(r, &TextBlockObject{})
if err != nil {
return err
}
e.Elements = append(e.Elements, elem.(*TextBlockObject))
case "image":
elem, err := unmarshalBlockElement(r, &ImageBlockElement{})
if err != nil {
return err
}
e.Elements = append(e.Elements, elem.(*ImageBlockElement))
default:
return fmt.Errorf("unsupported context element type %v", contextElementType)
2019-09-07 20:46:58 +00:00
}
}
return nil
}