Repository: slack-go/slack
Pull request #762 (2020-07-15 18:45:38)

Fix block_object typo and refactor to use static type
##### Pull Request Guidelines

These are recommendations for pull requests.
They are strictly guidelines to help manage expectations.

##### PR preparation
Run `make pr-prep` from the root of the repository to run formatting, linting and tests.

##### Should this be an issue instead
- [ ] is it a convenience method? (no new functionality, streamlines some use case)
- [ ] exposes a previously private type, const, method, etc.
- [ ] is it application specific (caching, retry logic, rate limiting, etc)
- [ ] is it performance related.

##### API changes

Since API changes have to be maintained they undergo a more detailed review and are more likely to require changes.

- no tests, if you're adding to the API include at least a single test of the happy case.
- If you can accomplish your goal without changing the API, then do so.
- dependency changes. updates are okay. adding/removing need justification.

###### Examples of API changes that do not meet guidelines:
- in library cache for users. caches are use case specific.
- Convenience methods for Sending Messages, update, post, ephemeral, etc. consider opening an issue instead.



Patch:
diff --git a/block_conv.go b/block_conv.go
index 656a9ab..4f11228 100644
--- a/block_conv.go
+++ b/block_conv.go
@@ -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
diff --git a/block_object.go b/block_object.go
index b6f1da4..d3f5074 100644
--- a/block_object.go
+++ b/block_object.go
@@ -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"
@@ -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
@@ -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
@@ -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,
@@ -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


Test patch:
diff --git a/block_image_test.go b/block_image_test.go
index 3cdd203..a52ec16 100644
--- a/block_image_test.go
+++ b/block_image_test.go
@@ -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")
diff --git a/block_object_test.go b/block_object_test.go
index 31d1905..3b001e5 100644
--- a/block_object_test.go
+++ b/block_object_test.go
@@ -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")
diff --git a/block_section_test.go b/block_section_test.go
index 3f9f452..5539687 100644
--- a/block_section_test.go
+++ b/block_section_test.go
@@ -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")
 
 }
diff --git a/chat_test.go b/chat_test.go
index 450cae7..ca2f697 100644
--- a/chat_test.go
+++ b/chat_test.go
@@ -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{
@@ -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) {