Fixed source property unmarshalling

This commit is contained in:
Marius Orcsik 2018-12-16 13:19:19 +01:00
parent 88e637fb3f
commit a955ef13ec
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
3 changed files with 22 additions and 7 deletions

View file

@ -27,12 +27,10 @@ type Object struct {
func GetAPSource(data []byte) Source {
s := Source{}
contBytes, _, _, err := jsonparser.Get(data, "source", "content")
if err == nil {
if contBytes, _, _, err := jsonparser.Get(data, "source", "content"); err == nil {
s.Content.UnmarshalJSON(contBytes)
}
mimeBytes, _, _, err := jsonparser.Get(data, "source", "mimeType")
if err == nil {
if mimeBytes, _, _, err := jsonparser.Get(data, "source", "mediaType"); err == nil {
s.MediaType.UnmarshalJSON(mimeBytes)
}

View file

@ -1,6 +1,8 @@
package activitypub
import "testing"
import (
"testing"
)
func validateEmptyObject(o Object, t *testing.T) {
if o.ID != "" {
@ -60,3 +62,16 @@ func TestSource_UnmarshalJSON(t *testing.T) {
s.UnmarshalJSON(dataEmpty)
validateEmptySource(s, t)
}
func TestGetAPSource(t *testing.T) {
data := []byte(`{"source": {"content": "test", "mediaType": "text/plain" }}`)
a := GetAPSource(data)
if a.Content.First() != "test" {
t.Errorf("Content didn't match test value. Received %q, expecting %q", a.Content, "test")
}
if a.MediaType != "text/plain" {
t.Errorf("Content didn't match test value. Received %q, expecting %q", a.MediaType, "text/plain")
}
}

View file

@ -227,6 +227,8 @@ func (l *LangRef) UnmarshalText(data []byte) error {
func (n *NaturalLanguageValue) UnmarshalJSON(data []byte) error {
val, typ, _, err := jsonparser.Get(data)
if err != nil {
// try our luck if data contains an unquoted string
n.Append(NilLangRef, string(data))
return nil
}
switch typ {
@ -418,9 +420,9 @@ type Relationship struct {
type Tombstone struct {
Parent
// FormerType On a Tombstone object, the formerType property identifies the type of the object that was deleted.
FormerType ActivityVocabularyType `jsonld:"formerType,omitempty"`
FormerType ActivityVocabularyType `jsonld:"formerType,omitempty"`
// Deleted On a Tombstone object, the deleted property is a timestamp for when the object was deleted.
Deleted time.Time `jsonld:"deleted,omitempty"`
Deleted time.Time `jsonld:"deleted,omitempty"`
}
// ValidGenericType validates the type against the valid generic object types