Skip to content

Commit

Permalink
Move block context unmarshalling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alyosha authored and james-lawrence committed May 8, 2019
1 parent 55e04f3 commit c181a3b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 48 deletions.
44 changes: 0 additions & 44 deletions block_context.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package slack

import (
"encoding/json"
)

// ContextBlock defines data that is used to display message context, which can
// include both images and text.
//
Expand Down Expand Up @@ -32,43 +28,3 @@ func NewContextBlock(blockID string, elements ContextElements) *ContextBlock {
Elements: elements,
}
}

// 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
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}

for _, r := range raw {
var obj map[string]interface{}
err := json.Unmarshal(r, &obj)
if err != nil {
return err
}

contextElementType := ""
if t, ok := obj["type"].(string); ok {
contextElementType = t
}

switch contextElementType {
case PlainTextType, MarkdownType:
elem, err := unmarshalBlockObject(r, &TextBlockObject{})
if err != nil {
return err
}
e.TextObjects = append(e.TextObjects, elem.(*TextBlockObject))
case "image":
elem, err := unmarshalBlockElement(r, &ImageBlockElement{})
if err != nil {
return err
}
e.ImageElements = append(e.ImageElements, elem.(*ImageBlockElement))
}
}

return nil
}
48 changes: 44 additions & 4 deletions block_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package slack

import "encoding/json"

// Conv/JSON encoding logic for Blocks
// Marshalling/unmarshalling logic for Blocks

// UnmarshalJSON implements the Unmarshaller interface for Blocks, so that any JSON
// unmarshalling is delegated and proper type determination can be made before unmarshal
Expand Down Expand Up @@ -87,7 +87,7 @@ func (b *Blocks) appendToBlocks(appendBlocks []Block) {
}
}

// Conv/JSON encoding logic for BlockElements
// Marshalling/unmarshalling logic for BlockElements

// MarshalJSON implements the Marshaller interface for BlockElements so that any JSON
// marshalling is delegated and proper type determination can be made before marshal
Expand Down Expand Up @@ -204,7 +204,7 @@ func toBlockElementSlice(elements *BlockElements) []BlockElement {
return slice
}

// Conv/JSON encoding related logic for Accessory
// Marshalling/unmarshalling logic for Accessory

// MarshalJSON implements the Marshaller interface for Accessory so that any JSON
// marshalling is delegated and proper type determination can be made before marshal
Expand Down Expand Up @@ -293,7 +293,7 @@ func toBlockElement(element *Accessory) BlockElement {
return nil
}

// Conv/JSON encoding related logic for ContextElements
// Marshalling/unmarsalling logic for ContextElements

// MarshalJSON implements the Marshaller interface for ContextElements so that any JSON
// marshalling is delegated and proper type determination can be made before marshal
Expand All @@ -317,3 +317,43 @@ func toMixedElements(elements *ContextElements) []mixedElement {

return slice
}

// 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
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}

for _, r := range raw {
var obj map[string]interface{}
err := json.Unmarshal(r, &obj)
if err != nil {
return err
}

contextElementType := ""
if t, ok := obj["type"].(string); ok {
contextElementType = t
}

switch contextElementType {
case PlainTextType, MarkdownType:
elem, err := unmarshalBlockObject(r, &TextBlockObject{})
if err != nil {
return err
}
e.TextObjects = append(e.TextObjects, elem.(*TextBlockObject))
case "image":
elem, err := unmarshalBlockElement(r, &ImageBlockElement{})
if err != nil {
return err
}
e.ImageElements = append(e.ImageElements, elem.(*ImageBlockElement))
}
}

return nil
}

0 comments on commit c181a3b

Please sign in to comment.