Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix block_object typo and refactor to use static type #762

Merged
merged 2 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion block_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func (e *ContextElements) UnmarshalJSON(data []byte) error {
}

switch contextElementType {
case PlainTextType, MarkdownType:
case string(MBTPlainText), string(MBTMarkdown):
elem, err := unmarshalBlockObject(r, &TextBlockObject{})
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion block_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestNewImageBlock(t *testing.T) {
imageBlock := NewImageBlock("https://api.slack.com/img/blocks/bkb_template_images/tripAgentLocationMarker.png", "Marker", "test", imageText)

assert.Equal(t, string(imageBlock.Type), "image")
assert.Equal(t, imageBlock.Title.Type, "plain_text")
assert.Equal(t, string(imageBlock.Title.Type), "plain_text")
assert.Equal(t, imageBlock.BlockID, "test")
assert.Contains(t, imageBlock.Title.Text, "Location")
assert.Contains(t, imageBlock.ImageURL, "tripAgentLocationMarker.png")
Expand Down
21 changes: 9 additions & 12 deletions block_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

// blockObject object types
const (
MarkdownType = "mrkdwn"
PlainTextType = "plain_text"
MBTMarkdown MessageBlockType = "mrkdwn"
MBTPlainText MessageBlockType = "plain_text"
// The following objects don't actually have types and their corresponding
// const values are just for internal use
motConfirmation = "confirm"
Expand Down Expand Up @@ -55,7 +55,7 @@ func (b *BlockObjects) UnmarshalJSON(data []byte) error {
blockObjectType := getBlockObjectType(obj)

switch blockObjectType {
case PlainTextType, MarkdownType:
case string(MBTPlainText), string(MBTMarkdown):
object, err := unmarshalBlockObject(r, &TextBlockObject{})
if err != nil {
return err
Expand Down Expand Up @@ -119,10 +119,10 @@ func unmarshalBlockObject(r json.RawMessage, object blockObject) (blockObject, e
//
// More Information: https://api.slack.com/reference/messaging/composition-objects#text
type TextBlockObject struct {
Type string `json:"type"`
Text string `json:"text"`
Emoji bool `json:"emoji,omitempty"`
Verbatim bool `json:"verbatim,omitempty"`
Type MessageBlockType `json:"type"`
Text string `json:"text"`
Emoji bool `json:"emoji,omitempty"`
Verbatim bool `json:"verbatim,omitempty"`
}

// validateType enforces block objects for element and block parameters
Expand All @@ -136,7 +136,7 @@ func (s TextBlockObject) MixedElementType() MixedElementType {
}

// NewTextBlockObject returns an instance of a new Text Block Object
func NewTextBlockObject(elementType, text string, emoji, verbatim bool) *TextBlockObject {
func NewTextBlockObject(elementType MessageBlockType, text string, emoji, verbatim bool) *TextBlockObject {
return &TextBlockObject{
Type: elementType,
Text: text,
Expand All @@ -147,10 +147,7 @@ func NewTextBlockObject(elementType, text string, emoji, verbatim bool) *TextBlo

// BlockType returns the type of the block
func (t TextBlockObject) BlockType() MessageBlockType {
if t.Type == "mrkdown" {
return MarkdownType
}
return PlainTextType
return t.Type
}

// ConfirmationBlockObject defines a dialog that provides a confirmation step to
Expand Down
2 changes: 1 addition & 1 deletion block_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestNewTextBlockObject(t *testing.T) {

textObject := NewTextBlockObject("plain_text", "test", true, false)

assert.Equal(t, textObject.Type, "plain_text")
assert.Equal(t, string(textObject.Type), "plain_text")
assert.Equal(t, textObject.Text, "test")
assert.True(t, textObject.Emoji, "Emoji property should be true")
assert.False(t, textObject.Verbatim, "Verbatim should be false")
Expand Down
2 changes: 1 addition & 1 deletion block_section_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestNewSectionBlock(t *testing.T) {
assert.Equal(t, string(sectionBlock.BlockID), "test_block")
assert.Equal(t, len(sectionBlock.Fields), 0)
assert.Nil(t, sectionBlock.Accessory)
assert.Equal(t, sectionBlock.Text.Type, "mrkdwn")
assert.Equal(t, string(sectionBlock.Text.Type), "mrkdwn")
assert.Contains(t, sectionBlock.Text.Text, "New Orleans")

}
Expand Down
4 changes: 2 additions & 2 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestPostMessage(t *testing.T) {
expected url.Values
}

blocks := []Block{NewContextBlock("context", NewTextBlockObject(PlainTextType, "hello", false, false))}
blocks := []Block{NewContextBlock("context", NewTextBlockObject(MBTPlainText, "hello", false, false))}
blockStr := `[{"type":"context","block_id":"context","elements":[{"type":"plain_text","text":"hello"}]}]`

tests := map[string]messageTest{
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestPostMessage(t *testing.T) {
}

func TestPostMessageWithBlocksWhenMsgOptionResponseURLApplied(t *testing.T) {
expectedBlocks := []Block{NewContextBlock("context", NewTextBlockObject(PlainTextType, "hello", false, false))}
expectedBlocks := []Block{NewContextBlock("context", NewTextBlockObject(MBTPlainText, "hello", false, false))}

http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/response-url", func(rw http.ResponseWriter, r *http.Request) {
Expand Down