Adding a MarshalJSON for NaturalLanguageValue alias
This commit is contained in:
parent
b078413f1b
commit
a47db907ad
2 changed files with 38 additions and 9 deletions
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"time"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type ObjectId string
|
||||
|
@ -61,6 +62,16 @@ type MimeType string
|
|||
type LangRef string
|
||||
type NaturalLanguageValue map[LangRef]string
|
||||
|
||||
func (this NaturalLanguageValue) MarshalJSON() ([]byte, error) {
|
||||
if len(this) == 1 {
|
||||
for _, v := range this {
|
||||
return json.Marshal(v)
|
||||
}
|
||||
}
|
||||
|
||||
return json.Marshal(map[LangRef]string(this))
|
||||
}
|
||||
|
||||
// Describes an object of any kind.
|
||||
// The Object type serves as the base type for most of the other kinds of objects defined in the Activity Vocabulary,
|
||||
// including other Core types such as Activity, IntransitiveActivity, Collection and OrderedCollection.
|
||||
|
|
|
@ -73,12 +73,30 @@ func TestValidLinkType(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestBaseObject_Serialize(t *testing.T) {
|
||||
//var testValue = ObjectId("test")
|
||||
//var testType = ArticleType
|
||||
//
|
||||
//o := ObjectNew(testValue, testType)
|
||||
//if o.Serialize() != "" {
|
||||
// t.Errorf("Invalid serialize result")
|
||||
//}
|
||||
}
|
||||
func TestMarshalJSON(t *testing.T) {
|
||||
m := make(map[LangRef]string)
|
||||
m["en"] = "test"
|
||||
m["de"] = "test"
|
||||
|
||||
n := NaturalLanguageValue(m)
|
||||
result, err := n.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Errorf("Failed marshaling '%v'", err)
|
||||
}
|
||||
m_res := "{\"de\":\"test\",\"en\":\"test\"}"
|
||||
if string(result) != m_res {
|
||||
t.Errorf("Different results '%v' vs. '%v'", string(result), m_res)
|
||||
}
|
||||
|
||||
s := make(map[LangRef]string)
|
||||
s["en"] = "test"
|
||||
n1 := NaturalLanguageValue(s)
|
||||
result1, err1 := n1.MarshalJSON()
|
||||
if err1 != nil {
|
||||
t.Errorf("Failed marshaling '%v'", err1)
|
||||
}
|
||||
m_res1 := "\"test\""
|
||||
if string(result1) != m_res1 {
|
||||
t.Errorf("Different results '%v' vs. '%v'", string(result1), m_res1)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue