-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.go
81 lines (69 loc) · 1.77 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package blockkit
import (
"encoding/json"
"fmt"
)
// Block block type
type Block interface {
// FIXME: もっと意味のあるインターフェースにしたい
IsBlock() bool
}
// SectionBlock https://api.slack.com/reference/block-kit/blocks#section
type SectionBlock struct {
Text TextObject `json:"text"`
}
// IsBlock is block?
func (sb SectionBlock) IsBlock() bool {
return true
}
// MarshalJSON fills default values and run normal marshal
func (sb SectionBlock) MarshalJSON() ([]byte, error) {
type Alias SectionBlock
return json.Marshal(struct {
Alias
Type string `json:"type"`
}{
Alias: (Alias)(sb),
Type: "section",
})
}
// ContextBlock https://api.slack.com/reference/block-kit/blocks#context
type ContextBlock struct {
Elements []ContextBlockElement `json:"elements"`
}
// ContextBlockElement represents children of context block
type ContextBlockElement interface {
AvailableAsContextBlockElement() bool
}
// IsBlock is block?
func (cb ContextBlock) IsBlock() bool {
return true
}
// MarshalJSON validate and fills default values and run normal marshal
func (cb ContextBlock) MarshalJSON() ([]byte, error) {
if len(cb.Elements) > 10 {
return nil, fmt.Errorf("context can include 10 or less elements, but %d elements given", len(cb.Elements))
}
type Alias ContextBlock
return json.Marshal(struct {
Alias
Type string `json:"type"`
}{
Alias: (Alias)(cb),
Type: "context",
})
}
// DividerBlock https://api.slack.com/reference/block-kit/blocks#divider
type DividerBlock struct{}
// IsBlock is block?
func (db DividerBlock) IsBlock() bool {
return true
}
// MarshalJSON fills default values and run normal marshal
func (db DividerBlock) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type string `json:"type"`
}{
Type: "divider",
})
}