Skip to content

Commit b80941d

Browse files
authored
refactor: simplify block decoder dispatch (#1697)
Switch directly on the decoded `type` field in the block, block element, accessory, and context element decoders. Remove four redundant temporary strings and their empty-string checks. Route unknown blocks through the same decode-and-append path as known blocks, preserving their raw JSON, ordering, and error behavior. This removes 25 lines without changing behavior or the public API. Validation: existing block and accessory tests pass, including unknown-block round trips and malformed input. Formatting, lint, module, and whitespace checks also pass.
1 parent 11eb9d3 commit b80941d

1 file changed

Lines changed: 7 additions & 32 deletions

File tree

‎block_conv.go‎

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@ func (b *Blocks) UnmarshalJSON(data []byte) error {
3636
return err
3737
}
3838

39-
var blockType string
40-
if s.TypeVal != "" {
41-
blockType = s.TypeVal
42-
}
43-
4439
var block Block
45-
switch blockType {
40+
switch s.TypeVal {
4641
case "actions":
4742
block = &ActionBlock{}
4843
case "context":
@@ -94,12 +89,7 @@ func (b *Blocks) UnmarshalJSON(data []byte) error {
9489
case "layout":
9590
block = &LayoutBlock{}
9691
default:
97-
b := &UnknownBlock{raw: r}
98-
if err = json.Unmarshal(r, b); err != nil {
99-
return err
100-
}
101-
blocks.BlockSet = append(blocks.BlockSet, b)
102-
continue
92+
block = &UnknownBlock{raw: r}
10393
}
10494

10595
err = json.Unmarshal(r, block)
@@ -214,13 +204,8 @@ func (b *BlockElements) UnmarshalJSON(data []byte) error {
214204
return err
215205
}
216206

217-
var blockElementType string
218-
if s.TypeVal != "" {
219-
blockElementType = s.TypeVal
220-
}
221-
222207
var blockElement BlockElement
223-
switch blockElementType {
208+
switch s.TypeVal {
224209
case "image":
225210
blockElement = &ImageBlockElement{}
226211
case "button":
@@ -260,7 +245,7 @@ func (b *BlockElements) UnmarshalJSON(data []byte) error {
260245
case "workflow_button":
261246
blockElement = &WorkflowButtonBlockElement{}
262247
default:
263-
return fmt.Errorf("unsupported block element type %v", blockElementType)
248+
return fmt.Errorf("unsupported block element type %v", s.TypeVal)
264249
}
265250

266251
err = json.Unmarshal(r, blockElement)
@@ -301,12 +286,7 @@ func (a *Accessory) UnmarshalJSON(data []byte) error {
301286
return err
302287
}
303288

304-
var blockElementType string
305-
if s.TypeVal != "" {
306-
blockElementType = s.TypeVal
307-
}
308-
309-
switch blockElementType {
289+
switch s.TypeVal {
310290
case "image":
311291
element := &ImageBlockElement{}
312292
if err := json.Unmarshal(r, element); err != nil {
@@ -457,12 +437,7 @@ func (e *ContextElements) UnmarshalJSON(data []byte) error {
457437
return err
458438
}
459439

460-
var contextElementType string
461-
if s.TypeVal != "" {
462-
contextElementType = s.TypeVal
463-
}
464-
465-
switch contextElementType {
440+
switch s.TypeVal {
466441
case PlainTextType, MarkdownType:
467442
elem := &TextBlockObject{}
468443
if err := json.Unmarshal(r, elem); err != nil {
@@ -478,7 +453,7 @@ func (e *ContextElements) UnmarshalJSON(data []byte) error {
478453

479454
e.Elements = append(e.Elements, elem)
480455
default:
481-
return fmt.Errorf("unsupported context element type %v", contextElementType)
456+
return fmt.Errorf("unsupported context element type %v", s.TypeVal)
482457
}
483458
}
484459

0 commit comments

Comments
 (0)