-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcomponent-json.go
More file actions
243 lines (219 loc) · 5.4 KB
/
Copy pathcomponent-json.go
File metadata and controls
243 lines (219 loc) · 5.4 KB
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
package tempest
import (
"encoding/json"
"fmt"
)
// UnmarshalComponent inspects the "type" field and returns the proper component struct.
func UnmarshalComponent(data []byte) (AnyComponent, error) {
var partial struct {
Type ComponentType `json:"type"`
}
if err := json.Unmarshal(data, &partial); err != nil {
return nil, err
}
switch partial.Type {
case BUTTON_COMPONENT_TYPE:
var cmp ButtonComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case STRING_SELECT_COMPONENT_TYPE:
var cmp StringSelectComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case TEXT_INPUT_COMPONENT_TYPE:
var cmp TextInputComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case USER_SELECT_COMPONENT_TYPE,
ROLE_SELECT_COMPONENT_TYPE,
MENTIONABLE_SELECT_COMPONENT_TYPE,
CHANNEL_SELECT_COMPONENT_TYPE:
var cmp SelectComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case SECTION_COMPONENT_TYPE:
var cmp SectionComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case TEXT_DISPLAY_COMPONENT_TYPE:
var cmp TextDisplayComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case THUMBNAIL_COMPONENT_TYPE:
var cmp ThumbnailComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case MEDIA_GALLERY_COMPONENT_TYPE:
var cmp MediaGalleryComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case FILE_COMPONENT_TYPE:
var cmp FileComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case SEPARATOR_COMPONENT_TYPE:
var cmp SeparatorComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case CONTAINER_COMPONENT_TYPE:
var cmp ContainerComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case ACTION_ROW_COMPONENT_TYPE:
var cmp ActionRowComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case LABEL_COMPONENT_TYPE:
var cmp LabelComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case FILE_UPLOAD_COMPONENT_TYPE:
var cmp FileUploadComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case RADIO_GROUP_COMPONENT_TYPE:
var cmp RadioGroupComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case CHECKBOX_GROUP_COMPONENT_TYPE:
var cmp CheckboxGroupComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
case CHECKBOX_COMPONENT_TYPE:
var cmp CheckboxComponent
if err := json.Unmarshal(data, &cmp); err != nil {
return nil, err
}
return cmp, nil
}
return nil, fmt.Errorf("unknown component type: %d", partial.Type)
}
func (c *LabelComponent) UnmarshalJSON(data []byte) error {
type alias LabelComponent
var raw struct {
alias
Component json.RawMessage `json:"component"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
*c = LabelComponent(raw.alias)
parsed, err := UnmarshalComponent(raw.Component)
if err != nil {
return err
}
if cmp, ok := parsed.(LabelChildComponent); ok {
c.Component = cmp
}
raw.Component = nil
return nil
}
func (c *ActionRowComponent) UnmarshalJSON(data []byte) error {
type alias ActionRowComponent
var raw struct {
alias
Components []json.RawMessage `json:"components"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
*c = ActionRowComponent(raw.alias)
for _, comp := range raw.Components {
parsed, err := UnmarshalComponent(comp)
if err != nil {
return err
}
if cmp, ok := parsed.(ActionRowChildComponent); ok {
c.Components = append(c.Components, cmp)
}
}
raw.Components = nil
return nil
}
func (c *ContainerComponent) UnmarshalJSON(data []byte) error {
type alias ContainerComponent
var raw struct {
alias
Components []json.RawMessage `json:"components"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
*c = ContainerComponent(raw.alias)
for _, comp := range raw.Components {
parsed, err := UnmarshalComponent(comp)
if err != nil {
return err
}
if cmp, ok := parsed.(ContainerChildComponent); ok {
c.Components = append(c.Components, cmp)
}
}
raw.Components = nil
return nil
}
func (c *SectionComponent) UnmarshalJSON(data []byte) error {
type alias SectionComponent
var raw struct {
alias
Components []json.RawMessage `json:"components"`
Accessory *json.RawMessage `json:"accessory,omitempty"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
*c = SectionComponent(raw.alias)
for _, comp := range raw.Components {
parsed, err := UnmarshalComponent(comp)
if err != nil {
return err
}
if cmp, ok := parsed.(TextDisplayComponent); ok {
c.Components = append(c.Components, cmp)
}
}
if raw.Accessory != nil {
parsed, err := UnmarshalComponent(*raw.Accessory)
if err != nil {
return err
}
if cmp, ok := parsed.(AccessoryComponent); ok {
c.Accessory = cmp
}
}
raw.Components = nil
raw.Accessory = nil
return nil
}