Skip to content

Commit 11eb9d3

Browse files
authored
refactor: decode block values directly (#1699)
Decode composition objects, accessories, and context elements directly into concrete pointers. Remove two helpers that returned their input as an interface, 19 redundant type assertions, and the unused private interface and marker methods. This removes 48 lines. Preserve decoded values, error handling, and assignment only after successful decoding. Public types and APIs remain unchanged. Validation: existing block, accessory, composition-object, and Slack icon tests pass, along with formatting, lint, module, and whitespace checks. A temporary comparison against the original implementation produced identical decoded state, error types, and error messages across 3,582 cases, including nulls, malformed fields, unknown types, prefilled receivers, and partial failures.
1 parent 68d04de commit 11eb9d3

2 files changed

Lines changed: 58 additions & 106 deletions

File tree

‎block_conv.go‎

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -308,96 +308,88 @@ func (a *Accessory) UnmarshalJSON(data []byte) error {
308308

309309
switch blockElementType {
310310
case "image":
311-
element, err := unmarshalBlockElement(r, &ImageBlockElement{})
312-
if err != nil {
311+
element := &ImageBlockElement{}
312+
if err := json.Unmarshal(r, element); err != nil {
313313
return err
314314
}
315-
a.ImageElement = element.(*ImageBlockElement)
315+
a.ImageElement = element
316316
case "button":
317-
element, err := unmarshalBlockElement(r, &ButtonBlockElement{})
318-
if err != nil {
317+
element := &ButtonBlockElement{}
318+
if err := json.Unmarshal(r, element); err != nil {
319319
return err
320320
}
321-
a.ButtonElement = element.(*ButtonBlockElement)
321+
a.ButtonElement = element
322322
case "overflow":
323-
element, err := unmarshalBlockElement(r, &OverflowBlockElement{})
324-
if err != nil {
323+
element := &OverflowBlockElement{}
324+
if err := json.Unmarshal(r, element); err != nil {
325325
return err
326326
}
327-
a.OverflowElement = element.(*OverflowBlockElement)
327+
a.OverflowElement = element
328328
case "datepicker":
329-
element, err := unmarshalBlockElement(r, &DatePickerBlockElement{})
330-
if err != nil {
329+
element := &DatePickerBlockElement{}
330+
if err := json.Unmarshal(r, element); err != nil {
331331
return err
332332
}
333-
a.DatePickerElement = element.(*DatePickerBlockElement)
333+
a.DatePickerElement = element
334334
case "timepicker":
335-
element, err := unmarshalBlockElement(r, &TimePickerBlockElement{})
336-
if err != nil {
335+
element := &TimePickerBlockElement{}
336+
if err := json.Unmarshal(r, element); err != nil {
337337
return err
338338
}
339-
a.TimePickerElement = element.(*TimePickerBlockElement)
339+
a.TimePickerElement = element
340340
case "plain_text_input":
341-
element, err := unmarshalBlockElement(r, &PlainTextInputBlockElement{})
342-
if err != nil {
341+
element := &PlainTextInputBlockElement{}
342+
if err := json.Unmarshal(r, element); err != nil {
343343
return err
344344
}
345-
a.PlainTextInputElement = element.(*PlainTextInputBlockElement)
345+
a.PlainTextInputElement = element
346346
case "rich_text_input":
347-
element, err := unmarshalBlockElement(r, &RichTextInputBlockElement{})
348-
if err != nil {
347+
element := &RichTextInputBlockElement{}
348+
if err := json.Unmarshal(r, element); err != nil {
349349
return err
350350
}
351-
a.RichTextInputElement = element.(*RichTextInputBlockElement)
351+
a.RichTextInputElement = element
352352
case "radio_buttons":
353-
element, err := unmarshalBlockElement(r, &RadioButtonsBlockElement{})
354-
if err != nil {
353+
element := &RadioButtonsBlockElement{}
354+
if err := json.Unmarshal(r, element); err != nil {
355355
return err
356356
}
357-
a.RadioButtonsElement = element.(*RadioButtonsBlockElement)
357+
a.RadioButtonsElement = element
358358
case "static_select", "external_select", "users_select", "conversations_select", "channels_select":
359-
element, err := unmarshalBlockElement(r, &SelectBlockElement{})
360-
if err != nil {
359+
element := &SelectBlockElement{}
360+
if err := json.Unmarshal(r, element); err != nil {
361361
return err
362362
}
363-
a.SelectElement = element.(*SelectBlockElement)
363+
a.SelectElement = element
364364
case "multi_static_select", "multi_external_select", "multi_users_select", "multi_conversations_select", "multi_channels_select":
365-
element, err := unmarshalBlockElement(r, &MultiSelectBlockElement{})
366-
if err != nil {
365+
element := &MultiSelectBlockElement{}
366+
if err := json.Unmarshal(r, element); err != nil {
367367
return err
368368
}
369-
a.MultiSelectElement = element.(*MultiSelectBlockElement)
369+
a.MultiSelectElement = element
370370
case "checkboxes":
371-
element, err := unmarshalBlockElement(r, &CheckboxGroupsBlockElement{})
372-
if err != nil {
371+
element := &CheckboxGroupsBlockElement{}
372+
if err := json.Unmarshal(r, element); err != nil {
373373
return err
374374
}
375-
a.CheckboxGroupsBlockElement = element.(*CheckboxGroupsBlockElement)
375+
a.CheckboxGroupsBlockElement = element
376376
case "workflow_button":
377-
element, err := unmarshalBlockElement(r, &WorkflowButtonBlockElement{})
378-
if err != nil {
377+
element := &WorkflowButtonBlockElement{}
378+
if err := json.Unmarshal(r, element); err != nil {
379379
return err
380380
}
381-
a.WorkflowButtonElement = element.(*WorkflowButtonBlockElement)
381+
a.WorkflowButtonElement = element
382382
default:
383-
element, err := unmarshalBlockElement(r, &UnknownBlockElement{})
384-
if err != nil {
383+
element := &UnknownBlockElement{}
384+
if err := json.Unmarshal(r, element); err != nil {
385385
return err
386386
}
387-
a.UnknownElement = element.(*UnknownBlockElement)
387+
a.UnknownElement = element
388388
}
389389

390390
return nil
391391
}
392392

393-
func unmarshalBlockElement(r json.RawMessage, element BlockElement) (BlockElement, error) {
394-
err := json.Unmarshal(r, element)
395-
if err != nil {
396-
return nil, err
397-
}
398-
return element, nil
399-
}
400-
401393
func toBlockElement(element *Accessory) BlockElement {
402394
if element.ImageElement != nil {
403395
return element.ImageElement
@@ -472,19 +464,19 @@ func (e *ContextElements) UnmarshalJSON(data []byte) error {
472464

473465
switch contextElementType {
474466
case PlainTextType, MarkdownType:
475-
elem, err := unmarshalBlockObject(r, &TextBlockObject{})
476-
if err != nil {
467+
elem := &TextBlockObject{}
468+
if err := json.Unmarshal(r, elem); err != nil {
477469
return err
478470
}
479471

480-
e.Elements = append(e.Elements, elem.(*TextBlockObject))
472+
e.Elements = append(e.Elements, elem)
481473
case "image":
482-
elem, err := unmarshalBlockElement(r, &ImageBlockElement{})
483-
if err != nil {
474+
elem := &ImageBlockElement{}
475+
if err := json.Unmarshal(r, elem); err != nil {
484476
return err
485477
}
486478

487-
e.Elements = append(e.Elements, elem.(*ImageBlockElement))
479+
e.Elements = append(e.Elements, elem)
488480
default:
489481
return fmt.Errorf("unsupported context element type %v", contextElementType)
490482
}

‎block_object.go‎

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import (
1010
//
1111
// For more information: https://api.slack.com/reference/messaging/composition-objects
1212

13-
// BlockObject defines an interface that all block object types should
14-
// implement.
15-
1613
// blockObject object types
1714
const (
1815
MarkdownType = "mrkdwn"
@@ -26,10 +23,6 @@ const (
2623

2724
type MessageObjectType string
2825

29-
type blockObject interface {
30-
validateType() MessageObjectType
31-
}
32-
3326
type BlockObjects struct {
3427
TextObjects []*TextBlockObject
3528
ConfirmationObjects []*ConfirmationBlockObject
@@ -57,29 +50,29 @@ func (b *BlockObjects) UnmarshalJSON(data []byte) error {
5750

5851
switch blockObjectType {
5952
case PlainTextType, MarkdownType:
60-
object, err := unmarshalBlockObject(r, &TextBlockObject{})
61-
if err != nil {
53+
object := &TextBlockObject{}
54+
if err := json.Unmarshal(r, object); err != nil {
6255
return err
6356
}
64-
b.TextObjects = append(b.TextObjects, object.(*TextBlockObject))
57+
b.TextObjects = append(b.TextObjects, object)
6558
case motConfirmation:
66-
object, err := unmarshalBlockObject(r, &ConfirmationBlockObject{})
67-
if err != nil {
59+
object := &ConfirmationBlockObject{}
60+
if err := json.Unmarshal(r, object); err != nil {
6861
return err
6962
}
70-
b.ConfirmationObjects = append(b.ConfirmationObjects, object.(*ConfirmationBlockObject))
63+
b.ConfirmationObjects = append(b.ConfirmationObjects, object)
7164
case motOption:
72-
object, err := unmarshalBlockObject(r, &OptionBlockObject{})
73-
if err != nil {
65+
object := &OptionBlockObject{}
66+
if err := json.Unmarshal(r, object); err != nil {
7467
return err
7568
}
76-
b.OptionObjects = append(b.OptionObjects, object.(*OptionBlockObject))
69+
b.OptionObjects = append(b.OptionObjects, object)
7770
case motOptionGroup:
78-
object, err := unmarshalBlockObject(r, &OptionGroupBlockObject{})
79-
if err != nil {
71+
object := &OptionGroupBlockObject{}
72+
if err := json.Unmarshal(r, object); err != nil {
8073
return err
8174
}
82-
b.OptionGroupObjects = append(b.OptionGroupObjects, object.(*OptionGroupBlockObject))
75+
b.OptionGroupObjects = append(b.OptionGroupObjects, object)
8376

8477
}
8578
}
@@ -108,14 +101,6 @@ func getBlockObjectType(obj map[string]any) string {
108101
return ""
109102
}
110103

111-
func unmarshalBlockObject(r json.RawMessage, object blockObject) (blockObject, error) {
112-
err := json.Unmarshal(r, object)
113-
if err != nil {
114-
return nil, err
115-
}
116-
return object, nil
117-
}
118-
119104
// TextBlockObject defines a text element object to be used with blocks
120105
//
121106
// More Information: https://api.slack.com/reference/messaging/composition-objects#text
@@ -126,12 +111,7 @@ type TextBlockObject struct {
126111
Verbatim bool `json:"verbatim,omitempty"`
127112
}
128113

129-
// validateType enforces block objects for element and block parameters
130-
func (s TextBlockObject) validateType() MessageObjectType {
131-
return MessageObjectType(s.Type)
132-
}
133-
134-
// validateType enforces block objects for element and block parameters
114+
// MixedElementType identifies text objects as context elements.
135115
func (s TextBlockObject) MixedElementType() MixedElementType {
136116
return MixedElementText
137117
}
@@ -207,11 +187,6 @@ type ConfirmationBlockObject struct {
207187
Style Style `json:"style,omitempty"`
208188
}
209189

210-
// validateType enforces block objects for element and block parameters
211-
func (s ConfirmationBlockObject) validateType() MessageObjectType {
212-
return motConfirmation
213-
}
214-
215190
// WithStyle add styling to confirmation object
216191
func (s *ConfirmationBlockObject) WithStyle(style Style) *ConfirmationBlockObject {
217192
s.Style = style
@@ -247,11 +222,6 @@ func NewOptionBlockObject(value string, text, description *TextBlockObject) *Opt
247222
}
248223
}
249224

250-
// validateType enforces block objects for element and block parameters
251-
func (s OptionBlockObject) validateType() MessageObjectType {
252-
return motOption
253-
}
254-
255225
// OptionGroupBlockObject Provides a way to group options in a select menu.
256226
//
257227
// More Information: https://api.slack.com/reference/messaging/composition-objects#option-group
@@ -260,11 +230,6 @@ type OptionGroupBlockObject struct {
260230
Options []*OptionBlockObject `json:"options"`
261231
}
262232

263-
// validateType enforces block objects for element and block parameters
264-
func (s OptionGroupBlockObject) validateType() MessageObjectType {
265-
return motOptionGroup
266-
}
267-
268233
// NewOptionGroupBlockElement returns an instance of a new option group block element
269234
func NewOptionGroupBlockElement(label *TextBlockObject, options ...*OptionBlockObject) *OptionGroupBlockObject {
270235
return &OptionGroupBlockObject{
@@ -283,11 +248,6 @@ type SlackIconObject struct {
283248
Name string `json:"name"`
284249
}
285250

286-
// validateType enforces block objects for element and block parameters
287-
func (s SlackIconObject) validateType() MessageObjectType {
288-
return MessageObjectType(s.Type)
289-
}
290-
291251
// NewSlackIconObject returns an instance of a new Slack icon object. The name
292252
// must be one of the icon names supported by Slack.
293253
func NewSlackIconObject(name string) *SlackIconObject {

0 commit comments

Comments
 (0)