| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | ) |
| 8 | |
| 9 | type sumtype struct { |
| 10 | TypeVal string `json:"type"` |
| 11 | } |
| 12 | |
| 13 | // MarshalJSON implements the Marshaller interface for Blocks so that any JSON |
| 14 | // marshalling is delegated and proper type determination can be made before marshal |
| 15 | func (b Blocks) MarshalJSON() ([]byte, error) { |
| 16 | bytes, err := json.Marshal(b.BlockSet) |
| 17 | if err != nil { |
| 18 | return nil, err |
| 19 | } |
| 20 | |
| 21 | return bytes, nil |
| 22 | } |
| 23 | |
| 24 | // UnmarshalJSON implements the Unmarshaller interface for Blocks, so that any JSON |
| 25 | // unmarshalling is delegated and proper type determination can be made before unmarshal |
| 26 | func (b *Blocks) UnmarshalJSON(data []byte) error { |
| 27 | var raw []json.RawMessage |
| 28 | |
| 29 | if string(data) == "{}" { |
| 30 | return nil |
| 31 | } |
| 32 | |
| 33 | err := json.Unmarshal(data, &raw) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | var blocks Blocks |
| 39 | for _, r := range raw { |
| 40 | s := sumtype{} |
| 41 | err := json.Unmarshal(r, &s) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | |
| 46 | var blockType string |
| 47 | if s.TypeVal != "" { |
| 48 | blockType = s.TypeVal |
| 49 | } |
| 50 | |
| 51 | var block Block |
| 52 | switch blockType { |
| 53 | case "actions": |
| 54 | block = &ActionBlock{} |
| 55 | case "context": |
| 56 | block = &ContextBlock{} |
| 57 | case "divider": |
| 58 | block = &DividerBlock{} |
| 59 | case "file": |
| 60 | block = &FileBlock{} |
| 61 | case "header": |
| 62 | block = &HeaderBlock{} |
| 63 | case "image": |
| 64 | block = &ImageBlock{} |
| 65 | case "input": |
| 66 | block = &InputBlock{} |
| 67 | case "rich_text": |
| 68 | block = &RichTextBlock{} |
| 69 | case "section": |
| 70 | block = &SectionBlock{} |
| 71 | default: |
| 72 | block = &UnknownBlock{} |
| 73 | } |
| 74 | |
| 75 | err = json.Unmarshal(r, block) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | blocks.BlockSet = append(blocks.BlockSet, block) |
| 81 | } |
| 82 | |
| 83 | *b = blocks |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | // UnmarshalJSON implements the Unmarshaller interface for InputBlock, so that any JSON |
| 88 | // unmarshalling is delegated and proper type determination can be made before unmarshal |
| 89 | func (b *InputBlock) UnmarshalJSON(data []byte) error { |
| 90 | type alias InputBlock |
| 91 | a := struct { |
| 92 | Element json.RawMessage `json:"element"` |
| 93 | *alias |
| 94 | }{ |
| 95 | alias: (*alias)(b), |
| 96 | } |
| 97 | |
| 98 | if err := json.Unmarshal(data, &a); err != nil { |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | s := sumtype{} |
| 103 | if err := json.Unmarshal(a.Element, &s); err != nil { |
| 104 | return nil |
| 105 | } |
| 106 | |
| 107 | var e BlockElement |
| 108 | switch s.TypeVal { |
| 109 | case "datepicker": |
| 110 | e = &DatePickerBlockElement{} |
| 111 | case "timepicker": |
| 112 | e = &TimePickerBlockElement{} |
| 113 | case "plain_text_input": |
| 114 | e = &PlainTextInputBlockElement{} |
| 115 | case "static_select", "external_select", "users_select", "conversations_select", "channels_select": |
| 116 | e = &SelectBlockElement{} |
| 117 | case "multi_static_select", "multi_external_select", "multi_users_select", "multi_conversations_select", "multi_channels_select": |
| 118 | e = &MultiSelectBlockElement{} |
| 119 | case "checkboxes": |
| 120 | e = &CheckboxGroupsBlockElement{} |
| 121 | case "overflow": |
| 122 | e = &OverflowBlockElement{} |
| 123 | case "radio_buttons": |
| 124 | e = &RadioButtonsBlockElement{} |
| 125 | default: |
| 126 | return errors.New("unsupported block element type") |
| 127 | } |
| 128 | |
| 129 | if err := json.Unmarshal(a.Element, e); err != nil { |
| 130 | return err |
| 131 | } |
| 132 | b.Element = e |
| 133 | |
| 134 | return nil |
| 135 | } |
| 136 | |
| 137 | // MarshalJSON implements the Marshaller interface for BlockElements so that any JSON |
| 138 | // marshalling is delegated and proper type determination can be made before marshal |
| 139 | func (b *BlockElements) MarshalJSON() ([]byte, error) { |
| 140 | bytes, err := json.Marshal(b.ElementSet) |
| 141 | if err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | |
| 145 | return bytes, nil |
| 146 | } |
| 147 | |
| 148 | // UnmarshalJSON implements the Unmarshaller interface for BlockElements, so that any JSON |
| 149 | // unmarshalling is delegated and proper type determination can be made before unmarshal |
| 150 | func (b *BlockElements) UnmarshalJSON(data []byte) error { |
| 151 | var raw []json.RawMessage |
| 152 | |
| 153 | if string(data) == "{}" { |
| 154 | return nil |
| 155 | } |
| 156 | |
| 157 | err := json.Unmarshal(data, &raw) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | |
| 162 | var blockElements BlockElements |
| 163 | for _, r := range raw { |
| 164 | s := sumtype{} |
| 165 | err := json.Unmarshal(r, &s) |
| 166 | if err != nil { |
| 167 | return err |
| 168 | } |
| 169 | |
| 170 | var blockElementType string |
| 171 | if s.TypeVal != "" { |
| 172 | blockElementType = s.TypeVal |
| 173 | } |
| 174 | |
| 175 | var blockElement BlockElement |
| 176 | switch blockElementType { |
| 177 | case "image": |
| 178 | blockElement = &ImageBlockElement{} |
| 179 | case "button": |
| 180 | blockElement = &ButtonBlockElement{} |
| 181 | case "overflow": |
| 182 | blockElement = &OverflowBlockElement{} |
| 183 | case "datepicker": |
| 184 | blockElement = &DatePickerBlockElement{} |
| 185 | case "timepicker": |
| 186 | blockElement = &TimePickerBlockElement{} |
| 187 | case "plain_text_input": |
| 188 | blockElement = &PlainTextInputBlockElement{} |
| 189 | case "checkboxes": |
| 190 | blockElement = &CheckboxGroupsBlockElement{} |
| 191 | case "radio_buttons": |
| 192 | blockElement = &RadioButtonsBlockElement{} |
| 193 | case "static_select", "external_select", "users_select", "conversations_select", "channels_select": |
| 194 | blockElement = &SelectBlockElement{} |
| 195 | default: |
| 196 | return fmt.Errorf("unsupported block element type %v", blockElementType) |
| 197 | } |
| 198 | |
| 199 | err = json.Unmarshal(r, blockElement) |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | |
| 204 | blockElements.ElementSet = append(blockElements.ElementSet, blockElement) |
| 205 | } |
| 206 | |
| 207 | *b = blockElements |
| 208 | return nil |
| 209 | } |
| 210 | |
| 211 | // MarshalJSON implements the Marshaller interface for Accessory so that any JSON |
| 212 | // marshalling is delegated and proper type determination can be made before marshal |
| 213 | func (a *Accessory) MarshalJSON() ([]byte, error) { |
| 214 | bytes, err := json.Marshal(toBlockElement(a)) |
| 215 | if err != nil { |
| 216 | return nil, err |
| 217 | } |
| 218 | |
| 219 | return bytes, nil |
| 220 | } |
| 221 | |
| 222 | // UnmarshalJSON implements the Unmarshaller interface for Accessory, so that any JSON |
| 223 | // unmarshalling is delegated and proper type determination can be made before unmarshal |
| 224 | func (a *Accessory) UnmarshalJSON(data []byte) error { |
| 225 | var r json.RawMessage |
| 226 | |
| 227 | if string(data) == "{\"accessory\":null}" { |
| 228 | return nil |
| 229 | } |
| 230 | |
| 231 | err := json.Unmarshal(data, &r) |
| 232 | if err != nil { |
| 233 | return err |
| 234 | } |
| 235 | |
| 236 | s := sumtype{} |
| 237 | err = json.Unmarshal(r, &s) |
| 238 | if err != nil { |
| 239 | return err |
| 240 | } |
| 241 | |
| 242 | var blockElementType string |
| 243 | if s.TypeVal != "" { |
| 244 | blockElementType = s.TypeVal |
| 245 | } |
| 246 | |
| 247 | switch blockElementType { |
| 248 | case "image": |
| 249 | element, err := unmarshalBlockElement(r, &ImageBlockElement{}) |
| 250 | if err != nil { |
| 251 | return err |
| 252 | } |
| 253 | a.ImageElement = element.(*ImageBlockElement) |
| 254 | case "button": |
| 255 | element, err := unmarshalBlockElement(r, &ButtonBlockElement{}) |
| 256 | if err != nil { |
| 257 | return err |
| 258 | } |
| 259 | a.ButtonElement = element.(*ButtonBlockElement) |
| 260 | case "overflow": |
| 261 | element, err := unmarshalBlockElement(r, &OverflowBlockElement{}) |
| 262 | if err != nil { |
| 263 | return err |
| 264 | } |
| 265 | a.OverflowElement = element.(*OverflowBlockElement) |
| 266 | case "datepicker": |
| 267 | element, err := unmarshalBlockElement(r, &DatePickerBlockElement{}) |
| 268 | if err != nil { |
| 269 | return err |
| 270 | } |
| 271 | a.DatePickerElement = element.(*DatePickerBlockElement) |
| 272 | case "timepicker": |
| 273 | element, err := unmarshalBlockElement(r, &TimePickerBlockElement{}) |
| 274 | if err != nil { |
| 275 | return err |
| 276 | } |
| 277 | a.TimePickerElement = element.(*TimePickerBlockElement) |
| 278 | case "plain_text_input": |
| 279 | element, err := unmarshalBlockElement(r, &PlainTextInputBlockElement{}) |
| 280 | if err != nil { |
| 281 | return err |
| 282 | } |
| 283 | a.PlainTextInputElement = element.(*PlainTextInputBlockElement) |
| 284 | case "radio_buttons": |
| 285 | element, err := unmarshalBlockElement(r, &RadioButtonsBlockElement{}) |
| 286 | if err != nil { |
| 287 | return err |
| 288 | } |
| 289 | a.RadioButtonsElement = element.(*RadioButtonsBlockElement) |
| 290 | case "static_select", "external_select", "users_select", "conversations_select", "channels_select": |
| 291 | element, err := unmarshalBlockElement(r, &SelectBlockElement{}) |
| 292 | if err != nil { |
| 293 | return err |
| 294 | } |
| 295 | a.SelectElement = element.(*SelectBlockElement) |
| 296 | case "multi_static_select", "multi_external_select", "multi_users_select", "multi_conversations_select", "multi_channels_select": |
| 297 | element, err := unmarshalBlockElement(r, &MultiSelectBlockElement{}) |
| 298 | if err != nil { |
| 299 | return err |
| 300 | } |
| 301 | a.MultiSelectElement = element.(*MultiSelectBlockElement) |
| 302 | case "checkboxes": |
| 303 | element, err := unmarshalBlockElement(r, &CheckboxGroupsBlockElement{}) |
| 304 | if err != nil { |
| 305 | return err |
| 306 | } |
| 307 | a.CheckboxGroupsBlockElement = element.(*CheckboxGroupsBlockElement) |
| 308 | default: |
| 309 | element, err := unmarshalBlockElement(r, &UnknownBlockElement{}) |
| 310 | if err != nil { |
| 311 | return err |
| 312 | } |
| 313 | a.UnknownElement = element.(*UnknownBlockElement) |
| 314 | } |
| 315 | |
| 316 | return nil |
| 317 | } |
| 318 | |
| 319 | func unmarshalBlockElement(r json.RawMessage, element BlockElement) (BlockElement, error) { |
| 320 | err := json.Unmarshal(r, element) |
| 321 | if err != nil { |
| 322 | return nil, err |
| 323 | } |
| 324 | return element, nil |
| 325 | } |
| 326 | |
| 327 | func toBlockElement(element *Accessory) BlockElement { |
| 328 | if element.ImageElement != nil { |
| 329 | return element.ImageElement |
| 330 | } |
| 331 | if element.ButtonElement != nil { |
| 332 | return element.ButtonElement |
| 333 | } |
| 334 | if element.OverflowElement != nil { |
| 335 | return element.OverflowElement |
| 336 | } |
| 337 | if element.DatePickerElement != nil { |
| 338 | return element.DatePickerElement |
| 339 | } |
| 340 | if element.TimePickerElement != nil { |
| 341 | return element.TimePickerElement |
| 342 | } |
| 343 | if element.PlainTextInputElement != nil { |
| 344 | return element.PlainTextInputElement |
| 345 | } |
| 346 | if element.RadioButtonsElement != nil { |
| 347 | return element.RadioButtonsElement |
| 348 | } |
| 349 | if element.CheckboxGroupsBlockElement != nil { |
| 350 | return element.CheckboxGroupsBlockElement |
| 351 | } |
| 352 | if element.SelectElement != nil { |
| 353 | return element.SelectElement |
| 354 | } |
| 355 | if element.MultiSelectElement != nil { |
| 356 | return element.MultiSelectElement |
| 357 | } |
| 358 | |
| 359 | return nil |
| 360 | } |
| 361 | |
| 362 | // MarshalJSON implements the Marshaller interface for ContextElements so that any JSON |
| 363 | // marshalling is delegated and proper type determination can be made before marshal |
| 364 | func (e *ContextElements) MarshalJSON() ([]byte, error) { |
| 365 | bytes, err := json.Marshal(e.Elements) |
| 366 | if err != nil { |
| 367 | return nil, err |
| 368 | } |
| 369 | |
| 370 | return bytes, nil |
| 371 | } |
| 372 | |
| 373 | // UnmarshalJSON implements the Unmarshaller interface for ContextElements, so that any JSON |
| 374 | // unmarshalling is delegated and proper type determination can be made before unmarshal |
| 375 | func (e *ContextElements) UnmarshalJSON(data []byte) error { |
| 376 | var raw []json.RawMessage |
| 377 | |
| 378 | if string(data) == "{\"elements\":null}" { |
| 379 | return nil |
| 380 | } |
| 381 | |
| 382 | err := json.Unmarshal(data, &raw) |
| 383 | if err != nil { |
| 384 | return err |
| 385 | } |
| 386 | |
| 387 | for _, r := range raw { |
| 388 | s := sumtype{} |
| 389 | err := json.Unmarshal(r, &s) |
| 390 | if err != nil { |
| 391 | return err |
| 392 | } |
| 393 | |
| 394 | var contextElementType string |
| 395 | if s.TypeVal != "" { |
| 396 | contextElementType = s.TypeVal |
| 397 | } |
| 398 | |
| 399 | switch contextElementType { |
| 400 | case PlainTextType, MarkdownType: |
| 401 | elem, err := unmarshalBlockObject(r, &TextBlockObject{}) |
| 402 | if err != nil { |
| 403 | return err |
| 404 | } |
| 405 | |
| 406 | e.Elements = append(e.Elements, elem.(*TextBlockObject)) |
| 407 | case "image": |
| 408 | elem, err := unmarshalBlockElement(r, &ImageBlockElement{}) |
| 409 | if err != nil { |
| 410 | return err |
| 411 | } |
| 412 | |
| 413 | e.Elements = append(e.Elements, elem.(*ImageBlockElement)) |
| 414 | default: |
| 415 | return errors.New("unsupported context element type") |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return nil |
| 420 | } |