-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasvocab_interfaces.go
118 lines (101 loc) · 2.4 KB
/
asvocab_interfaces.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
package astreams
import (
"encoding/json"
"io"
"reflect"
)
type JsonPayload struct {
Type string `json:"type"`
}
// ActivityStreamer is a generic type constraint representing all valid Activity Streams 2.0 types
type ActivityStreamer interface {
Object | Link | Actor | Activity | IntransitiveActivity | Collection | CollectionPage |
OrderedCollection | OrderedCollectionPage | Location | Icon | Image | Place | Profile |
Tombstone | Relationship | Question
}
// DecodePayloadObjectType can be used to check the specific type of unknown JSON payload
/*
payloadMeta, err := DecodePayloadObjectType(payload)
if err != nil {
//
}
switch payloadMeta.Type {
case "Note":
var note Note
err = json.Unmarshal([]byte(tc), ¬e)
if err != nil {
//
}
case "Offer":
var offer Offer
err = json.Unmarshal([]byte(tc), &offer)
if err != nil {
//
}
case "Person":
var person Person
err = json.Unmarshal([]byte(tc), &person)
if err != nil {
//
}
if person.Name == "" {
//
}
default:
var obj ObjectOrLinkOrString
err = json.Unmarshal([]byte(tc), &obj)
if err != nil {
//
}
}
*/
func DecodePayloadObjectType(payload io.Reader) (JsonPayload, error) {
var payloadType JsonPayload
err := json.NewDecoder(payload).Decode(&payloadType)
if err != nil {
return payloadType, err
}
return payloadType, nil
}
// ObjectLinker can be either a (sub)type of 'Object' or a (sub)type of 'Link'
type ObjectLinker interface {
IsObject() bool
IsLink() bool
GetObject() *Object
GetLink() *Link
}
// ConcreteType returns both, the type name obtained using reflection,
// and the Type property of the Object / Link JSON payload.
// The object's own Type property is going to be more specific, so use that where useful.
func ConcreteType(t ObjectLinker) (reflectType, astreamsType string) {
if t.IsLink() {
return reflect.TypeOf(t).Name(), t.GetLink().Type
}
return reflect.TypeOf(t).Name(), t.GetObject().Type
}
// Implements 'ObjectLinker' interface for 'Object'
func (o Object) IsObject() bool {
return true
}
func (o Object) IsLink() bool {
return false
}
func (o Object) GetObject() *Object {
return &o
}
func (o Object) GetLink() *Link {
return nil
}
// Implements 'ObjectLinker' interface for 'Link'
func (l Link) IsObject() bool {
return false
}
func (l Link) IsLink() bool {
return true
}
func (l Link) GetObject() *Object {
return nil
}
func (l Link) GetLink() *Link {
return &l
}