Making []Item and URI/IRI structs conform to ObjectOrLink

This commit is contained in:
Marius Orcsik 2018-07-25 11:54:12 +02:00
parent 6285a8fc39
commit 8038688be4
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
4 changed files with 100 additions and 0 deletions

View file

@ -341,6 +341,10 @@ func (c *Collection) UnmarshalJSON(data []byte) error {
if len(u) > 0 {
c.URL = u
}
it := getAPItems(data, "items")
if it != nil {
c.Items = *it.(*ItemCollection)
}
return nil
}

View file

@ -5,3 +5,29 @@ type ItemCollection []Item
// Item struct
type Item ObjectOrLink
// GetID returns the ObjectID corresponding to ItemCollection
func (i ItemCollection) GetID() *ObjectID {
return nil
}
// GetType returns the ItemCollection's type
func (i ItemCollection) GetType() ActivityVocabularyType {
return ActivityVocabularyType("")
}
// IsLink returns false for an ItemCollection object
func (i ItemCollection) IsLink() bool {
return false
}
// IsObject returns true for a ItemCollection object
func (i ItemCollection) IsObject() bool {
return false
}
// Append adds an element to an Inbox
func (i *ItemCollection) Append(ob ObjectOrLink) error {
*i = append(*i, ob)
return nil
}

View file

@ -73,6 +73,36 @@ func getAPNaturalLanguageField(data []byte, prop string) NaturalLanguageValue {
return n
}
func getAPItems(data []byte, prop string) CollectionInterface {
val, typ, _, err := jsonparser.Get(data, prop)
if err != nil {
return nil
}
aTyp := getAPType(val)
var it CollectionInterface
switch aTyp {
case CollectionType:
it = &Collection{}
case OrderedCollectionType:
it = &OrderedCollection{}
}
switch typ {
case jsonparser.Object:
jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
i := Object{}
err := i.UnmarshalJSON(val)
it.Append(i)
return err
}, prop)
case jsonparser.String:
s, _ := jsonparser.GetString(data)
it.Append(URI(s))
}
return it
}
func getURIField(data []byte, prop string) URI {
val, err := jsonparser.GetString(data, prop)
if err != nil {

View file

@ -55,3 +55,43 @@ func (i IRI) UnmarshalText(s []byte) error {
i = IRI(strings.Trim(string(s), "\""))
return nil
}
// IsObject
func (u URI) GetID() *ObjectID {
return nil
}
// IsObject
func (i IRI) GetID() *ObjectID {
return nil
}
// GetType
func (u URI) GetType() ActivityVocabularyType {
return LinkType
}
// GetType
func (i IRI) GetType() ActivityVocabularyType {
return LinkType
}
// IsLink
func (u URI) IsLink() bool {
return true
}
// IsLink
func (i IRI) IsLink() bool {
return true
}
// IsObject
func (u URI) IsObject() bool {
return false
}
// IsObject
func (i IRI) IsObject() bool {
return false
}