Refactored marshaling and removing bytes.Buffer in favor of a simple byte slice
This commit is contained in:
parent
22f934b796
commit
6bb34603e9
17
activity.go
17
activity.go
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
@ -797,24 +796,24 @@ func FlattenActivityProperties(act *Activity) *Activity {
|
|||
|
||||
// MarshalJSON
|
||||
func (a Activity) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b.Write([]byte{'{'})
|
||||
b := make([]byte, 0)
|
||||
write(&b, '{')
|
||||
|
||||
if !writeActivity(&b, a) {
|
||||
return nil, nil
|
||||
}
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// MarshalJSON
|
||||
func (i IntransitiveActivity) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b.Write([]byte{'{'})
|
||||
b := make([]byte, 0)
|
||||
write(&b, '{')
|
||||
|
||||
if !writeIntransitiveActivity(&b, i) {
|
||||
return nil, nil
|
||||
}
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
|
|
95
actor.go
95
actor.go
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/buger/jsonparser"
|
||||
"time"
|
||||
|
@ -212,30 +211,22 @@ func (p *PublicKey) UnmarshalJSON(data []byte) error {
|
|||
}
|
||||
|
||||
func (p PublicKey) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := true
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
if v, err := p.ID.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = !writeProp(&b, "id", v)
|
||||
notEmpty = !writeProp(&b, "id", v)
|
||||
}
|
||||
if p.Owner != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeIRIProp(&b, "owner", p.Owner) || notEmpty
|
||||
notEmpty = writeIRIProp(&b, "owner", p.Owner) || notEmpty
|
||||
}
|
||||
if len(p.PublicKeyPem) > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeIRIProp(&b, "publicKeyPem", p.Owner) || notEmpty
|
||||
notEmpty = writeIRIProp(&b, "publicKeyPem", p.Owner) || notEmpty
|
||||
}
|
||||
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -396,60 +387,46 @@ func (a *Actor) UnmarshalJSON(data []byte) error {
|
|||
}
|
||||
|
||||
func (a Actor) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
|
||||
OnObject(a, func(o *Object) error {
|
||||
notEmpty = writeObject(&b, *o)
|
||||
notEmpty = writeObject(&b, *o)
|
||||
return nil
|
||||
})
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
if a.Inbox != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "inbox", a.Inbox) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "inbox", a.Inbox) || notEmpty
|
||||
}
|
||||
if a.Outbox != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "outbox", a.Outbox) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "outbox", a.Outbox) || notEmpty
|
||||
}
|
||||
if a.Following != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "following", a.Following) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "following", a.Following) || notEmpty
|
||||
}
|
||||
if a.Followers != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "followers", a.Followers) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "followers", a.Followers) || notEmpty
|
||||
}
|
||||
if a.Liked != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "liked", a.Liked) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "liked", a.Liked) || notEmpty
|
||||
}
|
||||
if a.Endpoints != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
if v, err := a.Endpoints.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(&b, "endpoints", v) || notEmpty
|
||||
notEmpty = writeProp(&b, "endpoints", v) || notEmpty
|
||||
}
|
||||
}
|
||||
if len(a.Streams) > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
writePropName(&b, "streams")
|
||||
writePropName(&b, "streams")
|
||||
lNotEmpty := true
|
||||
for _, ss := range a.Streams {
|
||||
writeCommaIfNotEmpty(lNotEmpty)
|
||||
lNotEmpty = writeItemCollection(&b, ss) || lNotEmpty
|
||||
lNotEmpty = writeItemCollection(&b, ss) || lNotEmpty
|
||||
}
|
||||
notEmpty = lNotEmpty || notEmpty
|
||||
}
|
||||
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -494,43 +471,31 @@ func (e *Endpoints) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// MarshalJSON
|
||||
func (e Endpoints) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
if e.OauthAuthorizationEndpoint != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "oauthAuthorizationEndpoint", e.OauthAuthorizationEndpoint) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "oauthAuthorizationEndpoint", e.OauthAuthorizationEndpoint) || notEmpty
|
||||
}
|
||||
if e.OauthTokenEndpoint != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "oauthTokenEndpoint", e.OauthTokenEndpoint) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "oauthTokenEndpoint", e.OauthTokenEndpoint) || notEmpty
|
||||
}
|
||||
if e.ProvideClientKey != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "provideClientKey", e.ProvideClientKey) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "provideClientKey", e.ProvideClientKey) || notEmpty
|
||||
}
|
||||
if e.SignClientKey != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "signClientKey", e.SignClientKey) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "signClientKey", e.SignClientKey) || notEmpty
|
||||
}
|
||||
if e.SharedInbox != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "sharedInbox", e.SharedInbox) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "sharedInbox", e.SharedInbox) || notEmpty
|
||||
}
|
||||
if e.UploadMedia != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "uploadMedia", e.UploadMedia) || notEmpty
|
||||
notEmpty = writeItemProp(&b, "uploadMedia", e.UploadMedia) || notEmpty
|
||||
}
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
@ -298,42 +297,31 @@ func (c *Collection) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// MarshalJSON
|
||||
func (c Collection) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
|
||||
OnObject(c, func(o *Object) error {
|
||||
notEmpty = writeObject(&b, *o)
|
||||
return nil
|
||||
})
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
if c.Current != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "current", c.Current) || notEmpty
|
||||
}
|
||||
if c.First != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "first", c.First) || notEmpty
|
||||
}
|
||||
if c.Last != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "last", c.Last) || notEmpty
|
||||
}
|
||||
if c.Items != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemCollectionProp(&b, "items", c.Items) || notEmpty
|
||||
}
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeIntProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty
|
||||
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
@ -257,55 +256,41 @@ func (c *CollectionPage) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// MarshalJSON
|
||||
func (c CollectionPage) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
|
||||
OnObject(c, func(o *Object) error {
|
||||
notEmpty = writeObject(&b, *o)
|
||||
return nil
|
||||
})
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
if c.Current != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "current", c.Current) || notEmpty
|
||||
}
|
||||
if c.First != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "first", c.First) || notEmpty
|
||||
}
|
||||
if c.Last != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "last", c.Last) || notEmpty
|
||||
}
|
||||
if c.Items != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemCollectionProp(&b, "items", c.Items) || notEmpty
|
||||
}
|
||||
if c.PartOf != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "partOf", c.PartOf) || notEmpty
|
||||
}
|
||||
if c.Next != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "next", c.Next) || notEmpty
|
||||
}
|
||||
if c.Prev != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "prev", c.Prev) || notEmpty
|
||||
}
|
||||
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeIntProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty
|
||||
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
210
encoding.go
210
encoding.go
|
@ -1,38 +1,55 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func writeProp(b *bytes.Buffer, name string, val []byte) (notEmpty bool) {
|
||||
func writeComma(b *[]byte) {
|
||||
if len(*b) > 1 && (*b)[len(*b)-1] != ',' {
|
||||
*b = append(*b, ',')
|
||||
}
|
||||
}
|
||||
func writeProp(b *[]byte, name string, val []byte) (notEmpty bool) {
|
||||
if len(val) == 0 {
|
||||
return false
|
||||
}
|
||||
writePropName(b, name)
|
||||
return writeValue(b, val)
|
||||
writeComma(b)
|
||||
success := writePropName(b, name) && writeValue(b, val)
|
||||
if !success {
|
||||
*b = (*b)[:len(*b)-1]
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
func writePropName(b *bytes.Buffer, s string) (notEmpty bool) {
|
||||
func write(b *[]byte, c ...byte) {
|
||||
*b = append(*b, c...)
|
||||
}
|
||||
|
||||
func writeS(b *[]byte, s string) {
|
||||
*b = append(*b, s...)
|
||||
}
|
||||
|
||||
func writePropName(b *[]byte, s string) (notEmpty bool) {
|
||||
if len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
b.Write([]byte{'"'})
|
||||
b.WriteString(s)
|
||||
b.Write([]byte{'"', ':'})
|
||||
write(b, '"')
|
||||
writeS(b, s)
|
||||
write(b, '"', ':')
|
||||
return true
|
||||
}
|
||||
|
||||
func writeValue(b *bytes.Buffer, s []byte) (notEmpty bool) {
|
||||
func writeValue(b *[]byte, s []byte) (notEmpty bool) {
|
||||
if len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
b.Write(s)
|
||||
write(b, s...)
|
||||
return true
|
||||
}
|
||||
|
||||
func writeNaturalLanguageProp(b *bytes.Buffer, n string, nl NaturalLanguageValues) (notEmpty bool) {
|
||||
func writeNaturalLanguageProp(b *[]byte, n string, nl NaturalLanguageValues) (notEmpty bool) {
|
||||
l := nl.Count()
|
||||
if l > 1 {
|
||||
n += "Map"
|
||||
|
@ -42,257 +59,200 @@ func writeNaturalLanguageProp(b *bytes.Buffer, n string, nl NaturalLanguageValue
|
|||
}
|
||||
return false
|
||||
}
|
||||
func writeStringProp(b *bytes.Buffer, n string, s string) (notEmpty bool) {
|
||||
return writeProp(b, n, []byte(s))
|
||||
func writeStringProp(b *[]byte, n string, s string) (notEmpty bool) {
|
||||
return writeProp(b, n, []byte(fmt.Sprintf(`"%s"`, s)))
|
||||
}
|
||||
func writeBoolProp(b *bytes.Buffer, n string, t bool) (notEmpty bool) {
|
||||
return writeProp(b, n, []byte(fmt.Sprintf("%t", t)))
|
||||
func writeBoolProp(b *[]byte, n string, t bool) (notEmpty bool) {
|
||||
return writeProp(b, n, []byte(fmt.Sprintf(`"%t"`, t)))
|
||||
}
|
||||
func writeIntProp(b *bytes.Buffer, n string, d int64) (notEmpty bool) {
|
||||
func writeIntProp(b *[]byte, n string, d int64) (notEmpty bool) {
|
||||
return writeProp(b, n, []byte(fmt.Sprintf("%d", d)))
|
||||
}
|
||||
func writeFloatProp(b *bytes.Buffer, n string, f float64) (notEmpty bool) {
|
||||
func writeFloatProp(b *[]byte, n string, f float64) (notEmpty bool) {
|
||||
return writeProp(b, n, []byte(fmt.Sprintf("%f", f)))
|
||||
}
|
||||
func writeTimeProp(b *bytes.Buffer, n string, t time.Time) (notEmpty bool) {
|
||||
func writeTimeProp(b *[]byte, n string, t time.Time) (notEmpty bool) {
|
||||
if v, err := t.MarshalJSON(); err == nil {
|
||||
return writeProp(b, n, v)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func writeDurationProp(b *bytes.Buffer, n string, d time.Duration) (notEmpty bool) {
|
||||
func writeDurationProp(b *[]byte, n string, d time.Duration) (notEmpty bool) {
|
||||
if v, err := marshalXSD(d); err == nil {
|
||||
return writeProp(b, n, v)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func writeIRIProp(b *bytes.Buffer, n string, i LinkOrIRI) (notEmpty bool) {
|
||||
url := i.GetLink()
|
||||
func writeIRIProp(b *[]byte, n string, i LinkOrIRI) (notEmpty bool) {
|
||||
url := i.GetLink().String()
|
||||
if len(url) == 0 {
|
||||
return false
|
||||
}
|
||||
writePropName(b, n)
|
||||
b.Write([]byte{'"'})
|
||||
b.Write([]byte(url))
|
||||
b.Write([]byte{'"'})
|
||||
writeStringProp(b, n, url)
|
||||
return true
|
||||
}
|
||||
|
||||
func writeItemProp(b *bytes.Buffer, n string, i Item) (notEmpty bool) {
|
||||
func writeItemProp(b *[]byte, n string, i Item) (notEmpty bool) {
|
||||
if i == nil {
|
||||
return notEmpty
|
||||
}
|
||||
if i.IsObject() {
|
||||
OnObject(i, func(o *Object) error {
|
||||
v, err := o.MarshalJSON()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
notEmpty = writeProp(b, n, v)
|
||||
return nil
|
||||
})
|
||||
} else if i.IsCollection() {
|
||||
OnCollection(i, func(c CollectionInterface) error {
|
||||
notEmpty = writeItemCollectionProp(b, n, c.Collection()) || notEmpty
|
||||
return nil
|
||||
})
|
||||
if im, ok := i.(json.Marshaler); ok {
|
||||
v, err := im.MarshalJSON()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return writeProp(b, n, v)
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeString(b *bytes.Buffer, s string) (notEmpty bool) {
|
||||
func writeString(b *[]byte, s string) (notEmpty bool) {
|
||||
if len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
b.Write([]byte{'"'})
|
||||
b.WriteString(s)
|
||||
b.Write([]byte{'"'})
|
||||
write(b, '"')
|
||||
writeS(b, s)
|
||||
write(b, '"')
|
||||
return true
|
||||
}
|
||||
|
||||
func writeItemCollection(b *bytes.Buffer, col ItemCollection) (notEmpty bool) {
|
||||
func writeItemCollection(b *[]byte, col ItemCollection) (notEmpty bool) {
|
||||
if len(col) == 0 {
|
||||
return notEmpty
|
||||
}
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
write(b, ',')
|
||||
}
|
||||
}
|
||||
b.Write([]byte{'['})
|
||||
for _, i := range col {
|
||||
if i.IsObject() {
|
||||
OnObject(i, func(o *Object) error {
|
||||
v, err := o.MarshalJSON()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeValue(b, v) || notEmpty
|
||||
return nil
|
||||
})
|
||||
} else if i.IsLink() {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeValue(b, []byte(i.GetLink())) || notEmpty
|
||||
write(b, '[')
|
||||
for i, it := range col {
|
||||
if im, ok := it.(json.Marshaler); ok {
|
||||
v, err := im.MarshalJSON()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
writeCommaIfNotEmpty(i > 0)
|
||||
write(b, v...)
|
||||
}
|
||||
}
|
||||
b.Write([]byte{']'})
|
||||
return notEmpty
|
||||
write(b, ']')
|
||||
return true
|
||||
}
|
||||
func writeItemCollectionProp(b *bytes.Buffer, n string, col ItemCollection) (notEmpty bool) {
|
||||
func writeItemCollectionProp(b *[]byte, n string, col ItemCollection) (notEmpty bool) {
|
||||
if len(col) == 0 {
|
||||
return notEmpty
|
||||
}
|
||||
writePropName(b, n)
|
||||
return writeItemCollection(b, col)
|
||||
writeComma(b)
|
||||
success := writePropName(b, n) && writeItemCollection(b, col)
|
||||
if !success {
|
||||
*b = (*b)[:len(*b)-1]
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
func writeObject(b *bytes.Buffer, o Object) (notEmpty bool) {
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
func writeObject(b *[]byte, o Object) (notEmpty bool) {
|
||||
if v, err := o.ID.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(b, "id", v) || notEmpty
|
||||
}
|
||||
if v, err := o.Type.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeProp(b, "type", v) || notEmpty
|
||||
}
|
||||
if v, err := o.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeProp(b, "mediaType", v) || notEmpty
|
||||
}
|
||||
if len(o.Name) > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeNaturalLanguageProp(b, "name", o.Name) || notEmpty
|
||||
}
|
||||
if len(o.Summary) > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeNaturalLanguageProp(b, "summary", o.Summary) || notEmpty
|
||||
}
|
||||
if len(o.Content) > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeNaturalLanguageProp(b, "content", o.Content) || notEmpty
|
||||
}
|
||||
if o.Attachment != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "attachment", o.Attachment) || notEmpty
|
||||
}
|
||||
if o.AttributedTo != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "attributedTo", o.AttributedTo) || notEmpty
|
||||
}
|
||||
if o.Audience != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "audience", o.Audience) || notEmpty
|
||||
}
|
||||
if o.Context != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "context", o.Context) || notEmpty
|
||||
}
|
||||
if o.Generator != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "generator", o.Generator) || notEmpty
|
||||
}
|
||||
if o.Icon != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "icon", o.Icon) || notEmpty
|
||||
}
|
||||
if o.Image != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "image", o.Image) || notEmpty
|
||||
}
|
||||
if o.InReplyTo != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "inReplyTo", o.InReplyTo) || notEmpty
|
||||
}
|
||||
if o.Location != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "location", o.Location) || notEmpty
|
||||
}
|
||||
if o.Preview != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "preview", o.Preview) || notEmpty
|
||||
}
|
||||
if o.Replies != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "replies", o.Replies) || notEmpty
|
||||
}
|
||||
if o.Tag != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "tag", o.Tag) || notEmpty
|
||||
}
|
||||
if o.URL != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeIRIProp(b, "url", o.URL) || notEmpty
|
||||
}
|
||||
if o.To != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "to", o.To) || notEmpty
|
||||
}
|
||||
if o.Bto != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "bto", o.Bto) || notEmpty
|
||||
}
|
||||
if o.CC != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "cc", o.CC) || notEmpty
|
||||
}
|
||||
if o.BCC != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "bcc", o.BCC) || notEmpty
|
||||
}
|
||||
if !o.Published.IsZero() {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeTimeProp(b, "published", o.Published) || notEmpty
|
||||
}
|
||||
if !o.Updated.IsZero() {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeTimeProp(b, "updated", o.Updated) || notEmpty
|
||||
}
|
||||
if !o.StartTime.IsZero() {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeTimeProp(b, "startTime", o.StartTime) || notEmpty
|
||||
}
|
||||
if !o.EndTime.IsZero() {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeTimeProp(b, "endTime", o.EndTime) || notEmpty
|
||||
}
|
||||
if o.Duration != 0 {
|
||||
// TODO(marius): maybe don't use 0 as a nil value for Object types
|
||||
// which can have a valid duration of 0 - (Video, Audio, etc)
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeDurationProp(b, "duration", o.Duration) || notEmpty
|
||||
}
|
||||
if o.Likes != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "likes", o.Likes) || notEmpty
|
||||
}
|
||||
if o.Shares != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "shares", o.Shares) || notEmpty
|
||||
}
|
||||
if v, err := o.Source.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeProp(b, "source", v) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeActivity(b *bytes.Buffer, a Activity) (notEmpty bool) {
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
|
||||
func writeActivity(b *[]byte, a Activity) (notEmpty bool) {
|
||||
OnIntransitiveActivity(a, func(i *IntransitiveActivity) error {
|
||||
if i == nil {
|
||||
return nil
|
||||
|
@ -301,19 +261,12 @@ func writeActivity(b *bytes.Buffer, a Activity) (notEmpty bool) {
|
|||
return nil
|
||||
})
|
||||
if a.Object != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "object", a.Object) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeIntransitiveActivity(b *bytes.Buffer, i IntransitiveActivity) (notEmpty bool) {
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
func writeIntransitiveActivity(b *[]byte, i IntransitiveActivity) (notEmpty bool) {
|
||||
OnObject(i, func(o *Object) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
|
@ -322,36 +275,24 @@ func writeIntransitiveActivity(b *bytes.Buffer, i IntransitiveActivity) (notEmpt
|
|||
return nil
|
||||
})
|
||||
if i.Actor != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "actor", i.Actor) || notEmpty
|
||||
}
|
||||
if i.Target != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "target", i.Target) || notEmpty
|
||||
}
|
||||
if i.Result != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "result", i.Result) || notEmpty
|
||||
}
|
||||
if i.Origin != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "origin", i.Origin) || notEmpty
|
||||
}
|
||||
if i.Instrument != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "instrument", i.Instrument) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeQuestion(b *bytes.Buffer, q Question) (notEmpty bool) {
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
|
||||
func writeQuestion(b *[]byte, q Question) (notEmpty bool) {
|
||||
OnIntransitiveActivity(q, func(i *IntransitiveActivity) error {
|
||||
if i == nil {
|
||||
return nil
|
||||
|
@ -360,13 +301,10 @@ func writeQuestion(b *bytes.Buffer, q Question) (notEmpty bool) {
|
|||
return nil
|
||||
})
|
||||
if q.OneOf != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "oneOf", q.OneOf) || notEmpty
|
||||
} else if q.AnyOf != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(b, "oneOf", q.OneOf) || notEmpty
|
||||
}
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeBoolProp(b, "closed", q.Closed) || notEmpty
|
||||
return notEmpty
|
||||
}
|
||||
|
|
32
iri.go
32
iri.go
|
@ -47,6 +47,15 @@ func (i *IRI) UnmarshalJSON(s []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON
|
||||
func (i IRI) MarshalJSON() ([]byte, error) {
|
||||
b := make([]byte, 0)
|
||||
write(&b, '"')
|
||||
writeS(&b, i.String())
|
||||
write(&b, '"')
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// GetID
|
||||
func (i IRI) GetID() ID {
|
||||
return ID(i)
|
||||
|
@ -80,6 +89,29 @@ func FlattenToIRI(it Item) Item {
|
|||
return it
|
||||
}
|
||||
|
||||
func (i IRIs) MarshalJSON() ([]byte, error) {
|
||||
b := make([]byte, 0)
|
||||
if len(i) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
notEmpty := false
|
||||
writeComma := func() { writeS(&b, ",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
write(&b, '[')
|
||||
for _, iri := range i {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
write(&b, '"')
|
||||
writeS(&b, iri.String())
|
||||
write(&b, '"')
|
||||
}
|
||||
write(&b, ']')
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// Contains verifies if IRIs array contains the received one
|
||||
func (i IRIs) Contains(r IRI) bool {
|
||||
if len(i) == 0 {
|
||||
|
|
|
@ -33,6 +33,12 @@ func (i ItemCollection) IsObject() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (i ItemCollection) MarshalJSON() ([]byte, error) {
|
||||
b := make([]byte, 0)
|
||||
writeItemCollection(&b, i)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// Append facilitates adding elements to Item arrays
|
||||
// and ensures ItemCollection implements the Collection interface
|
||||
func (i *ItemCollection) Append(o Item) error {
|
||||
|
|
28
object.go
28
object.go
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/buger/jsonparser"
|
||||
"strings"
|
||||
|
@ -111,9 +110,9 @@ func (a ActivityVocabularyType) MarshalJSON() ([]byte, error) {
|
|||
if len(a) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
b := bytes.Buffer{}
|
||||
b := make([]byte, 0)
|
||||
writeString(&b, string(a))
|
||||
return b.Bytes(), nil
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// Object describes an ActivityPub object of any kind.
|
||||
|
@ -320,15 +319,15 @@ func (o *Object) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// MarshalJSON
|
||||
func (o Object) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
|
||||
notEmpty = writeObject(&b, o)
|
||||
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -376,9 +375,9 @@ func (m MimeType) MarshalJSON() ([]byte, error) {
|
|||
if len(m) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
b := bytes.Buffer{}
|
||||
b := make([]byte, 0)
|
||||
writeString(&b, string(m))
|
||||
return b.Bytes(), nil
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// ToObject returns an Object pointer to the data in the current Item
|
||||
|
@ -502,23 +501,20 @@ func (s *Source) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// MarshalJSON
|
||||
func (s Source) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b := make([]byte, 0)
|
||||
empty := true
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
if len(s.MediaType) > 0 {
|
||||
if v, err := s.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
empty = !writeProp(&b, "mediaType", v)
|
||||
}
|
||||
}
|
||||
if len(s.Content) > 0 {
|
||||
if !empty {
|
||||
b.Write([]byte{','})
|
||||
}
|
||||
empty = !writeNaturalLanguageProp(&b, "content", s.Content)
|
||||
}
|
||||
if !empty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
@ -294,42 +293,31 @@ func (o *OrderedCollection) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// MarshalJSON
|
||||
func (o OrderedCollection) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
|
||||
OnObject(o, func(o *Object) error {
|
||||
notEmpty = writeObject(&b, *o)
|
||||
return nil
|
||||
})
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
if o.Current != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "current", o.Current) || notEmpty
|
||||
}
|
||||
if o.First != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "first", o.First) || notEmpty
|
||||
}
|
||||
if o.Last != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "last", o.Last) || notEmpty
|
||||
}
|
||||
if o.OrderedItems != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemCollectionProp(&b, "orderedItems", o.OrderedItems) || notEmpty
|
||||
}
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeIntProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty
|
||||
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/buger/jsonparser"
|
||||
"time"
|
||||
|
@ -263,55 +262,40 @@ func (o *OrderedCollectionPage) UnmarshalJSON(data []byte) error {
|
|||
}
|
||||
// MarshalJSON
|
||||
func (c OrderedCollectionPage) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
|
||||
OnObject(c, func(o *Object) error {
|
||||
notEmpty = writeObject(&b, *o)
|
||||
return nil
|
||||
})
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
if c.Current != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "current", c.Current) || notEmpty
|
||||
}
|
||||
if c.First != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "first", c.First) || notEmpty
|
||||
}
|
||||
if c.Last != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "last", c.Last) || notEmpty
|
||||
}
|
||||
if c.OrderedItems != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemCollectionProp(&b, "orderedItems", c.OrderedItems) || notEmpty
|
||||
}
|
||||
if c.PartOf != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "partOf", c.PartOf) || notEmpty
|
||||
}
|
||||
if c.Next != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "next", c.Next) || notEmpty
|
||||
}
|
||||
if c.Prev != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "prev", c.Prev) || notEmpty
|
||||
}
|
||||
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeIntProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty
|
||||
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
21
place.go
21
place.go
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
@ -221,47 +220,35 @@ func (p *Place) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// MarshalJSON
|
||||
func (p Place) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
|
||||
OnObject(p, func(o *Object) error {
|
||||
notEmpty = writeObject(&b, *o)
|
||||
return nil
|
||||
})
|
||||
if p.Accuracy > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeFloatProp(&b, "accuracy", p.Accuracy) || notEmpty
|
||||
}
|
||||
if p.Altitude > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeFloatProp(&b, "altitude", p.Altitude) || notEmpty
|
||||
}
|
||||
if p.Latitude > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeFloatProp(&b, "latitude", p.Latitude) || notEmpty
|
||||
}
|
||||
if p.Longitude > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeFloatProp(&b, "longitude", p.Longitude) || notEmpty
|
||||
}
|
||||
if p.Radius > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeIntProp(&b, "radius", p.Radius) || notEmpty
|
||||
}
|
||||
if len(p.Units) > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeStringProp(&b, "radius", p.Units) || notEmpty
|
||||
}
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
17
profile.go
17
profile.go
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
@ -202,29 +201,21 @@ func (p *Profile) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// MarshalJSON
|
||||
func (p Profile) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
|
||||
OnObject(p, func(o *Object) error {
|
||||
notEmpty = writeObject(&b, *o)
|
||||
return nil
|
||||
})
|
||||
|
||||
if p.Describes != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "describes", p.Describes) || notEmpty
|
||||
}
|
||||
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
@ -231,14 +230,14 @@ func (q *Question) UnmarshalJSON(data []byte) error {
|
|||
}
|
||||
|
||||
func (q Question) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
b.Write([]byte{'{'})
|
||||
b := make([]byte, 0)
|
||||
write(&b, '{')
|
||||
|
||||
if !writeQuestion(&b, q) {
|
||||
return nil, nil
|
||||
}
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// QuestionNew initializes a Question activity
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
@ -214,15 +213,9 @@ func (r *Relationship) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// MarshalJSON
|
||||
func (r Relationship) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
|
||||
OnObject(r, func(o *Object) error {
|
||||
notEmpty = writeObject(&b, *o)
|
||||
|
@ -230,21 +223,18 @@ func (r Relationship) MarshalJSON() ([]byte, error) {
|
|||
})
|
||||
|
||||
if r.Subject != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "subject", r.Subject) || notEmpty
|
||||
}
|
||||
if r.Object != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "object", r.Object) || notEmpty
|
||||
}
|
||||
if r.Relationship != nil {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeItemProp(&b, "relationship", r.Relationship) || notEmpty
|
||||
}
|
||||
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
17
tombstone.go
17
tombstone.go
|
@ -1,7 +1,6 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
@ -205,33 +204,25 @@ func (t *Tombstone) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// MarshalJSON
|
||||
func (t Tombstone) MarshalJSON() ([]byte, error) {
|
||||
b := bytes.Buffer{}
|
||||
writeComma := func() { b.WriteString(",") }
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
writeComma()
|
||||
}
|
||||
}
|
||||
b := make([]byte, 0)
|
||||
notEmpty := false
|
||||
b.Write([]byte{'{'})
|
||||
write(&b, '{')
|
||||
|
||||
OnObject(t, func(o *Object) error {
|
||||
notEmpty = writeObject(&b, *o)
|
||||
return nil
|
||||
})
|
||||
if len(t.FormerType) > 0 {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
if v, err := t.FormerType.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(&b, "formerType", v) || notEmpty
|
||||
}
|
||||
}
|
||||
if !t.Deleted.IsZero() {
|
||||
writeCommaIfNotEmpty(notEmpty)
|
||||
notEmpty = writeTimeProp(&b, "deleted", t.Deleted) || notEmpty
|
||||
}
|
||||
if notEmpty {
|
||||
b.Write([]byte{'}'})
|
||||
return b.Bytes(), nil
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
Reference in a new issue