-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathattachment.go
71 lines (59 loc) · 1.4 KB
/
attachment.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
package messenger
import (
"encoding/json"
)
type AttachmentType string
const (
AttachmentTypeTemplate AttachmentType = "template"
AttachmentTypeImage AttachmentType = "image"
AttachmentTypeVideo AttachmentType = "video"
AttachmentTypeAudio AttachmentType = "audio"
AttachmentTypeLocation AttachmentType = "location"
)
type Attachment struct {
Type AttachmentType `json:"type"`
Payload interface{} `json:"payload,omitempty"`
}
type rawAttachment struct {
Type AttachmentType `json:"type"`
Payload json.RawMessage `json:"payload"`
}
func (a *Attachment) UnmarshalJSON(b []byte) error {
raw := &rawAttachment{}
err := json.Unmarshal(b, raw)
if err != nil {
return err
}
a.Type = raw.Type
var payload interface{}
switch a.Type {
case AttachmentTypeLocation:
payload = &Location{}
case AttachmentTypeTemplate:
//TODO: implement template unmarshalling
a.Payload = raw.Payload
return nil
default:
payload = &Resource{}
}
err = json.Unmarshal(raw.Payload, payload)
if err != nil {
return err
}
a.Payload = payload
return nil
}
type Resource struct {
URL string `json:"url"`
Reusable bool `json:"is_reusable,omitempty"`
}
type ReusableAttachment struct {
AttachmentID string `json:"attachment_id"`
}
type Location struct {
Coordinates Coordinates `json:"coordinates"`
}
type Coordinates struct {
Latitude float64 `json:"lat"`
Longitude float64 `json:"long"`
}