Fixed source property unmarshalling
This commit is contained in:
parent
88e637fb3f
commit
a955ef13ec
|
@ -27,12 +27,10 @@ type Object struct {
|
||||||
func GetAPSource(data []byte) Source {
|
func GetAPSource(data []byte) Source {
|
||||||
s := Source{}
|
s := Source{}
|
||||||
|
|
||||||
contBytes, _, _, err := jsonparser.Get(data, "source", "content")
|
if contBytes, _, _, err := jsonparser.Get(data, "source", "content"); err == nil {
|
||||||
if err == nil {
|
|
||||||
s.Content.UnmarshalJSON(contBytes)
|
s.Content.UnmarshalJSON(contBytes)
|
||||||
}
|
}
|
||||||
mimeBytes, _, _, err := jsonparser.Get(data, "source", "mimeType")
|
if mimeBytes, _, _, err := jsonparser.Get(data, "source", "mediaType"); err == nil {
|
||||||
if err == nil {
|
|
||||||
s.MediaType.UnmarshalJSON(mimeBytes)
|
s.MediaType.UnmarshalJSON(mimeBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package activitypub
|
package activitypub
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func validateEmptyObject(o Object, t *testing.T) {
|
func validateEmptyObject(o Object, t *testing.T) {
|
||||||
if o.ID != "" {
|
if o.ID != "" {
|
||||||
|
@ -60,3 +62,16 @@ func TestSource_UnmarshalJSON(t *testing.T) {
|
||||||
s.UnmarshalJSON(dataEmpty)
|
s.UnmarshalJSON(dataEmpty)
|
||||||
validateEmptySource(s, t)
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -227,6 +227,8 @@ func (l *LangRef) UnmarshalText(data []byte) error {
|
||||||
func (n *NaturalLanguageValue) UnmarshalJSON(data []byte) error {
|
func (n *NaturalLanguageValue) UnmarshalJSON(data []byte) error {
|
||||||
val, typ, _, err := jsonparser.Get(data)
|
val, typ, _, err := jsonparser.Get(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// try our luck if data contains an unquoted string
|
||||||
|
n.Append(NilLangRef, string(data))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
switch typ {
|
switch typ {
|
||||||
|
|
Reference in a new issue