Renamed internal functions used for JSON buffer write
This is done to ensure we're not overlapping with the future gob functionality
This commit is contained in:
parent
0c3f57f637
commit
a65a3e9048
|
@ -356,7 +356,7 @@ func removeFromCollection(col ItemCollection, items ...Item) ItemCollection {
|
|||
for _, ob := range col {
|
||||
found := false
|
||||
for _, it := range items {
|
||||
if IRI(ob.GetID()).Equals(IRI(it.GetID()), false) {
|
||||
if ob.GetID().Equals(it.GetID(), false) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
@ -751,7 +751,7 @@ func (a Activity) MarshalJSON() ([]byte, error) {
|
|||
b := make([]byte, 0)
|
||||
write(&b, '{')
|
||||
|
||||
if !writeActivityValue(&b, a) {
|
||||
if !writeActivityJSONValue(&b, a) {
|
||||
return nil, nil
|
||||
}
|
||||
write(&b, '}')
|
||||
|
|
38
actor.go
38
actor.go
|
@ -216,13 +216,13 @@ func (p PublicKey) MarshalJSON() ([]byte, error) {
|
|||
notEmpty := true
|
||||
write(&b, '{')
|
||||
if v, err := p.ID.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = !writeProp(&b, "id", v)
|
||||
notEmpty = !writeJSONProp(&b, "id", v)
|
||||
}
|
||||
if p.Owner != nil {
|
||||
notEmpty = writeIRIProp(&b, "owner", p.Owner) || notEmpty
|
||||
notEmpty = writeIRIJSONProp(&b, "owner", p.Owner) || notEmpty
|
||||
}
|
||||
if len(p.PublicKeyPem) > 0 {
|
||||
notEmpty = writeIRIProp(&b, "publicKeyPem", p.Owner) || notEmpty
|
||||
notEmpty = writeIRIJSONProp(&b, "publicKeyPem", p.Owner) || notEmpty
|
||||
}
|
||||
|
||||
if notEmpty {
|
||||
|
@ -325,37 +325,37 @@ func (a Actor) MarshalJSON() ([]byte, error) {
|
|||
write(&b, '{')
|
||||
|
||||
OnObject(a, func(o *Object) error {
|
||||
notEmpty = writeObjectValue(&b, *o)
|
||||
notEmpty = writeObjectJSONValue(&b, *o)
|
||||
return nil
|
||||
})
|
||||
if a.Inbox != nil {
|
||||
notEmpty = writeItemProp(&b, "inbox", a.Inbox) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "inbox", a.Inbox) || notEmpty
|
||||
}
|
||||
if a.Outbox != nil {
|
||||
notEmpty = writeItemProp(&b, "outbox", a.Outbox) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "outbox", a.Outbox) || notEmpty
|
||||
}
|
||||
if a.Following != nil {
|
||||
notEmpty = writeItemProp(&b, "following", a.Following) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "following", a.Following) || notEmpty
|
||||
}
|
||||
if a.Followers != nil {
|
||||
notEmpty = writeItemProp(&b, "followers", a.Followers) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "followers", a.Followers) || notEmpty
|
||||
}
|
||||
if a.Liked != nil {
|
||||
notEmpty = writeItemProp(&b, "liked", a.Liked) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "liked", a.Liked) || notEmpty
|
||||
}
|
||||
if a.PreferredUsername != nil {
|
||||
notEmpty = writeNaturalLanguageProp(&b, "preferredUsername", a.PreferredUsername) || notEmpty
|
||||
notEmpty = writeNaturalLanguageJSONProp(&b, "preferredUsername", a.PreferredUsername) || notEmpty
|
||||
}
|
||||
if a.Endpoints != nil {
|
||||
if v, err := a.Endpoints.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(&b, "endpoints", v) || notEmpty
|
||||
notEmpty = writeJSONProp(&b, "endpoints", v) || notEmpty
|
||||
}
|
||||
}
|
||||
if len(a.Streams) > 0 {
|
||||
writePropName(&b, "streams")
|
||||
writePropJSONName(&b, "streams")
|
||||
lNotEmpty := true
|
||||
for _, ss := range a.Streams {
|
||||
lNotEmpty = writeItemCollectionValue(&b, ss) || lNotEmpty
|
||||
lNotEmpty = writeItemCollectionJSONValue(&b, ss) || lNotEmpty
|
||||
}
|
||||
notEmpty = lNotEmpty || notEmpty
|
||||
}
|
||||
|
@ -412,22 +412,22 @@ func (e Endpoints) MarshalJSON() ([]byte, error) {
|
|||
|
||||
write(&b, '{')
|
||||
if e.OauthAuthorizationEndpoint != nil {
|
||||
notEmpty = writeItemProp(&b, "oauthAuthorizationEndpoint", e.OauthAuthorizationEndpoint) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "oauthAuthorizationEndpoint", e.OauthAuthorizationEndpoint) || notEmpty
|
||||
}
|
||||
if e.OauthTokenEndpoint != nil {
|
||||
notEmpty = writeItemProp(&b, "oauthTokenEndpoint", e.OauthTokenEndpoint) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "oauthTokenEndpoint", e.OauthTokenEndpoint) || notEmpty
|
||||
}
|
||||
if e.ProvideClientKey != nil {
|
||||
notEmpty = writeItemProp(&b, "provideClientKey", e.ProvideClientKey) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "provideClientKey", e.ProvideClientKey) || notEmpty
|
||||
}
|
||||
if e.SignClientKey != nil {
|
||||
notEmpty = writeItemProp(&b, "signClientKey", e.SignClientKey) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "signClientKey", e.SignClientKey) || notEmpty
|
||||
}
|
||||
if e.SharedInbox != nil {
|
||||
notEmpty = writeItemProp(&b, "sharedInbox", e.SharedInbox) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "sharedInbox", e.SharedInbox) || notEmpty
|
||||
}
|
||||
if e.UploadMedia != nil {
|
||||
notEmpty = writeItemProp(&b, "uploadMedia", e.UploadMedia) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "uploadMedia", e.UploadMedia) || notEmpty
|
||||
}
|
||||
if notEmpty {
|
||||
write(&b, '}')
|
||||
|
|
|
@ -240,21 +240,21 @@ func (c Collection) MarshalJSON() ([]byte, error) {
|
|||
write(&b, '{')
|
||||
|
||||
OnObject(c, func(o *Object) error {
|
||||
notEmpty = writeObjectValue(&b, *o)
|
||||
notEmpty = writeObjectJSONValue(&b, *o)
|
||||
return nil
|
||||
})
|
||||
if c.Current != nil {
|
||||
notEmpty = writeItemProp(&b, "current", c.Current) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "current", c.Current) || notEmpty
|
||||
}
|
||||
if c.First != nil {
|
||||
notEmpty = writeItemProp(&b, "first", c.First) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "first", c.First) || notEmpty
|
||||
}
|
||||
if c.Last != nil {
|
||||
notEmpty = writeItemProp(&b, "last", c.Last) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "last", c.Last) || notEmpty
|
||||
}
|
||||
notEmpty = writeIntProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty
|
||||
notEmpty = writeIntJSONProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty
|
||||
if c.Items != nil {
|
||||
notEmpty = writeItemCollectionProp(&b, "items", c.Items) || notEmpty
|
||||
notEmpty = writeItemCollectionJSONProp(&b, "items", c.Items) || notEmpty
|
||||
}
|
||||
if notEmpty {
|
||||
write(&b, '}')
|
||||
|
|
|
@ -195,30 +195,30 @@ func (c CollectionPage) MarshalJSON() ([]byte, error) {
|
|||
write(&b, '{')
|
||||
|
||||
OnObject(c, func(o *Object) error {
|
||||
notEmpty = writeObjectValue(&b, *o)
|
||||
notEmpty = writeObjectJSONValue(&b, *o)
|
||||
return nil
|
||||
})
|
||||
if c.PartOf != nil {
|
||||
notEmpty = writeItemProp(&b, "partOf", c.PartOf) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "partOf", c.PartOf) || notEmpty
|
||||
}
|
||||
if c.Current != nil {
|
||||
notEmpty = writeItemProp(&b, "current", c.Current) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "current", c.Current) || notEmpty
|
||||
}
|
||||
if c.First != nil {
|
||||
notEmpty = writeItemProp(&b, "first", c.First) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "first", c.First) || notEmpty
|
||||
}
|
||||
if c.Last != nil {
|
||||
notEmpty = writeItemProp(&b, "last", c.Last) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "last", c.Last) || notEmpty
|
||||
}
|
||||
if c.Next != nil {
|
||||
notEmpty = writeItemProp(&b, "next", c.Next) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "next", c.Next) || notEmpty
|
||||
}
|
||||
if c.Prev != nil {
|
||||
notEmpty = writeItemProp(&b, "prev", c.Prev) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "prev", c.Prev) || notEmpty
|
||||
}
|
||||
notEmpty = writeIntProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty
|
||||
notEmpty = writeIntJSONProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty
|
||||
if c.Items != nil {
|
||||
notEmpty = writeItemCollectionProp(&b, "items", c.Items) || notEmpty
|
||||
notEmpty = writeItemCollectionJSONProp(&b, "items", c.Items) || notEmpty
|
||||
}
|
||||
if notEmpty {
|
||||
write(&b, '}')
|
||||
|
|
351
encoding.go
351
encoding.go
|
@ -1,351 +0,0 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.sr.ht/~mariusor/go-xsd-duration"
|
||||
"github.com/go-ap/jsonld"
|
||||
"time"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
writeComma(b)
|
||||
success := writePropName(b, name) && writeValue(b, val)
|
||||
if !success {
|
||||
*b = (*b)[:len(*b)-1]
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
write(b, '"')
|
||||
writeS(b, s)
|
||||
write(b, '"', ':')
|
||||
return true
|
||||
}
|
||||
|
||||
func writeValue(b *[]byte, s []byte) (notEmpty bool) {
|
||||
if len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
write(b, s...)
|
||||
return true
|
||||
}
|
||||
|
||||
func writeNaturalLanguageProp(b *[]byte, n string, nl NaturalLanguageValues) (notEmpty bool) {
|
||||
l := nl.Count()
|
||||
if l > 1 {
|
||||
n += "Map"
|
||||
}
|
||||
if v, err := nl.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
return writeProp(b, n, v)
|
||||
}
|
||||
return false
|
||||
}
|
||||
func writeStringProp(b *[]byte, n string, s string) (notEmpty bool) {
|
||||
return writeProp(b, n, []byte(fmt.Sprintf(`"%s"`, s)))
|
||||
}
|
||||
func writeBoolProp(b *[]byte, n string, t bool) (notEmpty bool) {
|
||||
return writeProp(b, n, []byte(fmt.Sprintf(`"%t"`, t)))
|
||||
}
|
||||
func writeIntProp(b *[]byte, n string, d int64) (notEmpty bool) {
|
||||
return writeProp(b, n, []byte(fmt.Sprintf("%d", d)))
|
||||
}
|
||||
func writeFloatProp(b *[]byte, n string, f float64) (notEmpty bool) {
|
||||
return writeProp(b, n, []byte(fmt.Sprintf("%f", f)))
|
||||
}
|
||||
func writeTimeProp(b *[]byte, n string, t time.Time) (notEmpty bool) {
|
||||
if v, err := t.UTC().MarshalJSON(); err == nil {
|
||||
return writeProp(b, n, v)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func writeDurationProp(b *[]byte, n string, d time.Duration) (notEmpty bool) {
|
||||
if v, err := xsd.Marshal(d); err == nil {
|
||||
return writeProp(b, n, v)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func writeIRIProp(b *[]byte, n string, i LinkOrIRI) (notEmpty bool) {
|
||||
url := i.GetLink().String()
|
||||
if len(url) == 0 {
|
||||
return false
|
||||
}
|
||||
writeStringProp(b, n, url)
|
||||
return true
|
||||
}
|
||||
|
||||
func writeItemProp(b *[]byte, n string, i Item) (notEmpty bool) {
|
||||
if IsNil(i) {
|
||||
return notEmpty
|
||||
}
|
||||
if im, ok := i.(json.Marshaler); ok {
|
||||
v, err := im.MarshalJSON()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return writeProp(b, n, v)
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeStringValue(b *[]byte, s string) (notEmpty bool) {
|
||||
if len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
write(b, '"')
|
||||
writeS(b, s)
|
||||
write(b, '"')
|
||||
return true
|
||||
}
|
||||
|
||||
func writeItemCollectionValue(b *[]byte, col ItemCollection) (notEmpty bool) {
|
||||
if len(col) == 0 {
|
||||
return notEmpty
|
||||
}
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
write(b, ',')
|
||||
}
|
||||
}
|
||||
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...)
|
||||
}
|
||||
}
|
||||
write(b, ']')
|
||||
return true
|
||||
}
|
||||
func writeItemCollectionProp(b *[]byte, n string, col ItemCollection) (notEmpty bool) {
|
||||
if len(col) == 0 {
|
||||
return notEmpty
|
||||
}
|
||||
writeComma(b)
|
||||
success := writePropName(b, n) && writeItemCollectionValue(b, col)
|
||||
if !success {
|
||||
*b = (*b)[:len(*b)-1]
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
func writeObjectValue(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 {
|
||||
notEmpty = writeProp(b, "type", v) || notEmpty
|
||||
}
|
||||
if v, err := o.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(b, "mediaType", v) || notEmpty
|
||||
}
|
||||
if len(o.Name) > 0 {
|
||||
notEmpty = writeNaturalLanguageProp(b, "name", o.Name) || notEmpty
|
||||
}
|
||||
if len(o.Summary) > 0 {
|
||||
notEmpty = writeNaturalLanguageProp(b, "summary", o.Summary) || notEmpty
|
||||
}
|
||||
if len(o.Content) > 0 {
|
||||
notEmpty = writeNaturalLanguageProp(b, "content", o.Content) || notEmpty
|
||||
}
|
||||
if o.Attachment != nil {
|
||||
notEmpty = writeItemProp(b, "attachment", o.Attachment) || notEmpty
|
||||
}
|
||||
if o.AttributedTo != nil {
|
||||
notEmpty = writeItemProp(b, "attributedTo", o.AttributedTo) || notEmpty
|
||||
}
|
||||
if o.Audience != nil {
|
||||
notEmpty = writeItemProp(b, "audience", o.Audience) || notEmpty
|
||||
}
|
||||
if o.Context != nil {
|
||||
notEmpty = writeItemProp(b, "context", o.Context) || notEmpty
|
||||
}
|
||||
if o.Generator != nil {
|
||||
notEmpty = writeItemProp(b, "generator", o.Generator) || notEmpty
|
||||
}
|
||||
if o.Icon != nil {
|
||||
notEmpty = writeItemProp(b, "icon", o.Icon) || notEmpty
|
||||
}
|
||||
if o.Image != nil {
|
||||
notEmpty = writeItemProp(b, "image", o.Image) || notEmpty
|
||||
}
|
||||
if o.InReplyTo != nil {
|
||||
notEmpty = writeItemProp(b, "inReplyTo", o.InReplyTo) || notEmpty
|
||||
}
|
||||
if o.Location != nil {
|
||||
notEmpty = writeItemProp(b, "location", o.Location) || notEmpty
|
||||
}
|
||||
if o.Preview != nil {
|
||||
notEmpty = writeItemProp(b, "preview", o.Preview) || notEmpty
|
||||
}
|
||||
if o.Replies != nil {
|
||||
notEmpty = writeItemProp(b, "replies", o.Replies) || notEmpty
|
||||
}
|
||||
if o.Tag != nil {
|
||||
notEmpty = writeItemProp(b, "tag", o.Tag) || notEmpty
|
||||
}
|
||||
if o.URL != nil {
|
||||
notEmpty = writeIRIProp(b, "url", o.URL) || notEmpty
|
||||
}
|
||||
if o.To != nil {
|
||||
notEmpty = writeItemProp(b, "to", o.To) || notEmpty
|
||||
}
|
||||
if o.Bto != nil {
|
||||
notEmpty = writeItemProp(b, "bto", o.Bto) || notEmpty
|
||||
}
|
||||
if o.CC != nil {
|
||||
notEmpty = writeItemProp(b, "cc", o.CC) || notEmpty
|
||||
}
|
||||
if o.BCC != nil {
|
||||
notEmpty = writeItemProp(b, "bcc", o.BCC) || notEmpty
|
||||
}
|
||||
if !o.Published.IsZero() {
|
||||
notEmpty = writeTimeProp(b, "published", o.Published) || notEmpty
|
||||
}
|
||||
if !o.Updated.IsZero() {
|
||||
notEmpty = writeTimeProp(b, "updated", o.Updated) || notEmpty
|
||||
}
|
||||
if !o.StartTime.IsZero() {
|
||||
notEmpty = writeTimeProp(b, "startTime", o.StartTime) || notEmpty
|
||||
}
|
||||
if !o.EndTime.IsZero() {
|
||||
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)
|
||||
notEmpty = writeDurationProp(b, "duration", o.Duration) || notEmpty
|
||||
}
|
||||
if o.Likes != nil {
|
||||
notEmpty = writeItemProp(b, "likes", o.Likes) || notEmpty
|
||||
}
|
||||
if o.Shares != nil {
|
||||
notEmpty = writeItemProp(b, "shares", o.Shares) || notEmpty
|
||||
}
|
||||
if v, err := o.Source.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(b, "source", v) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeActivityValue(b *[]byte, a Activity) (notEmpty bool) {
|
||||
OnIntransitiveActivity(a, func(i *IntransitiveActivity) error {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
notEmpty = writeIntransitiveActivityValue(b, *i) || notEmpty
|
||||
return nil
|
||||
})
|
||||
if a.Object != nil {
|
||||
notEmpty = writeItemProp(b, "object", a.Object) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeIntransitiveActivityValue(b *[]byte, i IntransitiveActivity) (notEmpty bool) {
|
||||
OnObject(i, func(o *Object) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
notEmpty = writeObjectValue(b, *o) || notEmpty
|
||||
return nil
|
||||
})
|
||||
if i.Actor != nil {
|
||||
notEmpty = writeItemProp(b, "actor", i.Actor) || notEmpty
|
||||
}
|
||||
if i.Target != nil {
|
||||
notEmpty = writeItemProp(b, "target", i.Target) || notEmpty
|
||||
}
|
||||
if i.Result != nil {
|
||||
notEmpty = writeItemProp(b, "result", i.Result) || notEmpty
|
||||
}
|
||||
if i.Origin != nil {
|
||||
notEmpty = writeItemProp(b, "origin", i.Origin) || notEmpty
|
||||
}
|
||||
if i.Instrument != nil {
|
||||
notEmpty = writeItemProp(b, "instrument", i.Instrument) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeQuestionValue(b *[]byte, q Question) (notEmpty bool) {
|
||||
OnIntransitiveActivity(q, func(i *IntransitiveActivity) error {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
notEmpty = writeIntransitiveActivityValue(b, *i) || notEmpty
|
||||
return nil
|
||||
})
|
||||
if q.OneOf != nil {
|
||||
notEmpty = writeItemProp(b, "oneOf", q.OneOf) || notEmpty
|
||||
} else if q.AnyOf != nil {
|
||||
notEmpty = writeItemProp(b, "anyOf", q.OneOf) || notEmpty
|
||||
}
|
||||
notEmpty = writeBoolProp(b, "closed", q.Closed) || notEmpty
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeLinkValue(b *[]byte, l Link) (notEmpty bool) {
|
||||
if v, err := l.ID.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(b, "id", v) || notEmpty
|
||||
}
|
||||
if v, err := l.Type.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(b, "type", v) || notEmpty
|
||||
}
|
||||
if v, err := l.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(b, "mediaType", v) || notEmpty
|
||||
}
|
||||
if len(l.Name) > 0 {
|
||||
notEmpty = writeNaturalLanguageProp(b, "name", l.Name) || notEmpty
|
||||
}
|
||||
if v, err := l.Rel.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(b, "rel", v) || notEmpty
|
||||
}
|
||||
if l.Height > 0 {
|
||||
notEmpty = writeIntProp(b, "height", int64(l.Height))
|
||||
}
|
||||
if l.Width > 0 {
|
||||
notEmpty = writeIntProp(b, "width", int64(l.Width))
|
||||
}
|
||||
if l.Preview != nil {
|
||||
notEmpty = writeItemProp(b, "rel", l.Preview) || notEmpty
|
||||
}
|
||||
if v, err := l.Href.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(b, "href", v) || notEmpty
|
||||
}
|
||||
if len(l.HrefLang) > 0 {
|
||||
notEmpty = writeStringProp(b, "hrefLang", string(l.HrefLang)) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
// MarshalJSON wraps the jsonld.Marshal function
|
||||
func MarshalJSON(it Item) ([]byte, error) {
|
||||
return jsonld.Marshal(it)
|
||||
}
|
358
encoding_json.go
Normal file
358
encoding_json.go
Normal file
|
@ -0,0 +1,358 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.sr.ht/~mariusor/go-xsd-duration"
|
||||
"github.com/go-ap/jsonld"
|
||||
"time"
|
||||
)
|
||||
|
||||
func writeComma(b *[]byte) {
|
||||
if len(*b) > 1 && (*b)[len(*b)-1] != ',' {
|
||||
*b = append(*b, ',')
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSONProp(b *[]byte, name string, val []byte) (notEmpty bool) {
|
||||
if len(val) == 0 {
|
||||
return false
|
||||
}
|
||||
writeComma(b)
|
||||
success := writePropJSONName(b, name) && writeJSONValue(b, val)
|
||||
if !success {
|
||||
*b = (*b)[:len(*b)-1]
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
func write(b *[]byte, c ...byte) {
|
||||
*b = append(*b, c...)
|
||||
}
|
||||
|
||||
func writeS(b *[]byte, s string) {
|
||||
*b = append(*b, s...)
|
||||
}
|
||||
|
||||
func writePropJSONName(b *[]byte, s string) (notEmpty bool) {
|
||||
if len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
write(b, '"')
|
||||
writeS(b, s)
|
||||
write(b, '"', ':')
|
||||
return true
|
||||
}
|
||||
|
||||
func writeJSONValue(b *[]byte, s []byte) (notEmpty bool) {
|
||||
if len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
write(b, s...)
|
||||
return true
|
||||
}
|
||||
|
||||
func writeNaturalLanguageJSONProp(b *[]byte, n string, nl NaturalLanguageValues) (notEmpty bool) {
|
||||
l := nl.Count()
|
||||
if l > 1 {
|
||||
n += "Map"
|
||||
}
|
||||
if v, err := nl.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
return writeJSONProp(b, n, v)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func writeStringJSONProp(b *[]byte, n string, s string) (notEmpty bool) {
|
||||
return writeJSONProp(b, n, []byte(fmt.Sprintf(`"%s"`, s)))
|
||||
}
|
||||
|
||||
func writeBoolJSONProp(b *[]byte, n string, t bool) (notEmpty bool) {
|
||||
return writeJSONProp(b, n, []byte(fmt.Sprintf(`"%t"`, t)))
|
||||
}
|
||||
|
||||
func writeIntJSONProp(b *[]byte, n string, d int64) (notEmpty bool) {
|
||||
return writeJSONProp(b, n, []byte(fmt.Sprintf("%d", d)))
|
||||
}
|
||||
|
||||
func writeFloatJSONProp(b *[]byte, n string, f float64) (notEmpty bool) {
|
||||
return writeJSONProp(b, n, []byte(fmt.Sprintf("%f", f)))
|
||||
}
|
||||
|
||||
func writeTimeJSONProp(b *[]byte, n string, t time.Time) (notEmpty bool) {
|
||||
if v, err := t.UTC().MarshalJSON(); err == nil {
|
||||
return writeJSONProp(b, n, v)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func writeDurationJSONProp(b *[]byte, n string, d time.Duration) (notEmpty bool) {
|
||||
if v, err := xsd.Marshal(d); err == nil {
|
||||
return writeJSONProp(b, n, v)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func writeIRIJSONProp(b *[]byte, n string, i LinkOrIRI) (notEmpty bool) {
|
||||
url := i.GetLink().String()
|
||||
if len(url) == 0 {
|
||||
return false
|
||||
}
|
||||
writeStringJSONProp(b, n, url)
|
||||
return true
|
||||
}
|
||||
|
||||
func writeItemJSONProp(b *[]byte, n string, i Item) (notEmpty bool) {
|
||||
if i == nil {
|
||||
return notEmpty
|
||||
}
|
||||
if im, ok := i.(json.Marshaler); ok {
|
||||
v, err := im.MarshalJSON()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return writeJSONProp(b, n, v)
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeStringJSONValue(b *[]byte, s string) (notEmpty bool) {
|
||||
if len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
write(b, '"')
|
||||
writeS(b, s)
|
||||
write(b, '"')
|
||||
return true
|
||||
}
|
||||
|
||||
func writeItemCollectionJSONValue(b *[]byte, col ItemCollection) (notEmpty bool) {
|
||||
if len(col) == 0 {
|
||||
return notEmpty
|
||||
}
|
||||
writeCommaIfNotEmpty := func(notEmpty bool) {
|
||||
if notEmpty {
|
||||
write(b, ',')
|
||||
}
|
||||
}
|
||||
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...)
|
||||
}
|
||||
}
|
||||
write(b, ']')
|
||||
return true
|
||||
}
|
||||
|
||||
func writeItemCollectionJSONProp(b *[]byte, n string, col ItemCollection) (notEmpty bool) {
|
||||
if len(col) == 0 {
|
||||
return notEmpty
|
||||
}
|
||||
writeComma(b)
|
||||
success := writePropJSONName(b, n) && writeItemCollectionJSONValue(b, col)
|
||||
if !success {
|
||||
*b = (*b)[:len(*b)-1]
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
func writeObjectJSONValue(b *[]byte, o Object) (notEmpty bool) {
|
||||
if v, err := o.ID.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeJSONProp(b, "id", v) || notEmpty
|
||||
}
|
||||
if v, err := o.Type.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeJSONProp(b, "type", v) || notEmpty
|
||||
}
|
||||
if v, err := o.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeJSONProp(b, "mediaType", v) || notEmpty
|
||||
}
|
||||
if len(o.Name) > 0 {
|
||||
notEmpty = writeNaturalLanguageJSONProp(b, "name", o.Name) || notEmpty
|
||||
}
|
||||
if len(o.Summary) > 0 {
|
||||
notEmpty = writeNaturalLanguageJSONProp(b, "summary", o.Summary) || notEmpty
|
||||
}
|
||||
if len(o.Content) > 0 {
|
||||
notEmpty = writeNaturalLanguageJSONProp(b, "content", o.Content) || notEmpty
|
||||
}
|
||||
if o.Attachment != nil {
|
||||
notEmpty = writeItemJSONProp(b, "attachment", o.Attachment) || notEmpty
|
||||
}
|
||||
if o.AttributedTo != nil {
|
||||
notEmpty = writeItemJSONProp(b, "attributedTo", o.AttributedTo) || notEmpty
|
||||
}
|
||||
if o.Audience != nil {
|
||||
notEmpty = writeItemJSONProp(b, "audience", o.Audience) || notEmpty
|
||||
}
|
||||
if o.Context != nil {
|
||||
notEmpty = writeItemJSONProp(b, "context", o.Context) || notEmpty
|
||||
}
|
||||
if o.Generator != nil {
|
||||
notEmpty = writeItemJSONProp(b, "generator", o.Generator) || notEmpty
|
||||
}
|
||||
if o.Icon != nil {
|
||||
notEmpty = writeItemJSONProp(b, "icon", o.Icon) || notEmpty
|
||||
}
|
||||
if o.Image != nil {
|
||||
notEmpty = writeItemJSONProp(b, "image", o.Image) || notEmpty
|
||||
}
|
||||
if o.InReplyTo != nil {
|
||||
notEmpty = writeItemJSONProp(b, "inReplyTo", o.InReplyTo) || notEmpty
|
||||
}
|
||||
if o.Location != nil {
|
||||
notEmpty = writeItemJSONProp(b, "location", o.Location) || notEmpty
|
||||
}
|
||||
if o.Preview != nil {
|
||||
notEmpty = writeItemJSONProp(b, "preview", o.Preview) || notEmpty
|
||||
}
|
||||
if o.Replies != nil {
|
||||
notEmpty = writeItemJSONProp(b, "replies", o.Replies) || notEmpty
|
||||
}
|
||||
if o.Tag != nil {
|
||||
notEmpty = writeItemJSONProp(b, "tag", o.Tag) || notEmpty
|
||||
}
|
||||
if o.URL != nil {
|
||||
notEmpty = writeIRIJSONProp(b, "url", o.URL) || notEmpty
|
||||
}
|
||||
if o.To != nil {
|
||||
notEmpty = writeItemJSONProp(b, "to", o.To) || notEmpty
|
||||
}
|
||||
if o.Bto != nil {
|
||||
notEmpty = writeItemJSONProp(b, "bto", o.Bto) || notEmpty
|
||||
}
|
||||
if o.CC != nil {
|
||||
notEmpty = writeItemJSONProp(b, "cc", o.CC) || notEmpty
|
||||
}
|
||||
if o.BCC != nil {
|
||||
notEmpty = writeItemJSONProp(b, "bcc", o.BCC) || notEmpty
|
||||
}
|
||||
if !o.Published.IsZero() {
|
||||
notEmpty = writeTimeJSONProp(b, "published", o.Published) || notEmpty
|
||||
}
|
||||
if !o.Updated.IsZero() {
|
||||
notEmpty = writeTimeJSONProp(b, "updated", o.Updated) || notEmpty
|
||||
}
|
||||
if !o.StartTime.IsZero() {
|
||||
notEmpty = writeTimeJSONProp(b, "startTime", o.StartTime) || notEmpty
|
||||
}
|
||||
if !o.EndTime.IsZero() {
|
||||
notEmpty = writeTimeJSONProp(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)
|
||||
notEmpty = writeDurationJSONProp(b, "duration", o.Duration) || notEmpty
|
||||
}
|
||||
if o.Likes != nil {
|
||||
notEmpty = writeItemJSONProp(b, "likes", o.Likes) || notEmpty
|
||||
}
|
||||
if o.Shares != nil {
|
||||
notEmpty = writeItemJSONProp(b, "shares", o.Shares) || notEmpty
|
||||
}
|
||||
if v, err := o.Source.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeJSONProp(b, "source", v) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeActivityJSONValue(b *[]byte, a Activity) (notEmpty bool) {
|
||||
OnIntransitiveActivity(a, func(i *IntransitiveActivity) error {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
notEmpty = writeIntransitiveActivityJSONValue(b, *i) || notEmpty
|
||||
return nil
|
||||
})
|
||||
if a.Object != nil {
|
||||
notEmpty = writeItemJSONProp(b, "object", a.Object) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeIntransitiveActivityJSONValue(b *[]byte, i IntransitiveActivity) (notEmpty bool) {
|
||||
OnObject(i, func(o *Object) error {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
notEmpty = writeObjectJSONValue(b, *o) || notEmpty
|
||||
return nil
|
||||
})
|
||||
if i.Actor != nil {
|
||||
notEmpty = writeItemJSONProp(b, "actor", i.Actor) || notEmpty
|
||||
}
|
||||
if i.Target != nil {
|
||||
notEmpty = writeItemJSONProp(b, "target", i.Target) || notEmpty
|
||||
}
|
||||
if i.Result != nil {
|
||||
notEmpty = writeItemJSONProp(b, "result", i.Result) || notEmpty
|
||||
}
|
||||
if i.Origin != nil {
|
||||
notEmpty = writeItemJSONProp(b, "origin", i.Origin) || notEmpty
|
||||
}
|
||||
if i.Instrument != nil {
|
||||
notEmpty = writeItemJSONProp(b, "instrument", i.Instrument) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeQuestionJSONValue(b *[]byte, q Question) (notEmpty bool) {
|
||||
OnIntransitiveActivity(q, func(i *IntransitiveActivity) error {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
notEmpty = writeIntransitiveActivityJSONValue(b, *i) || notEmpty
|
||||
return nil
|
||||
})
|
||||
if q.OneOf != nil {
|
||||
notEmpty = writeItemJSONProp(b, "oneOf", q.OneOf) || notEmpty
|
||||
} else if q.AnyOf != nil {
|
||||
notEmpty = writeItemJSONProp(b, "anyOf", q.OneOf) || notEmpty
|
||||
}
|
||||
notEmpty = writeBoolJSONProp(b, "closed", q.Closed) || notEmpty
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
func writeLinkJSONValue(b *[]byte, l Link) (notEmpty bool) {
|
||||
if v, err := l.ID.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeJSONProp(b, "id", v) || notEmpty
|
||||
}
|
||||
if v, err := l.Type.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeJSONProp(b, "type", v) || notEmpty
|
||||
}
|
||||
if v, err := l.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeJSONProp(b, "mediaType", v) || notEmpty
|
||||
}
|
||||
if len(l.Name) > 0 {
|
||||
notEmpty = writeNaturalLanguageJSONProp(b, "name", l.Name) || notEmpty
|
||||
}
|
||||
if v, err := l.Rel.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeJSONProp(b, "rel", v) || notEmpty
|
||||
}
|
||||
if l.Height > 0 {
|
||||
notEmpty = writeIntJSONProp(b, "height", int64(l.Height))
|
||||
}
|
||||
if l.Width > 0 {
|
||||
notEmpty = writeIntJSONProp(b, "width", int64(l.Width))
|
||||
}
|
||||
if l.Preview != nil {
|
||||
notEmpty = writeItemJSONProp(b, "rel", l.Preview) || notEmpty
|
||||
}
|
||||
if v, err := l.Href.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeJSONProp(b, "href", v) || notEmpty
|
||||
}
|
||||
if len(l.HrefLang) > 0 {
|
||||
notEmpty = writeStringJSONProp(b, "hrefLang", string(l.HrefLang)) || notEmpty
|
||||
}
|
||||
return notEmpty
|
||||
}
|
||||
|
||||
// MarshalJSON wraps the jsonld.Marshal function
|
||||
func MarshalJSON(it Item) ([]byte, error) {
|
||||
return jsonld.Marshal(it)
|
||||
}
|
|
@ -36,8 +36,8 @@ func Test_writeActivity(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeActivityValue(tt.args.b, tt.args.a); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeActivityValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeActivityJSONValue(tt.args.b, tt.args.a); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeActivityJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -58,8 +58,8 @@ func Test_writeBoolProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeBoolProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeBoolProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeBoolJSONProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeBoolJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -96,8 +96,8 @@ func Test_writeDurationProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeDurationProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeDurationProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeDurationJSONProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeDurationJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -118,8 +118,8 @@ func Test_writeFloatProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeFloatProp(tt.args.b, tt.args.n, tt.args.f); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeFloatProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeFloatJSONProp(tt.args.b, tt.args.n, tt.args.f); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeFloatJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -140,8 +140,8 @@ func Test_writeIRIProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeIRIProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeIRIProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeIRIJSONProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeIRIJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -162,8 +162,8 @@ func Test_writeIntProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeIntProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeIntProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeIntJSONProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeIntJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -183,8 +183,8 @@ func Test_writeIntransitiveActivity(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeIntransitiveActivityValue(tt.args.b, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeIntransitiveActivityValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeIntransitiveActivityJSONValue(tt.args.b, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeIntransitiveActivityJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -204,8 +204,8 @@ func Test_writeItemCollection(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeItemCollectionValue(tt.args.b, tt.args.col); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeItemCollectionValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeItemCollectionJSONValue(tt.args.b, tt.args.col); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeItemCollectionJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -226,8 +226,8 @@ func Test_writeItemCollectionProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeItemCollectionProp(tt.args.b, tt.args.n, tt.args.col); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeItemCollectionProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeItemCollectionJSONProp(tt.args.b, tt.args.n, tt.args.col); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeItemCollectionJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -248,8 +248,8 @@ func Test_writeItemProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeItemProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeItemProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeItemJSONProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeItemJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -270,8 +270,8 @@ func Test_writeNaturalLanguageProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeNaturalLanguageProp(tt.args.b, tt.args.n, tt.args.nl); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeNaturalLanguageProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeNaturalLanguageJSONProp(tt.args.b, tt.args.n, tt.args.nl); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeNaturalLanguageJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -291,8 +291,8 @@ func Test_writeObject(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeObjectValue(tt.args.b, tt.args.o); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeObjectValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeObjectJSONValue(tt.args.b, tt.args.o); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeObjectJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -313,8 +313,8 @@ func Test_writeProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeProp(tt.args.b, tt.args.name, tt.args.val); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeJSONProp(tt.args.b, tt.args.name, tt.args.val); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -334,8 +334,8 @@ func Test_writePropName(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writePropName(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writePropName() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writePropJSONName(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writePropJSONName() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -355,8 +355,8 @@ func Test_writeQuestion(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeQuestionValue(tt.args.b, tt.args.q); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeQuestionValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeQuestionJSONValue(tt.args.b, tt.args.q); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeQuestionJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -393,8 +393,8 @@ func Test_writeString(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeStringValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeStringValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeStringJSONValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeStringJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -415,8 +415,8 @@ func Test_writeStringProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeStringProp(tt.args.b, tt.args.n, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeStringProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeStringJSONProp(tt.args.b, tt.args.n, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeStringJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -437,8 +437,8 @@ func Test_writeTimeProp(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeTimeProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeTimeProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeTimeJSONProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeTimeJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -458,8 +458,8 @@ func Test_writeValue(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotNotEmpty := writeValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
if gotNotEmpty := writeJSONValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
|
||||
t.Errorf("writeJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
|
||||
}
|
||||
})
|
||||
}
|
|
@ -185,7 +185,7 @@ func (i IntransitiveActivity) MarshalJSON() ([]byte, error) {
|
|||
b := make([]byte, 0)
|
||||
write(&b, '{')
|
||||
|
||||
if !writeIntransitiveActivityValue(&b, i) {
|
||||
if !writeIntransitiveActivityJSONValue(&b, i) {
|
||||
return nil, nil
|
||||
}
|
||||
write(&b, '}')
|
||||
|
|
|
@ -36,7 +36,7 @@ func (i ItemCollection) IsObject() bool {
|
|||
|
||||
func (i ItemCollection) MarshalJSON() ([]byte, error) {
|
||||
b := make([]byte, 0)
|
||||
writeItemCollectionValue(&b, i)
|
||||
writeItemCollectionJSONValue(&b, i)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
|
|
2
link.go
2
link.go
|
@ -90,7 +90,7 @@ func (l Link) MarshalJSON() ([]byte, error) {
|
|||
b := make([]byte, 0)
|
||||
write(&b, '{')
|
||||
|
||||
if writeLinkValue(&b, l) {
|
||||
if writeLinkJSONValue(&b, l) {
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
|
|
10
object.go
10
object.go
|
@ -104,7 +104,7 @@ func (a ActivityVocabularyType) MarshalJSON() ([]byte, error) {
|
|||
return nil, nil
|
||||
}
|
||||
b := make([]byte, 0)
|
||||
writeStringValue(&b, string(a))
|
||||
writeStringJSONValue(&b, string(a))
|
||||
return b, nil
|
||||
}
|
||||
|
||||
|
@ -257,7 +257,7 @@ func (o Object) MarshalJSON() ([]byte, error) {
|
|||
b := make([]byte, 0)
|
||||
write(&b, '{')
|
||||
|
||||
if writeObjectValue(&b, o) {
|
||||
if writeObjectJSONValue(&b, o) {
|
||||
write(&b, '}')
|
||||
return b, nil
|
||||
}
|
||||
|
@ -307,7 +307,7 @@ func (m MimeType) MarshalJSON() ([]byte, error) {
|
|||
return nil, nil
|
||||
}
|
||||
b := make([]byte, 0)
|
||||
writeStringValue(&b, string(m))
|
||||
writeStringJSONValue(&b, string(m))
|
||||
return b, nil
|
||||
}
|
||||
|
||||
|
@ -426,11 +426,11 @@ func (s Source) MarshalJSON() ([]byte, error) {
|
|||
write(&b, '{')
|
||||
if len(s.MediaType) > 0 {
|
||||
if v, err := s.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
empty = !writeProp(&b, "mediaType", v)
|
||||
empty = !writeJSONProp(&b, "mediaType", v)
|
||||
}
|
||||
}
|
||||
if len(s.Content) > 0 {
|
||||
empty = !writeNaturalLanguageProp(&b, "content", s.Content)
|
||||
empty = !writeNaturalLanguageJSONProp(&b, "content", s.Content)
|
||||
}
|
||||
if !empty {
|
||||
write(&b, '}')
|
||||
|
|
|
@ -236,21 +236,21 @@ func (o OrderedCollection) MarshalJSON() ([]byte, error) {
|
|||
write(&b, '{')
|
||||
|
||||
OnObject(o, func(o *Object) error {
|
||||
notEmpty = writeObjectValue(&b, *o)
|
||||
notEmpty = writeObjectJSONValue(&b, *o)
|
||||
return nil
|
||||
})
|
||||
if o.Current != nil {
|
||||
notEmpty = writeItemProp(&b, "current", o.Current) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "current", o.Current) || notEmpty
|
||||
}
|
||||
if o.First != nil {
|
||||
notEmpty = writeItemProp(&b, "first", o.First) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "first", o.First) || notEmpty
|
||||
}
|
||||
if o.Last != nil {
|
||||
notEmpty = writeItemProp(&b, "last", o.Last) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "last", o.Last) || notEmpty
|
||||
}
|
||||
notEmpty = writeIntProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty
|
||||
notEmpty = writeIntJSONProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty
|
||||
if o.OrderedItems != nil {
|
||||
notEmpty = writeItemCollectionProp(&b, "orderedItems", o.OrderedItems) || notEmpty
|
||||
notEmpty = writeItemCollectionJSONProp(&b, "orderedItems", o.OrderedItems) || notEmpty
|
||||
}
|
||||
if notEmpty {
|
||||
write(&b, '}')
|
||||
|
|
|
@ -198,30 +198,30 @@ func (o OrderedCollectionPage) MarshalJSON() ([]byte, error) {
|
|||
write(&b, '{')
|
||||
|
||||
OnObject(o, func(o *Object) error {
|
||||
notEmpty = writeObjectValue(&b, *o)
|
||||
notEmpty = writeObjectJSONValue(&b, *o)
|
||||
return nil
|
||||
})
|
||||
if o.PartOf != nil {
|
||||
notEmpty = writeItemProp(&b, "partOf", o.PartOf) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "partOf", o.PartOf) || notEmpty
|
||||
}
|
||||
if o.Current != nil {
|
||||
notEmpty = writeItemProp(&b, "current", o.Current) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "current", o.Current) || notEmpty
|
||||
}
|
||||
if o.First != nil {
|
||||
notEmpty = writeItemProp(&b, "first", o.First) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "first", o.First) || notEmpty
|
||||
}
|
||||
if o.Last != nil {
|
||||
notEmpty = writeItemProp(&b, "last", o.Last) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "last", o.Last) || notEmpty
|
||||
}
|
||||
if o.Next != nil {
|
||||
notEmpty = writeItemProp(&b, "next", o.Next) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "next", o.Next) || notEmpty
|
||||
}
|
||||
if o.Prev != nil {
|
||||
notEmpty = writeItemProp(&b, "prev", o.Prev) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "prev", o.Prev) || notEmpty
|
||||
}
|
||||
notEmpty = writeIntProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty
|
||||
notEmpty = writeIntJSONProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty
|
||||
if o.OrderedItems != nil {
|
||||
notEmpty = writeItemCollectionProp(&b, "orderedItems", o.OrderedItems) || notEmpty
|
||||
notEmpty = writeItemCollectionJSONProp(&b, "orderedItems", o.OrderedItems) || notEmpty
|
||||
}
|
||||
if notEmpty {
|
||||
write(&b, '}')
|
||||
|
|
14
place.go
14
place.go
|
@ -163,26 +163,26 @@ func (p Place) MarshalJSON() ([]byte, error) {
|
|||
write(&b, '{')
|
||||
|
||||
OnObject(p, func(o *Object) error {
|
||||
notEmpty = writeObjectValue(&b, *o)
|
||||
notEmpty = writeObjectJSONValue(&b, *o)
|
||||
return nil
|
||||
})
|
||||
if p.Accuracy > 0 {
|
||||
notEmpty = writeFloatProp(&b, "accuracy", p.Accuracy) || notEmpty
|
||||
notEmpty = writeFloatJSONProp(&b, "accuracy", p.Accuracy) || notEmpty
|
||||
}
|
||||
if p.Altitude > 0 {
|
||||
notEmpty = writeFloatProp(&b, "altitude", p.Altitude) || notEmpty
|
||||
notEmpty = writeFloatJSONProp(&b, "altitude", p.Altitude) || notEmpty
|
||||
}
|
||||
if p.Latitude > 0 {
|
||||
notEmpty = writeFloatProp(&b, "latitude", p.Latitude) || notEmpty
|
||||
notEmpty = writeFloatJSONProp(&b, "latitude", p.Latitude) || notEmpty
|
||||
}
|
||||
if p.Longitude > 0 {
|
||||
notEmpty = writeFloatProp(&b, "longitude", p.Longitude) || notEmpty
|
||||
notEmpty = writeFloatJSONProp(&b, "longitude", p.Longitude) || notEmpty
|
||||
}
|
||||
if p.Radius > 0 {
|
||||
notEmpty = writeIntProp(&b, "radius", p.Radius) || notEmpty
|
||||
notEmpty = writeIntJSONProp(&b, "radius", p.Radius) || notEmpty
|
||||
}
|
||||
if len(p.Units) > 0 {
|
||||
notEmpty = writeStringProp(&b, "radius", p.Units) || notEmpty
|
||||
notEmpty = writeStringJSONProp(&b, "radius", p.Units) || notEmpty
|
||||
}
|
||||
if notEmpty {
|
||||
write(&b, '}')
|
||||
|
|
|
@ -153,7 +153,7 @@ func (p Profile) MarshalJSON() ([]byte, error) {
|
|||
})
|
||||
|
||||
if p.Describes != nil {
|
||||
notEmpty = writeItemProp(&b, "describes", p.Describes) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "describes", p.Describes) || notEmpty
|
||||
}
|
||||
|
||||
if notEmpty {
|
||||
|
|
|
@ -171,7 +171,7 @@ func (q Question) MarshalJSON() ([]byte, error) {
|
|||
b := make([]byte, 0)
|
||||
write(&b, '{')
|
||||
|
||||
if !writeQuestionValue(&b, q) {
|
||||
if !writeQuestionJSONValue(&b, q) {
|
||||
return nil, nil
|
||||
}
|
||||
write(&b, '}')
|
||||
|
|
|
@ -159,18 +159,18 @@ func (r Relationship) MarshalJSON() ([]byte, error) {
|
|||
write(&b, '{')
|
||||
|
||||
OnObject(r, func(o *Object) error {
|
||||
notEmpty = writeObjectValue(&b, *o)
|
||||
notEmpty = writeObjectJSONValue(&b, *o)
|
||||
return nil
|
||||
})
|
||||
|
||||
if r.Subject != nil {
|
||||
notEmpty = writeItemProp(&b, "subject", r.Subject) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "subject", r.Subject) || notEmpty
|
||||
}
|
||||
if r.Object != nil {
|
||||
notEmpty = writeItemProp(&b, "object", r.Object) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "object", r.Object) || notEmpty
|
||||
}
|
||||
if r.Relationship != nil {
|
||||
notEmpty = writeItemProp(&b, "relationship", r.Relationship) || notEmpty
|
||||
notEmpty = writeItemJSONProp(&b, "relationship", r.Relationship) || notEmpty
|
||||
}
|
||||
|
||||
if notEmpty {
|
||||
|
|
|
@ -151,16 +151,16 @@ func (t Tombstone) MarshalJSON() ([]byte, error) {
|
|||
write(&b, '{')
|
||||
|
||||
OnObject(t, func(o *Object) error {
|
||||
notEmpty = writeObjectValue(&b, *o)
|
||||
notEmpty = writeObjectJSONValue(&b, *o)
|
||||
return nil
|
||||
})
|
||||
if len(t.FormerType) > 0 {
|
||||
if v, err := t.FormerType.MarshalJSON(); err == nil && len(v) > 0 {
|
||||
notEmpty = writeProp(&b, "formerType", v) || notEmpty
|
||||
notEmpty = writeJSONProp(&b, "formerType", v) || notEmpty
|
||||
}
|
||||
}
|
||||
if !t.Deleted.IsZero() {
|
||||
notEmpty = writeTimeProp(&b, "deleted", t.Deleted) || notEmpty
|
||||
notEmpty = writeTimeJSONProp(&b, "deleted", t.Deleted) || notEmpty
|
||||
}
|
||||
if notEmpty {
|
||||
write(&b, '}')
|
||||
|
|
Reference in a new issue