Adding Unmarshalling methods for CollectionPage and OrderedCollectionPage

This commit is contained in:
Marius Orcsik 2018-10-18 20:19:18 +02:00
parent cbad1a2add
commit c1f61e3a35
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
4 changed files with 104 additions and 11 deletions

View file

@ -2,9 +2,16 @@ package activitystreams
import (
"time"
"github.com/buger/jsonparser"
)
var validCollectionTypes = [...]ActivityVocabularyType{CollectionType, OrderedCollectionType}
var validCollectionTypes = [...]ActivityVocabularyType{
CollectionType,
OrderedCollectionType,
CollectionPageType,
OrderedCollectionPageType,
}
type CollectionInterface interface {
ObjectOrLink
@ -203,7 +210,7 @@ type OrderedCollection struct {
type CollectionPage struct {
Collection
// Identifies the Collection to which a CollectionPage objects items belong.
PartOf Item
PartOf Item `jsonld:"partOf,omitempty"`
// In a paged Collection, indicates the page that contains the most recently updated member items.
Current Item `jsonld:"current,omitempty"`
// In a paged Collection, indicates the furthest preceeding page of items in the collection.
@ -223,7 +230,7 @@ type CollectionPage struct {
type OrderedCollectionPage struct {
OrderedCollection
// Identifies the Collection to which a CollectionPage objects items belong.
PartOf Item
PartOf Item `jsonld:"partOf,omitempty"`
// In a paged Collection, indicates the page that contains the most recently updated member items.
Current Item `jsonld:"current,omitempty"`
// In a paged Collection, indicates the furthest preceeding page of items in the collection.
@ -274,6 +281,7 @@ func CollectionPageNew(parent CollectionInterface) *CollectionPage {
if pc, ok := parent.(*Collection); ok {
p.Collection = *pc
}
p.Type = CollectionPageType
return &p
}
@ -285,6 +293,7 @@ func OrderedCollectionPageNew(parent CollectionInterface) *OrderedCollectionPage
if pc, ok := parent.(*OrderedCollection); ok {
p.OrderedCollection = *pc
}
p.Type = OrderedCollectionPageType
return &p
}
@ -410,6 +419,34 @@ func (c *Collection) UnmarshalJSON(data []byte) error {
return nil
}
// UnmarshalJSON
func (o *OrderedCollectionPage) UnmarshalJSON(data []byte) error {
o.OrderedCollection.UnmarshalJSON(data)
o.Current = getAPItem(data, "current")
o.Next = getAPItem(data, "next")
o.Prev = getAPItem(data, "prev")
o.PartOf = getAPItem(data, "partOf")
o.First = getAPItem(data, "first")
o.Last = getAPItem(data, "last")
if si, err := jsonparser.GetInt(data, "startIndex"); err != nil {
o.StartIndex = uint(si)
}
return nil
}
// UnmarshalJSON
func (c *CollectionPage) UnmarshalJSON(data []byte) error {
c.Collection.UnmarshalJSON(data)
c.Current = getAPItem(data, "current")
c.Next = getAPItem(data, "next")
c.Prev = getAPItem(data, "prev")
c.PartOf = getAPItem(data, "partOf")
c.First = getAPItem(data, "first")
c.Last = getAPItem(data, "last")
return nil
}
/*
func (c *Collection) MarshalJSON() ([]byte, error) {
return nil, nil

View file

@ -26,14 +26,16 @@ type ObjectID IRI
const (
// ActivityBaseURI the basic URI for the activity streams namespaces
ActivityBaseURI = IRI("https://www.w3.org/ns/activitystreams")
ObjectType ActivityVocabularyType = "Object"
LinkType ActivityVocabularyType = "Link"
ActivityType ActivityVocabularyType = "Activity"
IntransitiveActivityType ActivityVocabularyType = "IntransitiveActivity"
ActorType ActivityVocabularyType = "Actor"
CollectionType ActivityVocabularyType = "Collection"
OrderedCollectionType ActivityVocabularyType = "OrderedCollection"
ActivityBaseURI = IRI("https://www.w3.org/ns/activitystreams")
ObjectType ActivityVocabularyType = "Object"
LinkType ActivityVocabularyType = "Link"
ActivityType ActivityVocabularyType = "Activity"
IntransitiveActivityType ActivityVocabularyType = "IntransitiveActivity"
ActorType ActivityVocabularyType = "Actor"
CollectionType ActivityVocabularyType = "Collection"
OrderedCollectionType ActivityVocabularyType = "OrderedCollection"
CollectionPageType ActivityVocabularyType = "CollectionPage"
OrderedCollectionPageType ActivityVocabularyType = "OrderedCollectionPage"
// Activity Pub Object Types
ArticleType ActivityVocabularyType = "Article"

View file

@ -0,0 +1,25 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "http://example.com/outbox?page=2",
"type": "OrderedCollectionPage",
"url": "http://example.com/outbox?page=2",
"totalItems": 1,
"partOf": "http://example.com/outbox",
"current": "http://example.com/outbox?page=1",
"next": "http://example.com/outbox?page=3",
"prev" : "http://example.com/outbox?page=1",
"startIndex": "100",
"orderedItems": [
{
"id": "http://example.com/outbox/53c6fb47",
"type": "Article",
"name": "Example title",
"content": "Example content!",
"url": "http://example.com/53c6fb47",
"mediaType": "text/markdown",
"published": "2018-07-05T16:46:44.00000UTC",
"generator": "http://example.com",
"attributedTo": "http://example.com/account/alice"
}
]
}

View file

@ -299,6 +299,35 @@ var allTests = tests{
},
},
},
"ordered_collection_page": testPair{
expected: true,
blank: &a.OrderedCollectionPage{},
result: &a.OrderedCollectionPage{
PartOf: a.IRI("http://example.com/outbox"),
Next: a.IRI("http://example.com/outbox?page=3"),
Prev: a.IRI("http://example.com/outbox?page=1"),
Current: a.IRI("http://example.com/outbox?page=2"),
OrderedCollection: a.OrderedCollection{
ID: a.ObjectID("http://example.com/outbox?page=2"),
Type: a.OrderedCollectionPageType,
URL: a.IRI("http://example.com/outbox?page=2"),
TotalItems: 1,
OrderedItems: a.ItemCollection{
&a.Object{
ID: a.ObjectID("http://example.com/outbox/53c6fb47"),
Type: a.ArticleType,
Name: a.NaturalLanguageValue{{a.NilLangRef, "Example title"}},
Content: a.NaturalLanguageValue{{a.NilLangRef, "Example content!"}},
URL: a.IRI("http://example.com/53c6fb47"),
MediaType: a.MimeType("text/markdown"),
Published: time.Date(2018, time.July, 5, 16, 46, 44, 0, zLoc),
Generator: a.IRI("http://example.com"),
AttributedTo: a.IRI("http://example.com/accounts/alice"),
},
},
},
},
},
"natural_language_values": {
expected: true,
blank: &a.NaturalLanguageValue{},