Set the ItemTyperFunc when doing UnmarshalJSON

This commit is contained in:
Marius Orcsik 2019-05-18 22:45:54 +02:00
parent 14d9cc820b
commit 385e1cd273
No known key found for this signature in database
GPG key ID: 8218F7122969D484
5 changed files with 16 additions and 1 deletions

View file

@ -1532,6 +1532,9 @@ func (u *Update) UnmarshalJSON(data []byte) error {
// UnmarshalJSON
func (c *Create) UnmarshalJSON(data []byte) error {
if ItemTyperFunc == nil {
ItemTyperFunc = JSONGetItemByType
}
a := Activity(*c)
err := a.UnmarshalJSON(data)

View file

@ -203,6 +203,9 @@ func (o OrderedCollection) IsObject() bool {
// UnmarshalJSON
func (o *OrderedCollection) UnmarshalJSON(data []byte) error {
if ItemTyperFunc == nil {
ItemTyperFunc = JSONGetItemByType
}
o.Parent.UnmarshalJSON(data)
o.TotalItems = uint(JSONGetInt(data, "totalItems"))

View file

@ -116,6 +116,9 @@ func (m Mention) GetType() ActivityVocabularyType {
// UnmarshalJSON
func (l *Link) UnmarshalJSON(data []byte) error {
if ItemTyperFunc == nil {
ItemTyperFunc = JSONGetItemByType
}
l.ID = JSONGetObjectID(data)
l.Type = JSONGetType(data)
l.MediaType = JSONGetMimeType(data)

View file

@ -531,6 +531,9 @@ func (c *MimeType) UnmarshalJSON(data []byte) error {
// UnmarshalJSON
func (o *Object) UnmarshalJSON(data []byte) error {
if ItemTyperFunc == nil {
ItemTyperFunc = JSONGetItemByType
}
o.ID = JSONGetObjectID(data)
o.Type = JSONGetType(data)
o.Name = JSONGetNaturalLanguageField(data, "name")

View file

@ -19,7 +19,7 @@ var (
// ItemTyperFunc will return an instance of a struct that implements activitystreams.Item
// The default for this package is JSONGetItemByType but can be overwritten
var ItemTyperFunc TyperFunction = JSONGetItemByType
var ItemTyperFunc TyperFunction
// TyperFunction is the type of the function which returns an activitystreams.Item struct instance
// for a specific ActivityVocabularyType
@ -234,6 +234,9 @@ func JSONGetIRI(data []byte, prop string) IRI {
// UnmarshalJSON tries to detect the type of the object in the json data and then outputs a matching
// ActivityStreams object, if possible
func UnmarshalJSON(data []byte) (Item, error) {
if ItemTyperFunc == nil {
ItemTyperFunc = JSONGetItemByType
}
return JSONUnmarshalToItem(data), nil
}