-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
67 lines (53 loc) · 2.8 KB
/
collection.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
package streams
import (
"encoding/json"
"github.com/benpate/derp"
"github.com/benpate/hannibal/vocab"
"github.com/benpate/rosetta/mapof"
)
// Collection is a subtype of Object that represents ordered or unordered sets of Object or Link instances.
// https://www.w3.org/ns/activitystreams#Collection
type Collection struct {
Context Context `json:"@context,omitempty" bson:"context,omitempty"`
ID string `json:"id,omitempty" bson:"id,omitempty"`
Type string `json:"type,omitempty" bson:"type,omitempty"`
Summary string `json:"summary,omitempty" bson:"summary,omitempty"` // A natural language summarization of the object encoded as HTML. Multiple language tagged summaries may be provided.
TotalItems int `json:"totalItems,omitempty" bson:"totalItems,omitempty"` // A non-negative integer specifying the total number of objects contained by the logical view of the collection. This number might not reflect the actual number of items serialized within the Collection object instance.
Current string `json:"current,omitempty" bson:"current,omitempty"` // In a paged Collection, indicates the page that contains the most recently updated member items.
First string `json:"first,omitempty" bson:"first,omitempty"` // In a paged Collection, indicates the furthest preceding page of items in the collection.
Last string `json:"last,omitempty" bson:"last,omitempty"` // In a paged Collection, indicates the furthest proceeding page of the collection.
Items []any `json:"items,omitempty" bson:"items,omitempty"` // Identifies the items contained in a collection. The items might be ordered or unordered.
}
func NewCollection() Collection {
return Collection{
Context: DefaultContext(),
Type: vocab.CoreTypeCollection,
}
}
/******************************************
* JSON Marshalling
******************************************/
func (c *Collection) UnmarshalJSON(data []byte) error {
result := make(map[string]any)
if err := json.Unmarshal(data, &result); err != nil {
return derp.Wrap(err, "activitystreams.Collection.UnmarshalJSON", "Error unmarshalling JSON", string(data))
}
return c.UnmarshalMap(result)
}
func (c *Collection) UnmarshalMap(data mapof.Any) error {
if dataType := data.GetString("type"); dataType != vocab.CoreTypeCollection {
return derp.NewInternalError("activitystreams.Collection.UnmarshalMap", "Invalid type", dataType)
}
c.Type = vocab.CoreTypeCollection
c.Summary = data.GetString("summary")
c.TotalItems = data.GetInt("totalItems")
c.Current = data.GetString("current")
c.First = data.GetString("first")
c.Last = data.GetString("last")
if dataItems, ok := data["items"]; ok {
if items, ok := UnmarshalItems(dataItems); ok {
c.Items = items
}
}
return nil
}