From 7d6b9976bfebe39ceff389b401cf751085782813 Mon Sep 17 00:00:00 2001 From: mariusor Date: Fri, 6 Nov 2020 19:40:46 +0100 Subject: [PATCH] Rename the item typer, as it's not strictly tied into json --- decoding.go | 11 ++++++----- decoding_test.go | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/decoding.go b/decoding.go index 19fb4e7..d64669d 100644 --- a/decoding.go +++ b/decoding.go @@ -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 +// The default for this package is GetItemByType but can be overwritten var ItemTyperFunc TyperFn // TyperFn is the type of the function which returns an activitystreams.Item struct instance @@ -144,6 +144,7 @@ func itemFn(data []byte) (Item, error) { return nil, nil } + // todo(marius): this allows passing an empty type to the typer function i, err := ItemTyperFunc(JSONGetType(data)) if err != nil || i == nil { if i, ok := asIRI(data); ok { @@ -330,12 +331,12 @@ func JSONGetIRI(data []byte, prop string) IRI { // ActivityStreams object, if possible func UnmarshalJSON(data []byte) (Item, error) { if ItemTyperFunc == nil { - ItemTyperFunc = JSONGetItemByType + ItemTyperFunc = GetItemByType } return JSONUnmarshalToItem(data), nil } -func JSONGetItemByType(typ ActivityVocabularyType) (Item, error) { +func GetItemByType(typ ActivityVocabularyType) (Item, error) { switch typ { case ObjectType: return ObjectNew(typ), nil @@ -468,7 +469,7 @@ func JSONGetActorEndpoints(data []byte, prop string) *Endpoints { func loadObject(data []byte, o *Object) error { if ItemTyperFunc == nil { - ItemTyperFunc = JSONGetItemByType + ItemTyperFunc = GetItemByType } o.ID = JSONGetID(data) o.Type = JSONGetType(data) @@ -663,7 +664,7 @@ func loadTombstone(data []byte, t *Tombstone) error { func loadLink(data []byte, l *Link) error { if ItemTyperFunc == nil { - ItemTyperFunc = JSONGetItemByType + ItemTyperFunc = GetItemByType } l.ID = JSONGetID(data) l.Type = JSONGetType(data) diff --git a/decoding_test.go b/decoding_test.go index 4b141fc..37f2e3c 100644 --- a/decoding_test.go +++ b/decoding_test.go @@ -281,7 +281,7 @@ var tests = testPairs{ func TestJSONGetItemByType(t *testing.T) { for typ, test := range tests { t.Run(string(typ), func(t *testing.T) { - v, err := JSONGetItemByType(typ) + v, err := GetItemByType(typ) if err != nil { t.Error(err) }