-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.go
1272 lines (1041 loc) · 25.5 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package slack
import (
"net/url"
)
type BlockType string
const (
ActionsBlock BlockType = "actions"
ContextBlock BlockType = "context"
DividerBlock BlockType = "divider"
FileBlock BlockType = "file"
HeaderBlock BlockType = "header"
ImageBlock BlockType = "image"
InputBlock BlockType = "input"
SectionBlock BlockType = "section"
VideoBlock BlockType = "video"
)
// stringer for BlockType
func (b BlockType) String() string {
return string(b)
}
type Block interface {
BlockRender()
}
func (a Actions) BlockRender() {}
func (c Context) BlockRender() {}
func (d Divider) BlockRender() {}
func (f File) BlockRender() {}
func (h Header) BlockRender() {}
func (i Image) BlockRender() {}
func (i Input) BlockRender() {}
func (s Section) BlockRender() {}
func (v Video) BlockRender() {}
type Actions struct {
slackType BlockType
elements []Element
blockId string
optionals actionOptions
}
func (b Button) Actions() Actions {
return NewAction().AddElement(b)
}
func NewAction() Actions {
return Actions{
slackType: ActionsBlock,
elements: []Element{},
optionals: actionOptions{
blockId: false,
},
}
}
// SetBlockId sets the block id for the block.
func (a *Actions) setBlockId(blockId string) {
a.blockId = blockId
a.optionals.blockId = true
}
func (a *Actions) removeBlockId() {
a.blockId = ""
a.optionals.blockId = false
}
func (a *Actions) addElement(element Element) {
a.elements = append(a.elements, element)
}
type actionOptions struct {
blockId bool
}
type actionAbstraction struct {
Type string
Elements []Element
BlockId string
Optionals actionOptions
}
// AddBlockId chain function to add block id to an existing action block
func (a Actions) AddBlockId(blockId string) Actions {
a.setBlockId(blockId)
return a
}
// RemoveBlockId remove add block id from action block
func (a Actions) RemoveBlockId() Actions {
a.removeBlockId()
return a
}
// AddElement Add element to existing action block.
func (a Actions) AddElement(element Element) Actions {
a.addElement(element)
return a
}
// generate abstraction from action
func (a Actions) abstraction() actionAbstraction {
return actionAbstraction{
Type: string(a.slackType),
Elements: a.elements,
BlockId: a.blockId,
Optionals: a.optionals,
}
}
func (a actionAbstraction) Template() string {
return `{
"type": "{{.Type}}",
"elements": [{{range $index, $element := .Elements}}{{if $index}},{{end}}{{$element.Render}}{{end}}]
{{if .BlockId}},
"block_id": "{{.BlockId}}"
{{end}}
}`
}
// Render the block
func (a Actions) Render() string {
raw := Render(a.abstraction())
return Pretty(raw)
}
type ActionType interface {
Action()
}
///////////////////////////////////////////
// Context
// ContextElement Context allows for only two type of elements, either Text or Image elements as defined here:
//
// https://api.slack.com/reference/block-kit/blocks#context.
type ContextElement interface {
ContextElement()
}
func (i ImageElement) ContextElement() {}
func (m CompositionText) ContextElement() {}
func (i ImageElement) Context() Context {
return NewContext().AddElement(i)
}
func (m CompositionText) Context() Context {
return NewContext().AddElement(m)
}
type Context struct {
slackType BlockType
elements []ContextElement
blockId string
optionals contextOptions
}
type contextOptions struct {
BlockId bool
}
// NewContext creates a new context.
func NewContext() Context {
return Context{
slackType: ContextBlock,
elements: []ContextElement{},
optionals: contextOptions{
BlockId: false,
},
}
}
// setBlockId sets the block id for the block.
func (c *Context) setBlockId(blockId string) {
c.blockId = blockId
c.optionals.BlockId = true
}
// removeBlockId removes the block id from the context.
func (c *Context) removeBlockId() {
c.optionals.BlockId = false
}
// AddBlockId chain function to add block id to an existing context
func (c Context) AddBlockId(blockId string) Context {
c.setBlockId(blockId)
return c
}
// RemoveBlockId remove add block id from context
func (c Context) RemoveBlockId() Context {
c.removeBlockId()
return c
}
// addElement adds an element to the context.
func (c *Context) addElement(element ContextElement) {
c.elements = append(c.elements, element)
}
// AddElement chain function to add an element to an existing context
func (c Context) AddElement(element ContextElement) Context {
c.addElement(element)
return c
}
// contextAbstraction is the abstraction of the context block.
type contextAbstraction struct {
Type string
BlockId string
Elements []ContextElement
Optionals contextOptions
}
// BlockRenderAbstraction is the implementation of the BlockRenderAbstraction interface.
func (c Context) abstraction() contextAbstraction {
return contextAbstraction{
Type: c.slackType.String(),
BlockId: c.blockId,
Elements: c.elements,
Optionals: c.optionals,
}
}
// Render
func (c Context) Render() string {
raw := Render(c.abstraction())
return Pretty(raw)
}
// Template is the template for the context block.
func (c contextAbstraction) Template() string {
return `{
"type": "{{.Type}}",
"elements": [{{range $index, $element := .Elements}}{{if $index}},{{end}}{{$element.Render}}{{end}}]
{{if .Optionals.BlockId}},
"block_id": "{{.BlockId}}"
{{end}}
}`
}
///////////////////////////////////////////
// Divider
type Divider struct {
slackType BlockType // required
blockId string // optional
optionals dividerOptionals
}
func NewDividerBlock() Divider {
divider := Divider{
slackType: DividerBlock,
optionals: dividerOptionals{
BlockId: false,
},
}
return divider
}
// SetBlockId sets the block id for the block.
func (d *Divider) setBlockId(blockId string) {
d.blockId = blockId
d.optionals.BlockId = true
}
func (d *Divider) removeBlockId() {
d.blockId = ""
d.optionals.BlockId = false
}
// Render renders the block to a string.
func (d Divider) Render() string {
raw := Render(d.abstraction())
return Pretty(raw)
}
// SlackType return slack type
func (d Divider) Type() string {
return string(d.slackType)
}
func (d dividerAbstraction) Template() string {
return `{
"type": "{{.Type}}"
{{if .Optionals.BlockId}},"block_id": "{{.BlockId}}"{{end}}
}`
}
type dividerAbstraction struct {
Type string
BlockId string
Optionals dividerOptionals
}
// create an abstraction of the divider block
func (d Divider) abstraction() dividerAbstraction {
return dividerAbstraction{
Type: d.Type(),
BlockId: d.blockId,
Optionals: d.optionals,
}
}
type dividerOptionals struct {
BlockId bool
}
// SetBlockId sets the block id for the block.
func (d Divider) SetBlockId(blockId string) Divider {
d.setBlockId(blockId)
return d
}
// RemoveBlockId removes the block id from the block.
func (d Divider) RemoveBlockId() Divider {
d.blockId = ""
d.optionals.BlockId = false
return d
}
///////////////////////////////////////////
// File
type File struct {
slackType BlockType
externalId string
source string
blockId string
optionals fileOptions
}
type fileOptions struct {
BlockId bool
}
// NewFile creates a new file.
func NewFile(externalId string, source string) File {
return File{
slackType: FileBlock,
externalId: externalId,
source: source,
optionals: fileOptions{
BlockId: false,
},
}
}
// SetBlockId sets the block id for the block.
func (f *File) setBlockId(blockId string) {
f.blockId = blockId
f.optionals.BlockId = true
}
func (f *File) removeBlockId() {
f.blockId = ""
f.optionals.BlockId = false
}
// AddBlockId chain function to add block id to an existing file
func (f File) AddBlockId(blockId string) File {
f.setBlockId(blockId)
return f
}
// RemoveBlockId remove add block id from file
func (f File) RemoveBlockId() File {
f.removeBlockId()
return f
}
// fileAbstraction is a helper struct to generate the abstraction for the file.
type fileAbstraction struct {
Type string
ExternalId string
Source string
BlockId string
Optionals fileOptions
}
// abstraction is a helper function to generate the abstraction for the file.
func (f File) abstraction() fileAbstraction {
return fileAbstraction{
Type: string(f.slackType),
ExternalId: f.externalId,
Source: f.source,
BlockId: f.blockId,
Optionals: f.optionals,
}
}
// template is a helper function to generate the template for the file.
func (f fileAbstraction) Template() string {
return `{
"type": "{{.Type}}",
"external_id": "{{.ExternalId}}",
"source": "{{.Source}}"{{if .Optionals.BlockId}},
"block_id": "{{.BlockId}}"{{end}}
}`
}
// render is a helper function to generate the json for the file.
func (f File) Render() string {
raw := Render(f.abstraction())
return Pretty(raw)
}
///////////////////////////////////////////
// Header
type Header struct {
slackType BlockType
text CompositionText
blockId string
optionals headerOptions
}
type headerOptions struct {
BlockId bool
}
// NewHeader creates a new header.
func NewHeader(text string) Header {
return Header{
slackType: HeaderBlock,
text: NewPlainText(text),
optionals: headerOptions{
BlockId: false,
},
}
}
// SetBlockId sets the block id for the block.
func (h *Header) setBlockId(blockId string) {
h.blockId = blockId
h.optionals.BlockId = true
}
func (h *Header) removeBlockId() {
h.blockId = ""
h.optionals.BlockId = false
}
// AddBlockId chain function to add block id to an existing header
func (h Header) AddBlockId(blockId string) Header {
h.setBlockId(blockId)
return h
}
// RemoveBlockId remove add block id from header
func (h Header) RemoveBlockId() Header {
h.removeBlockId()
return h
}
// abstraction is a helper struct to generate the abstraction for the header.
type headerAbstraction struct {
Type string
Text CompositionText
BlockId string
Optionals headerOptions
}
// generate abstraction from header
func (h Header) abstraction() headerAbstraction {
return headerAbstraction{
Type: string(h.slackType),
Text: h.text,
BlockId: h.blockId,
Optionals: h.optionals,
}
}
// template is a helper struct to generate the template for the header.
func (f headerAbstraction) Template() string {
return `{
"type": "{{.Type}}",
"text": {{.Text.Render}}{{if .Optionals.BlockId}},
"block_id": "{{.BlockId}}"
{{end}}
}`
}
// render is a helper function to generate the json for the file.
func (h Header) Render() string {
raw := Render(h.abstraction())
return Pretty(raw)
}
// BlockRender is a dummy function to satisfy the Block interface.
type Image struct {
slackType BlockType
imageUrl *url.URL // todo: make this a string set using a url
altText string
title CompositionText
blockId string
optionals imageOptions
}
type imageOptions struct {
Title bool
BlockId bool
}
// NewImage creates a new image.
func NewImage(imageUrl *url.URL, altText string) Image {
return Image{
slackType: ImageBlock,
imageUrl: imageUrl,
altText: altText,
optionals: imageOptions{
Title: false,
BlockId: false,
},
}
}
// SetTitle sets the title for the image.
func (i *Image) setTitle(title string) {
i.title = NewPlainText(title)
i.optionals.Title = true
}
func (i *Image) removeTitle() {
i.optionals.Title = false
}
// AddTitle chain function to add title to an existing image
func (i Image) AddTitle(title string) Image {
i.setTitle(title)
return i
}
// RemoveTitle remove add title from image
func (i Image) RemoveTitle() Image {
i.removeTitle()
return i
}
// SetBlockId sets the block id for the block.
func (i *Image) setBlockId(blockId string) {
i.blockId = blockId
i.optionals.BlockId = true
}
func (i *Image) removeBlockId() {
i.optionals.BlockId = false
}
// AddBlockId chain function to add block id to an existing image
func (i Image) AddBlockId(blockId string) Image {
i.setBlockId(blockId)
return i
}
// RemoveBlockId remove add block id from image
func (i Image) RemoveBlockId() Image {
i.removeBlockId()
return i
}
// abstraction is a helper struct to generate the abstraction for the image.
type imageAbstraction struct {
Type string
ImageUrl string
AltText string
Title CompositionText
BlockId string
Optionals imageOptions
}
// generate abstraction from image
func (i Image) abstraction() imageAbstraction {
return imageAbstraction{
Type: i.slackType.String(),
ImageUrl: i.imageUrl.String(),
AltText: i.altText,
Title: i.title,
BlockId: i.blockId,
Optionals: i.optionals,
}
}
// Template returns the template for the image.
func (i imageAbstraction) Template() string {
return `{
"type": "{{.Type}}",
"image_url": "{{.ImageUrl}}",
"alt_text": "{{.AltText}}"{{if .Optionals.Title}},
"title": {{.Title.Render}}{{end}}{{if .Optionals.BlockId}},
"block_id": "{{.BlockId}}"{{end}}
}`
}
// Render renders the image to a string.
func (i Image) Render() string {
raw := Render(i.abstraction())
return Pretty(raw)
}
///////////////////////////////////////////
// Input
type Input struct {
slackType BlockType
label CompositionText
element InputElement
dispatchAction bool
blockID string
hint CompositionText
slack bool
optionals inputOptional
}
type inputOptional struct {
DispatchAction bool
BlockID bool
Hint bool
Slack bool
}
func NewInput(label string, element InputElement) Input {
return Input{
slackType: InputBlock,
label: NewPlainText(label),
element: element,
}
}
// set dispatch action
func (i *Input) setDispatchAction(dispatchAction bool) {
i.dispatchAction = dispatchAction
i.optionals.DispatchAction = true
}
// set block id
func (i *Input) setBlockID(blockID string) {
i.blockID = blockID
i.optionals.BlockID = true
}
// remove block id
func (i *Input) removeBlockID() {
i.optionals.BlockID = false
}
// setHint
func (i *Input) setHint(s string) {
i.hint = NewPlainText(s)
i.optionals.Hint = true
}
// removeHint remove hint
func (i *Input) removeHint() {
i.optionals.Hint = false
}
// set label
func (i *Input) setLabel(label string) {
i.label = NewPlainText(label)
}
// SetSlackset slack optional
func (i *Input) setSlackOptional() {
i.slack = true
i.optionals.Slack = true
}
// RemoveSlackremove slack optional
func (i *Input) removeSlackOptional() {
i.optionals.Slack = false
}
type abstractionInput struct {
Type string
Label CompositionText
Element InputElement
DispatchAction bool
BlockID string
Hint CompositionText
Slack bool
Optionals inputOptional
}
// create abstraction input
func (i Input) abstraction() abstractionInput {
return abstractionInput{
Type: i.slackType.String(),
Label: i.label,
Element: i.element,
DispatchAction: i.dispatchAction,
BlockID: i.blockID,
Hint: i.hint,
Slack: i.slack,
Optionals: i.optionals,
}
}
// Template for input
func (i abstractionInput) Template() string {
return `{
"type": "{{.Type}}",
"label": {{.Label.Render}},
"element": {{.Element.Render}}
{{if .Optionals.DispatchAction}},
"dispatch_action": "{{.DispatchAction}}"
{{end}}
{{if .Optionals.BlockID}},
"block_id": "{{.BlockID}}"
{{end}}
{{if .Optionals.Hint}},
"hint": {{.Hint.Render}}
{{end}}
{{if .Optionals.SlackOptional}},
"optionals": "{{.SlackOptional}}"
{{end}}
}`
}
// Render render input
func (i Input) Render() string {
raw := Render(i.abstraction())
return Pretty(raw)
}
type InputType interface {
Input()
}
func (i Input) SetLabel(label string) Input {
i.setLabel(label)
return i
}
// EnableDispatchAction bool as chain method
func (i Input) EnableDispatchAction() Input {
i.setDispatchAction(true)
return i
}
// DisableDispatchAction bool as chain method
func (i Input) DisableDispatchAction() Input {
i.setDispatchAction(false)
return i
}
// SetBlockID BlockID string as chain method
func (i Input) SetBlockID(blockID string) Input {
i.setBlockID(blockID)
return i
}
// RemoveBlockID string as chain method
func (i Input) RemoveBlockID() Input {
i.removeBlockID()
return i
}
// SetHint as chain method
func (i Input) SetHint(s string) Input {
i.setHint(s)
return i
}
// RemoveHint as chain method
func (i Input) RemoveHint() Input {
i.removeHint()
return i
}
// SetSlackas chain method
func (i Input) MakeOptional() Input {
i.setSlackOptional()
return i
}
func (i Input) RemoveOptional() Input {
i.removeSlackOptional()
return i
}
///////////////////////////////////////////
// Section
type Section struct {
slackType BlockType
text CompositionText
accessory Element
blockId string
fields []CompositionText
optionals sectionOptions
}
type sectionOptions struct {
Text bool
Accessory bool
BlockId bool
Field bool
}
// NewSection creates a new section.
func NewSection() Section {
return Section{
slackType: SectionBlock,
optionals: sectionOptions{
Text: false,
Accessory: false,
BlockId: false,
Field: false,
},
}
}
// SetAccessory sets the accessory for the section.
func (s *Section) setAccessory(accessory Element) {
s.accessory = accessory
s.optionals.Accessory = true
}
func (s *Section) removeAccessory() {
s.optionals.Accessory = false
}
// addAccessory adds an accessory to the section.
func (s Section) AddAccessory(accessory Element) Section {
s.setAccessory(accessory)
return s
}
// SetBlockId sets the block id for the section.
func (s Section) SetBlockId(blockId string) Section {
s.setBlockId(blockId)
return s
}
// setBlockId sets the block id for the section.
func (s *Section) setBlockId(blockId string) {
s.blockId = blockId
s.optionals.BlockId = true
}
func (s *Section) removeBlockId() {
s.optionals.BlockId = false
}
// AddField adds a field to the section.
func (s *Section) addPlainField(field string) {
s.fields = append(s.fields, NewPlainText(field))
s.optionals.Field = true
}
func (s *Section) addMarkdownField(field string) {
s.fields = append(s.fields, NewMrkdwnText(field))
s.optionals.Field = true
}
func (s *Section) addMarkdownFieldText(field string) {
s.text = NewMrkdwnText(field)
s.optionals.Text = true
}
func (s *Section) removeField() {
s.optionals.Field = false
}
// AddPlainField adds a field to the section.
func (s Section) AddPlainField(field string) Section {
s.addPlainField(field)
return s
}
// AddMrkdownField adds a field to the section.
func (s Section) AddMrkdownField(field string) Section {
s.addMarkdownField(field)
return s
}
// AddMrkdownField adds a field text to the section.
func (s Section) AddMrkdownFieldText(field string) Section {
s.addMarkdownFieldText(field)
return s
}
// setText
func (s *Section) setText(text CompositionText) {
s.text = text
s.optionals.Text = true
}
func (s Section) SetText(text CompositionText) Section {
s.setText(text)
return s
}
// abstraction for the section block
type sectionAbstraction struct {
Type string
Text CompositionText
Accessory Element
BlockId string
Fields []CompositionText
Optionals sectionOptions
}
// abstraction
func (s Section) abstraction() sectionAbstraction {
return sectionAbstraction{
Type: s.slackType.String(),
Text: s.text,
Accessory: s.accessory,
BlockId: s.blockId,
Fields: s.fields,
Optionals: s.optionals,
}
}
// Template returns the template for the section.
func (s sectionAbstraction) Template() string {
return `{
"type": "{{.Type}}"
{{if .Optionals.Text}},
"text": {{.Text.Render}}
{{end}}
{{if .Optionals.Accessory}},
"accessory": {{.Accessory.Render}}
{{end}}
{{if .Optionals.BlockId}},
"block_id": "{{.BlockId}}"
{{end}}
{{if .Optionals.Field}},
"fields": [
{{range $index, $field := .Fields}}{{if $index}},{{end}}{{ $field.Render}}{{end}}
]{{end}}
}`
}
// Render renders the section to a string
func (s Section) Render() string {
raw := Render(s.abstraction())
return Pretty(raw)
}
type SectionType interface {
Section()
}
func (s Section) String() string {
return s.Render()
}
///////////////////////////////////////////
// Video
type Video struct {
slackType BlockType
title CompositionText