Export all write* functions as JSONWrite*

This commit is contained in:
Anthony Wang 2022-08-20 21:27:03 -05:00
parent b9fb54dea7
commit 29bb602eb1
Signed by: a
GPG key ID: BC96B00AEC5F2D76
18 changed files with 284 additions and 284 deletions

View file

@ -831,12 +831,12 @@ func ToActivity(it Item) (*Activity, error) {
// MarshalJSON encodes the receiver object to a JSON document. // MarshalJSON encodes the receiver object to a JSON document.
func (a Activity) MarshalJSON() ([]byte, error) { func (a Activity) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
write(&b, '{') JSONWrite(&b, '{')
if !writeActivityJSONValue(&b, a) { if !JSONWriteActivityJSONValue(&b, a) {
return nil, nil return nil, nil
} }
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }

View file

@ -216,21 +216,21 @@ func (p *PublicKey) UnmarshalJSON(data []byte) error {
func (p PublicKey) MarshalJSON() ([]byte, error) { func (p PublicKey) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := true notEmpty := true
write(&b, '{') JSONWrite(&b, '{')
if v, err := p.ID.MarshalJSON(); err == nil && len(v) > 0 { if v, err := p.ID.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = !writeJSONProp(&b, "id", v) notEmpty = !JSONWriteJSONProp(&b, "id", v)
} }
if len(p.Owner) > 0 { if len(p.Owner) > 0 {
notEmpty = writeIRIJSONProp(&b, "owner", p.Owner) || notEmpty notEmpty = JSONWriteIRIJSONProp(&b, "owner", p.Owner) || notEmpty
} }
if len(p.PublicKeyPem) > 0 { if len(p.PublicKeyPem) > 0 {
if pem, err := json.Marshal(p.PublicKeyPem); err == nil { if pem, err := json.Marshal(p.PublicKeyPem); err == nil {
notEmpty = writeJSONProp(&b, "publicKeyPem", pem) || notEmpty notEmpty = JSONWriteJSONProp(&b, "publicKeyPem", pem) || notEmpty
} }
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil
@ -362,46 +362,46 @@ func (a *Actor) UnmarshalJSON(data []byte) error {
func (a Actor) MarshalJSON() ([]byte, error) { func (a Actor) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := false notEmpty := false
write(&b, '{') JSONWrite(&b, '{')
OnObject(a, func(o *Object) error { OnObject(a, func(o *Object) error {
notEmpty = writeObjectJSONValue(&b, *o) notEmpty = JSONWriteObjectJSONValue(&b, *o)
return nil return nil
}) })
if a.Inbox != nil { if a.Inbox != nil {
notEmpty = writeItemJSONProp(&b, "inbox", a.Inbox) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "inbox", a.Inbox) || notEmpty
} }
if a.Outbox != nil { if a.Outbox != nil {
notEmpty = writeItemJSONProp(&b, "outbox", a.Outbox) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "outbox", a.Outbox) || notEmpty
} }
if a.Following != nil { if a.Following != nil {
notEmpty = writeItemJSONProp(&b, "following", a.Following) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "following", a.Following) || notEmpty
} }
if a.Followers != nil { if a.Followers != nil {
notEmpty = writeItemJSONProp(&b, "followers", a.Followers) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "followers", a.Followers) || notEmpty
} }
if a.Liked != nil { if a.Liked != nil {
notEmpty = writeItemJSONProp(&b, "liked", a.Liked) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "liked", a.Liked) || notEmpty
} }
if a.PreferredUsername != nil { if a.PreferredUsername != nil {
notEmpty = writeNaturalLanguageJSONProp(&b, "preferredUsername", a.PreferredUsername) || notEmpty notEmpty = JSONWriteNaturalLanguageJSONProp(&b, "preferredUsername", a.PreferredUsername) || notEmpty
} }
if a.Endpoints != nil { if a.Endpoints != nil {
if v, err := a.Endpoints.MarshalJSON(); err == nil && len(v) > 0 { if v, err := a.Endpoints.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(&b, "endpoints", v) || notEmpty notEmpty = JSONWriteJSONProp(&b, "endpoints", v) || notEmpty
} }
} }
if len(a.Streams) > 0 { if len(a.Streams) > 0 {
notEmpty = writeItemCollectionJSONProp(&b, "streams", a.Streams) notEmpty = JSONWriteItemCollectionJSONProp(&b, "streams", a.Streams)
} }
if len(a.PublicKey.PublicKeyPem)+len(a.PublicKey.ID) > 0 { if len(a.PublicKey.PublicKeyPem)+len(a.PublicKey.ID) > 0 {
if v, err := a.PublicKey.MarshalJSON(); err == nil && len(v) > 0 { if v, err := a.PublicKey.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(&b, "publicKey", v) || notEmpty notEmpty = JSONWriteJSONProp(&b, "publicKey", v) || notEmpty
} }
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil
@ -462,27 +462,27 @@ func (e Endpoints) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := false notEmpty := false
write(&b, '{') JSONWrite(&b, '{')
if e.OauthAuthorizationEndpoint != nil { if e.OauthAuthorizationEndpoint != nil {
notEmpty = writeItemJSONProp(&b, "oauthAuthorizationEndpoint", e.OauthAuthorizationEndpoint) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "oauthAuthorizationEndpoint", e.OauthAuthorizationEndpoint) || notEmpty
} }
if e.OauthTokenEndpoint != nil { if e.OauthTokenEndpoint != nil {
notEmpty = writeItemJSONProp(&b, "oauthTokenEndpoint", e.OauthTokenEndpoint) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "oauthTokenEndpoint", e.OauthTokenEndpoint) || notEmpty
} }
if e.ProvideClientKey != nil { if e.ProvideClientKey != nil {
notEmpty = writeItemJSONProp(&b, "provideClientKey", e.ProvideClientKey) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "provideClientKey", e.ProvideClientKey) || notEmpty
} }
if e.SignClientKey != nil { if e.SignClientKey != nil {
notEmpty = writeItemJSONProp(&b, "signClientKey", e.SignClientKey) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "signClientKey", e.SignClientKey) || notEmpty
} }
if e.SharedInbox != nil { if e.SharedInbox != nil {
notEmpty = writeItemJSONProp(&b, "sharedInbox", e.SharedInbox) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "sharedInbox", e.SharedInbox) || notEmpty
} }
if e.UploadMedia != nil { if e.UploadMedia != nil {
notEmpty = writeItemJSONProp(&b, "uploadMedia", e.UploadMedia) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "uploadMedia", e.UploadMedia) || notEmpty
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil

View file

@ -245,27 +245,27 @@ func (c *Collection) UnmarshalJSON(data []byte) error {
func (c Collection) MarshalJSON() ([]byte, error) { func (c Collection) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := false notEmpty := false
write(&b, '{') JSONWrite(&b, '{')
OnObject(c, func(o *Object) error { OnObject(c, func(o *Object) error {
notEmpty = writeObjectJSONValue(&b, *o) notEmpty = JSONWriteObjectJSONValue(&b, *o)
return nil return nil
}) })
if c.Current != nil { if c.Current != nil {
notEmpty = writeItemJSONProp(&b, "current", c.Current) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "current", c.Current) || notEmpty
} }
if c.First != nil { if c.First != nil {
notEmpty = writeItemJSONProp(&b, "first", c.First) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "first", c.First) || notEmpty
} }
if c.Last != nil { if c.Last != nil {
notEmpty = writeItemJSONProp(&b, "last", c.Last) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "last", c.Last) || notEmpty
} }
notEmpty = writeIntJSONProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty notEmpty = JSONWriteIntJSONProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty
if c.Items != nil { if c.Items != nil {
notEmpty = writeItemCollectionJSONProp(&b, "items", c.Items) || notEmpty notEmpty = JSONWriteItemCollectionJSONProp(&b, "items", c.Items) || notEmpty
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil

View file

@ -202,36 +202,36 @@ func (c *CollectionPage) UnmarshalJSON(data []byte) error {
func (c CollectionPage) MarshalJSON() ([]byte, error) { func (c CollectionPage) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := false notEmpty := false
write(&b, '{') JSONWrite(&b, '{')
OnObject(c, func(o *Object) error { OnObject(c, func(o *Object) error {
notEmpty = writeObjectJSONValue(&b, *o) notEmpty = JSONWriteObjectJSONValue(&b, *o)
return nil return nil
}) })
if c.PartOf != nil { if c.PartOf != nil {
notEmpty = writeItemJSONProp(&b, "partOf", c.PartOf) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "partOf", c.PartOf) || notEmpty
} }
if c.Current != nil { if c.Current != nil {
notEmpty = writeItemJSONProp(&b, "current", c.Current) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "current", c.Current) || notEmpty
} }
if c.First != nil { if c.First != nil {
notEmpty = writeItemJSONProp(&b, "first", c.First) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "first", c.First) || notEmpty
} }
if c.Last != nil { if c.Last != nil {
notEmpty = writeItemJSONProp(&b, "last", c.Last) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "last", c.Last) || notEmpty
} }
if c.Next != nil { if c.Next != nil {
notEmpty = writeItemJSONProp(&b, "next", c.Next) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "next", c.Next) || notEmpty
} }
if c.Prev != nil { if c.Prev != nil {
notEmpty = writeItemJSONProp(&b, "prev", c.Prev) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "prev", c.Prev) || notEmpty
} }
notEmpty = writeIntJSONProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty notEmpty = JSONWriteIntJSONProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty
if c.Items != nil { if c.Items != nil {
notEmpty = writeItemCollectionJSONProp(&b, "items", c.Items) || notEmpty notEmpty = JSONWriteItemCollectionJSONProp(&b, "items", c.Items) || notEmpty
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil

View file

@ -9,102 +9,102 @@ import (
"github.com/go-ap/jsonld" "github.com/go-ap/jsonld"
) )
func writeComma(b *[]byte) { func JSONWriteComma(b *[]byte) {
if len(*b) > 1 && (*b)[len(*b)-1] != ',' { if len(*b) > 1 && (*b)[len(*b)-1] != ',' {
*b = append(*b, ',') *b = append(*b, ',')
} }
} }
func writeJSONProp(b *[]byte, name string, val []byte) (notEmpty bool) { func JSONWriteJSONProp(b *[]byte, name string, val []byte) (notEmpty bool) {
if len(val) == 0 { if len(val) == 0 {
return false return false
} }
writeComma(b) JSONWriteComma(b)
success := writePropJSONName(b, name) && writeJSONValue(b, val) success := JSONWritePropJSONName(b, name) && JSONWriteJSONValue(b, val)
if !success { if !success {
*b = (*b)[:len(*b)-1] *b = (*b)[:len(*b)-1]
} }
return success return success
} }
func write(b *[]byte, c ...byte) { func JSONWrite(b *[]byte, c ...byte) {
*b = append(*b, c...) *b = append(*b, c...)
} }
func writeS(b *[]byte, s string) { func JSONWriteS(b *[]byte, s string) {
*b = append(*b, s...) *b = append(*b, s...)
} }
func writePropJSONName(b *[]byte, s string) (notEmpty bool) { func JSONWritePropJSONName(b *[]byte, s string) (notEmpty bool) {
if len(s) == 0 { if len(s) == 0 {
return false return false
} }
write(b, '"') JSONWrite(b, '"')
writeS(b, s) JSONWriteS(b, s)
write(b, '"', ':') JSONWrite(b, '"', ':')
return true return true
} }
func writeJSONValue(b *[]byte, s []byte) (notEmpty bool) { func JSONWriteJSONValue(b *[]byte, s []byte) (notEmpty bool) {
if len(s) == 0 { if len(s) == 0 {
return false return false
} }
write(b, s...) JSONWrite(b, s...)
return true return true
} }
func writeNaturalLanguageJSONProp(b *[]byte, n string, nl NaturalLanguageValues) (notEmpty bool) { func JSONWriteNaturalLanguageJSONProp(b *[]byte, n string, nl NaturalLanguageValues) (notEmpty bool) {
l := nl.Count() l := nl.Count()
if l > 1 { if l > 1 {
n += "Map" n += "Map"
} }
if v, err := nl.MarshalJSON(); err == nil && len(v) > 0 { if v, err := nl.MarshalJSON(); err == nil && len(v) > 0 {
return writeJSONProp(b, n, v) return JSONWriteJSONProp(b, n, v)
} }
return false return false
} }
func writeStringJSONProp(b *[]byte, n string, s string) (notEmpty bool) { func JSONWriteStringJSONProp(b *[]byte, n string, s string) (notEmpty bool) {
return writeJSONProp(b, n, []byte(fmt.Sprintf(`"%s"`, s))) return JSONWriteJSONProp(b, n, []byte(fmt.Sprintf(`"%s"`, s)))
} }
func writeBoolJSONProp(b *[]byte, n string, t bool) (notEmpty bool) { func JSONWriteBoolJSONProp(b *[]byte, n string, t bool) (notEmpty bool) {
return writeJSONProp(b, n, []byte(fmt.Sprintf(`"%t"`, t))) return JSONWriteJSONProp(b, n, []byte(fmt.Sprintf(`"%t"`, t)))
} }
func writeIntJSONProp(b *[]byte, n string, d int64) (notEmpty bool) { func JSONWriteIntJSONProp(b *[]byte, n string, d int64) (notEmpty bool) {
return writeJSONProp(b, n, []byte(fmt.Sprintf("%d", d))) return JSONWriteJSONProp(b, n, []byte(fmt.Sprintf("%d", d)))
} }
func writeFloatJSONProp(b *[]byte, n string, f float64) (notEmpty bool) { func JSONWriteFloatJSONProp(b *[]byte, n string, f float64) (notEmpty bool) {
return writeJSONProp(b, n, []byte(fmt.Sprintf("%f", f))) return JSONWriteJSONProp(b, n, []byte(fmt.Sprintf("%f", f)))
} }
func writeTimeJSONProp(b *[]byte, n string, t time.Time) (notEmpty bool) { func JSONWriteTimeJSONProp(b *[]byte, n string, t time.Time) (notEmpty bool) {
var tb []byte var tb []byte
write(&tb, '"') JSONWrite(&tb, '"')
writeS(&tb, t.UTC().Format(time.RFC3339)) JSONWriteS(&tb, t.UTC().Format(time.RFC3339))
write(&tb, '"') JSONWrite(&tb, '"')
return writeJSONProp(b, n, tb) return JSONWriteJSONProp(b, n, tb)
} }
func writeDurationJSONProp(b *[]byte, n string, d time.Duration) (notEmpty bool) { func JSONWriteDurationJSONProp(b *[]byte, n string, d time.Duration) (notEmpty bool) {
if v, err := xsd.Marshal(d); err == nil { if v, err := xsd.Marshal(d); err == nil {
return writeJSONProp(b, n, v) return JSONWriteJSONProp(b, n, v)
} }
return false return false
} }
func writeIRIJSONProp(b *[]byte, n string, i LinkOrIRI) (notEmpty bool) { func JSONWriteIRIJSONProp(b *[]byte, n string, i LinkOrIRI) (notEmpty bool) {
url := i.GetLink().String() url := i.GetLink().String()
if len(url) == 0 { if len(url) == 0 {
return false return false
} }
writeStringJSONProp(b, n, url) JSONWriteStringJSONProp(b, n, url)
return true return true
} }
func writeItemJSONProp(b *[]byte, n string, i Item) (notEmpty bool) { func JSONWriteItemJSONProp(b *[]byte, n string, i Item) (notEmpty bool) {
if i == nil { if i == nil {
return notEmpty return notEmpty
} }
@ -113,244 +113,244 @@ func writeItemJSONProp(b *[]byte, n string, i Item) (notEmpty bool) {
if err != nil { if err != nil {
return false return false
} }
return writeJSONProp(b, n, v) return JSONWriteJSONProp(b, n, v)
} }
return notEmpty return notEmpty
} }
func writeStringJSONValue(b *[]byte, s string) (notEmpty bool) { func JSONWriteStringJSONValue(b *[]byte, s string) (notEmpty bool) {
if len(s) == 0 { if len(s) == 0 {
return false return false
} }
write(b, '"') JSONWrite(b, '"')
writeS(b, s) JSONWriteS(b, s)
write(b, '"') JSONWrite(b, '"')
return true return true
} }
func writeItemCollectionJSONValue(b *[]byte, col ItemCollection) (notEmpty bool) { func JSONWriteItemCollectionJSONValue(b *[]byte, col ItemCollection) (notEmpty bool) {
if len(col) == 0 { if len(col) == 0 {
return notEmpty return notEmpty
} }
writeCommaIfNotEmpty := func(notEmpty bool) { JSONWriteCommaIfNotEmpty := func(notEmpty bool) {
if notEmpty { if notEmpty {
write(b, ',') JSONWrite(b, ',')
} }
} }
write(b, '[') JSONWrite(b, '[')
for i, it := range col { for i, it := range col {
if im, ok := it.(json.Marshaler); ok { if im, ok := it.(json.Marshaler); ok {
v, err := im.MarshalJSON() v, err := im.MarshalJSON()
if err != nil { if err != nil {
return false return false
} }
writeCommaIfNotEmpty(i > 0) JSONWriteCommaIfNotEmpty(i > 0)
write(b, v...) JSONWrite(b, v...)
} }
} }
write(b, ']') JSONWrite(b, ']')
return true return true
} }
func writeItemCollectionJSONProp(b *[]byte, n string, col ItemCollection) (notEmpty bool) { func JSONWriteItemCollectionJSONProp(b *[]byte, n string, col ItemCollection) (notEmpty bool) {
if len(col) == 0 { if len(col) == 0 {
return notEmpty return notEmpty
} }
writeComma(b) JSONWriteComma(b)
success := writePropJSONName(b, n) && writeItemCollectionJSONValue(b, col) success := JSONWritePropJSONName(b, n) && JSONWriteItemCollectionJSONValue(b, col)
if !success { if !success {
*b = (*b)[:len(*b)-1] *b = (*b)[:len(*b)-1]
} }
return success return success
} }
func writeObjectJSONValue(b *[]byte, o Object) (notEmpty bool) { func JSONWriteObjectJSONValue(b *[]byte, o Object) (notEmpty bool) {
if v, err := o.ID.MarshalJSON(); err == nil && len(v) > 0 { if v, err := o.ID.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(b, "id", v) || notEmpty notEmpty = JSONWriteJSONProp(b, "id", v) || notEmpty
} }
if v, err := o.Type.MarshalJSON(); err == nil && len(v) > 0 { if v, err := o.Type.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(b, "type", v) || notEmpty notEmpty = JSONWriteJSONProp(b, "type", v) || notEmpty
} }
if v, err := o.MediaType.MarshalJSON(); err == nil && len(v) > 0 { if v, err := o.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(b, "mediaType", v) || notEmpty notEmpty = JSONWriteJSONProp(b, "mediaType", v) || notEmpty
} }
if len(o.Name) > 0 { if len(o.Name) > 0 {
notEmpty = writeNaturalLanguageJSONProp(b, "name", o.Name) || notEmpty notEmpty = JSONWriteNaturalLanguageJSONProp(b, "name", o.Name) || notEmpty
} }
if len(o.Summary) > 0 { if len(o.Summary) > 0 {
notEmpty = writeNaturalLanguageJSONProp(b, "summary", o.Summary) || notEmpty notEmpty = JSONWriteNaturalLanguageJSONProp(b, "summary", o.Summary) || notEmpty
} }
if len(o.Content) > 0 { if len(o.Content) > 0 {
notEmpty = writeNaturalLanguageJSONProp(b, "content", o.Content) || notEmpty notEmpty = JSONWriteNaturalLanguageJSONProp(b, "content", o.Content) || notEmpty
} }
if o.Attachment != nil { if o.Attachment != nil {
notEmpty = writeItemJSONProp(b, "attachment", o.Attachment) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "attachment", o.Attachment) || notEmpty
} }
if o.AttributedTo != nil { if o.AttributedTo != nil {
notEmpty = writeItemJSONProp(b, "attributedTo", o.AttributedTo) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "attributedTo", o.AttributedTo) || notEmpty
} }
if o.Audience != nil { if o.Audience != nil {
notEmpty = writeItemJSONProp(b, "audience", o.Audience) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "audience", o.Audience) || notEmpty
} }
if o.Context != nil { if o.Context != nil {
notEmpty = writeItemJSONProp(b, "context", o.Context) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "context", o.Context) || notEmpty
} }
if o.Generator != nil { if o.Generator != nil {
notEmpty = writeItemJSONProp(b, "generator", o.Generator) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "generator", o.Generator) || notEmpty
} }
if o.Icon != nil { if o.Icon != nil {
notEmpty = writeItemJSONProp(b, "icon", o.Icon) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "icon", o.Icon) || notEmpty
} }
if o.Image != nil { if o.Image != nil {
notEmpty = writeItemJSONProp(b, "image", o.Image) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "image", o.Image) || notEmpty
} }
if o.InReplyTo != nil { if o.InReplyTo != nil {
notEmpty = writeItemJSONProp(b, "inReplyTo", o.InReplyTo) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "inReplyTo", o.InReplyTo) || notEmpty
} }
if o.Location != nil { if o.Location != nil {
notEmpty = writeItemJSONProp(b, "location", o.Location) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "location", o.Location) || notEmpty
} }
if o.Preview != nil { if o.Preview != nil {
notEmpty = writeItemJSONProp(b, "preview", o.Preview) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "preview", o.Preview) || notEmpty
} }
if o.Replies != nil { if o.Replies != nil {
notEmpty = writeItemJSONProp(b, "replies", o.Replies) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "replies", o.Replies) || notEmpty
} }
if o.Tag != nil { if o.Tag != nil {
notEmpty = writeItemJSONProp(b, "tag", o.Tag) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "tag", o.Tag) || notEmpty
} }
if o.URL != nil { if o.URL != nil {
notEmpty = writeItemJSONProp(b, "url", o.URL) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "url", o.URL) || notEmpty
} }
if o.To != nil { if o.To != nil {
notEmpty = writeItemJSONProp(b, "to", o.To) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "to", o.To) || notEmpty
} }
if o.Bto != nil { if o.Bto != nil {
notEmpty = writeItemJSONProp(b, "bto", o.Bto) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "bto", o.Bto) || notEmpty
} }
if o.CC != nil { if o.CC != nil {
notEmpty = writeItemJSONProp(b, "cc", o.CC) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "cc", o.CC) || notEmpty
} }
if o.BCC != nil { if o.BCC != nil {
notEmpty = writeItemJSONProp(b, "bcc", o.BCC) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "bcc", o.BCC) || notEmpty
} }
if !o.Published.IsZero() { if !o.Published.IsZero() {
notEmpty = writeTimeJSONProp(b, "published", o.Published) || notEmpty notEmpty = JSONWriteTimeJSONProp(b, "published", o.Published) || notEmpty
} }
if !o.Updated.IsZero() { if !o.Updated.IsZero() {
notEmpty = writeTimeJSONProp(b, "updated", o.Updated) || notEmpty notEmpty = JSONWriteTimeJSONProp(b, "updated", o.Updated) || notEmpty
} }
if !o.StartTime.IsZero() { if !o.StartTime.IsZero() {
notEmpty = writeTimeJSONProp(b, "startTime", o.StartTime) || notEmpty notEmpty = JSONWriteTimeJSONProp(b, "startTime", o.StartTime) || notEmpty
} }
if !o.EndTime.IsZero() { if !o.EndTime.IsZero() {
notEmpty = writeTimeJSONProp(b, "endTime", o.EndTime) || notEmpty notEmpty = JSONWriteTimeJSONProp(b, "endTime", o.EndTime) || notEmpty
} }
if o.Duration != 0 { if o.Duration != 0 {
// TODO(marius): maybe don't use 0 as a nil value for Object types // 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) // which can have a valid duration of 0 - (Video, Audio, etc)
notEmpty = writeDurationJSONProp(b, "duration", o.Duration) || notEmpty notEmpty = JSONWriteDurationJSONProp(b, "duration", o.Duration) || notEmpty
} }
if o.Likes != nil { if o.Likes != nil {
notEmpty = writeItemJSONProp(b, "likes", o.Likes) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "likes", o.Likes) || notEmpty
} }
if o.Shares != nil { if o.Shares != nil {
notEmpty = writeItemJSONProp(b, "shares", o.Shares) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "shares", o.Shares) || notEmpty
} }
if v, err := o.Source.MarshalJSON(); err == nil && len(v) > 0 { if v, err := o.Source.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(b, "source", v) || notEmpty notEmpty = JSONWriteJSONProp(b, "source", v) || notEmpty
} }
return notEmpty return notEmpty
} }
func writeActivityJSONValue(b *[]byte, a Activity) (notEmpty bool) { func JSONWriteActivityJSONValue(b *[]byte, a Activity) (notEmpty bool) {
OnIntransitiveActivity(a, func(i *IntransitiveActivity) error { OnIntransitiveActivity(a, func(i *IntransitiveActivity) error {
if i == nil { if i == nil {
return nil return nil
} }
notEmpty = writeIntransitiveActivityJSONValue(b, *i) || notEmpty notEmpty = JSONWriteIntransitiveActivityJSONValue(b, *i) || notEmpty
return nil return nil
}) })
if a.Object != nil { if a.Object != nil {
notEmpty = writeItemJSONProp(b, "object", a.Object) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "object", a.Object) || notEmpty
} }
return notEmpty return notEmpty
} }
func writeIntransitiveActivityJSONValue(b *[]byte, i IntransitiveActivity) (notEmpty bool) { func JSONWriteIntransitiveActivityJSONValue(b *[]byte, i IntransitiveActivity) (notEmpty bool) {
OnObject(i, func(o *Object) error { OnObject(i, func(o *Object) error {
if o == nil { if o == nil {
return nil return nil
} }
notEmpty = writeObjectJSONValue(b, *o) || notEmpty notEmpty = JSONWriteObjectJSONValue(b, *o) || notEmpty
return nil return nil
}) })
if i.Actor != nil { if i.Actor != nil {
notEmpty = writeItemJSONProp(b, "actor", i.Actor) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "actor", i.Actor) || notEmpty
} }
if i.Target != nil { if i.Target != nil {
notEmpty = writeItemJSONProp(b, "target", i.Target) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "target", i.Target) || notEmpty
} }
if i.Result != nil { if i.Result != nil {
notEmpty = writeItemJSONProp(b, "result", i.Result) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "result", i.Result) || notEmpty
} }
if i.Origin != nil { if i.Origin != nil {
notEmpty = writeItemJSONProp(b, "origin", i.Origin) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "origin", i.Origin) || notEmpty
} }
if i.Instrument != nil { if i.Instrument != nil {
notEmpty = writeItemJSONProp(b, "instrument", i.Instrument) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "instrument", i.Instrument) || notEmpty
} }
return notEmpty return notEmpty
} }
func writeQuestionJSONValue(b *[]byte, q Question) (notEmpty bool) { func JSONWriteQuestionJSONValue(b *[]byte, q Question) (notEmpty bool) {
OnIntransitiveActivity(q, func(i *IntransitiveActivity) error { OnIntransitiveActivity(q, func(i *IntransitiveActivity) error {
if i == nil { if i == nil {
return nil return nil
} }
notEmpty = writeIntransitiveActivityJSONValue(b, *i) || notEmpty notEmpty = JSONWriteIntransitiveActivityJSONValue(b, *i) || notEmpty
return nil return nil
}) })
if q.OneOf != nil { if q.OneOf != nil {
notEmpty = writeItemJSONProp(b, "oneOf", q.OneOf) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "oneOf", q.OneOf) || notEmpty
} }
if q.AnyOf != nil { if q.AnyOf != nil {
notEmpty = writeItemJSONProp(b, "anyOf", q.AnyOf) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "anyOf", q.AnyOf) || notEmpty
} }
notEmpty = writeBoolJSONProp(b, "closed", q.Closed) || notEmpty notEmpty = JSONWriteBoolJSONProp(b, "closed", q.Closed) || notEmpty
return notEmpty return notEmpty
} }
func writeLinkJSONValue(b *[]byte, l Link) (notEmpty bool) { func JSONWriteLinkJSONValue(b *[]byte, l Link) (notEmpty bool) {
if v, err := l.ID.MarshalJSON(); err == nil && len(v) > 0 { if v, err := l.ID.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(b, "id", v) || notEmpty notEmpty = JSONWriteJSONProp(b, "id", v) || notEmpty
} }
if v, err := l.Type.MarshalJSON(); err == nil && len(v) > 0 { if v, err := l.Type.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(b, "type", v) || notEmpty notEmpty = JSONWriteJSONProp(b, "type", v) || notEmpty
} }
if v, err := l.MediaType.MarshalJSON(); err == nil && len(v) > 0 { if v, err := l.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(b, "mediaType", v) || notEmpty notEmpty = JSONWriteJSONProp(b, "mediaType", v) || notEmpty
} }
if len(l.Name) > 0 { if len(l.Name) > 0 {
notEmpty = writeNaturalLanguageJSONProp(b, "name", l.Name) || notEmpty notEmpty = JSONWriteNaturalLanguageJSONProp(b, "name", l.Name) || notEmpty
} }
if v, err := l.Rel.MarshalJSON(); err == nil && len(v) > 0 { if v, err := l.Rel.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(b, "rel", v) || notEmpty notEmpty = JSONWriteJSONProp(b, "rel", v) || notEmpty
} }
if l.Height > 0 { if l.Height > 0 {
notEmpty = writeIntJSONProp(b, "height", int64(l.Height)) notEmpty = JSONWriteIntJSONProp(b, "height", int64(l.Height))
} }
if l.Width > 0 { if l.Width > 0 {
notEmpty = writeIntJSONProp(b, "width", int64(l.Width)) notEmpty = JSONWriteIntJSONProp(b, "width", int64(l.Width))
} }
if l.Preview != nil { if l.Preview != nil {
notEmpty = writeItemJSONProp(b, "rel", l.Preview) || notEmpty notEmpty = JSONWriteItemJSONProp(b, "rel", l.Preview) || notEmpty
} }
if v, err := l.Href.MarshalJSON(); err == nil && len(v) > 0 { if v, err := l.Href.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(b, "href", v) || notEmpty notEmpty = JSONWriteJSONProp(b, "href", v) || notEmpty
} }
if len(l.HrefLang) > 0 { if len(l.HrefLang) > 0 {
notEmpty = writeStringJSONProp(b, "hrefLang", string(l.HrefLang)) || notEmpty notEmpty = JSONWriteStringJSONProp(b, "hrefLang", string(l.HrefLang)) || notEmpty
} }
return notEmpty return notEmpty
} }

View file

@ -5,7 +5,7 @@ import (
"time" "time"
) )
func Test_write(t *testing.T) { func Test_JSONWrite(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
c []byte c []byte
@ -22,7 +22,7 @@ func Test_write(t *testing.T) {
} }
} }
func Test_writeActivity(t *testing.T) { func Test_JSONWriteActivity(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
a Activity a Activity
@ -36,14 +36,14 @@ func Test_writeActivity(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeActivityJSONValue(tt.args.b, tt.args.a); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteActivityJSONValue(tt.args.b, tt.args.a); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeActivityJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteActivityJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeBoolProp(t *testing.T) { func Test_JSONWriteBoolProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
n string n string
@ -58,14 +58,14 @@ func Test_writeBoolProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeBoolJSONProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteBoolJSONProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeBoolJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteBoolJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeComma(t *testing.T) { func Test_JSONWriteComma(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
} }
@ -81,7 +81,7 @@ func Test_writeComma(t *testing.T) {
} }
} }
func Test_writeDurationProp(t *testing.T) { func Test_JSONWriteDurationProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
n string n string
@ -96,14 +96,14 @@ func Test_writeDurationProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeDurationJSONProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteDurationJSONProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeDurationJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteDurationJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeFloatProp(t *testing.T) { func Test_JSONWriteFloatProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
n string n string
@ -118,14 +118,14 @@ func Test_writeFloatProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeFloatJSONProp(tt.args.b, tt.args.n, tt.args.f); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteFloatJSONProp(tt.args.b, tt.args.n, tt.args.f); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeFloatJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteFloatJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeIRIProp(t *testing.T) { func Test_JSONWriteIRIProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
n string n string
@ -140,14 +140,14 @@ func Test_writeIRIProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeIRIJSONProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteIRIJSONProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeIRIJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteIRIJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeIntProp(t *testing.T) { func Test_JSONWriteIntProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
n string n string
@ -162,14 +162,14 @@ func Test_writeIntProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeIntJSONProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteIntJSONProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeIntJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteIntJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeIntransitiveActivity(t *testing.T) { func Test_JSONWriteIntransitiveActivity(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
i IntransitiveActivity i IntransitiveActivity
@ -183,14 +183,14 @@ func Test_writeIntransitiveActivity(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeIntransitiveActivityJSONValue(tt.args.b, tt.args.i); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteIntransitiveActivityJSONValue(tt.args.b, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeIntransitiveActivityJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteIntransitiveActivityJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeItemCollection(t *testing.T) { func Test_JSONWriteItemCollection(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
col ItemCollection col ItemCollection
@ -204,14 +204,14 @@ func Test_writeItemCollection(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeItemCollectionJSONValue(tt.args.b, tt.args.col); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteItemCollectionJSONValue(tt.args.b, tt.args.col); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeItemCollectionJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteItemCollectionJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeItemCollectionProp(t *testing.T) { func Test_JSONWriteItemCollectionProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
n string n string
@ -226,14 +226,14 @@ func Test_writeItemCollectionProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeItemCollectionJSONProp(tt.args.b, tt.args.n, tt.args.col); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteItemCollectionJSONProp(tt.args.b, tt.args.n, tt.args.col); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeItemCollectionJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteItemCollectionJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeItemProp(t *testing.T) { func Test_JSONWriteItemProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
n string n string
@ -248,14 +248,14 @@ func Test_writeItemProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeItemJSONProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteItemJSONProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeItemJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteItemJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeNaturalLanguageProp(t *testing.T) { func Test_JSONWriteNaturalLanguageProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
n string n string
@ -270,14 +270,14 @@ func Test_writeNaturalLanguageProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeNaturalLanguageJSONProp(tt.args.b, tt.args.n, tt.args.nl); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteNaturalLanguageJSONProp(tt.args.b, tt.args.n, tt.args.nl); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeNaturalLanguageJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteNaturalLanguageJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeObject(t *testing.T) { func Test_JSONWriteObject(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
o Object o Object
@ -291,14 +291,14 @@ func Test_writeObject(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeObjectJSONValue(tt.args.b, tt.args.o); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteObjectJSONValue(tt.args.b, tt.args.o); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeObjectJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteObjectJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeProp(t *testing.T) { func Test_JSONWriteProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
name string name string
@ -313,14 +313,14 @@ func Test_writeProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeJSONProp(tt.args.b, tt.args.name, tt.args.val); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteJSONProp(tt.args.b, tt.args.name, tt.args.val); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writePropName(t *testing.T) { func Test_JSONWritePropName(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
s string s string
@ -334,14 +334,14 @@ func Test_writePropName(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writePropJSONName(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWritePropJSONName(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writePropJSONName() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWritePropJSONName() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeQuestion(t *testing.T) { func Test_JSONWriteQuestion(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
q Question q Question
@ -355,14 +355,14 @@ func Test_writeQuestion(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeQuestionJSONValue(tt.args.b, tt.args.q); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteQuestionJSONValue(tt.args.b, tt.args.q); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeQuestionJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteQuestionJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeS(t *testing.T) { func Test_JSONWriteS(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
s string s string
@ -379,7 +379,7 @@ func Test_writeS(t *testing.T) {
} }
} }
func Test_writeString(t *testing.T) { func Test_JSONWriteString(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
s string s string
@ -393,14 +393,14 @@ func Test_writeString(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeStringJSONValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteStringJSONValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeStringJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteStringJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeStringProp(t *testing.T) { func Test_JSONWriteStringProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
n string n string
@ -415,14 +415,14 @@ func Test_writeStringProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeStringJSONProp(tt.args.b, tt.args.n, tt.args.s); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteStringJSONProp(tt.args.b, tt.args.n, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeStringJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteStringJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeTimeProp(t *testing.T) { func Test_JSONWriteTimeProp(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
n string n string
@ -437,14 +437,14 @@ func Test_writeTimeProp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeTimeJSONProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteTimeJSONProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeTimeJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteTimeJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }
} }
func Test_writeValue(t *testing.T) { func Test_JSONWriteValue(t *testing.T) {
type args struct { type args struct {
b *[]byte b *[]byte
s []byte s []byte
@ -458,8 +458,8 @@ func Test_writeValue(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeJSONValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty { if gotNotEmpty := JSONWriteJSONValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) t.Errorf("JSONWriteJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
} }
}) })
} }

View file

@ -197,12 +197,12 @@ func (i *IntransitiveActivity) UnmarshalJSON(data []byte) error {
// MarshalJSON encodes the receiver object to a JSON document. // MarshalJSON encodes the receiver object to a JSON document.
func (i IntransitiveActivity) MarshalJSON() ([]byte, error) { func (i IntransitiveActivity) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
write(&b, '{') JSONWrite(&b, '{')
if !writeIntransitiveActivityJSONValue(&b, i) { if !JSONWriteIntransitiveActivityJSONValue(&b, i) {
return nil, nil return nil, nil
} }
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }

22
iri.go
View file

@ -73,9 +73,9 @@ func (i IRI) MarshalJSON() ([]byte, error) {
return nil, nil return nil, nil
} }
b := make([]byte, 0) b := make([]byte, 0)
write(&b, '"') JSONWrite(&b, '"')
writeS(&b, i.String()) JSONWriteS(&b, i.String())
write(&b, '"') JSONWrite(&b, '"')
return b, nil return b, nil
} }
@ -175,19 +175,19 @@ func (i IRIs) MarshalJSON() ([]byte, error) {
if len(i) == 0 { if len(i) == 0 {
return nil, nil return nil, nil
} }
writeCommaIfNotEmpty := func(notEmpty bool) { JSONWriteCommaIfNotEmpty := func(notEmpty bool) {
if notEmpty { if notEmpty {
writeS(&b, ",") JSONWriteS(&b, ",")
} }
} }
write(&b, '[') JSONWrite(&b, '[')
for k, iri := range i { for k, iri := range i {
writeCommaIfNotEmpty(k > 0) JSONWriteCommaIfNotEmpty(k > 0)
write(&b, '"') JSONWrite(&b, '"')
writeS(&b, iri.String()) JSONWriteS(&b, iri.String())
write(&b, '"') JSONWrite(&b, '"')
} }
write(&b, ']') JSONWrite(&b, ']')
return b, nil return b, nil
} }

View file

@ -39,7 +39,7 @@ func (i ItemCollection) MarshalJSON() ([]byte, error) {
return nil, nil return nil, nil
} }
b := make([]byte, 0) b := make([]byte, 0)
writeItemCollectionJSONValue(&b, i) JSONWriteItemCollectionJSONValue(&b, i)
return b, nil return b, nil
} }

View file

@ -102,10 +102,10 @@ func (l Link) GetType() ActivityVocabularyType {
// MarshalJSON encodes the receiver object to a JSON document. // MarshalJSON encodes the receiver object to a JSON document.
func (l Link) MarshalJSON() ([]byte, error) { func (l Link) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
write(&b, '{') JSONWrite(&b, '{')
if writeLinkJSONValue(&b, l) { if JSONWriteLinkJSONValue(&b, l) {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil

View file

@ -109,7 +109,7 @@ func (a ActivityVocabularyType) MarshalJSON() ([]byte, error) {
return nil, nil return nil, nil
} }
b := make([]byte, 0) b := make([]byte, 0)
writeStringJSONValue(&b, string(a)) JSONWriteStringJSONValue(&b, string(a))
return b, nil return b, nil
} }
@ -295,10 +295,10 @@ func (o *Object) UnmarshalJSON(data []byte) error {
// MarshalJSON encodes the receiver object to a JSON document. // MarshalJSON encodes the receiver object to a JSON document.
func (o Object) MarshalJSON() ([]byte, error) { func (o Object) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
write(&b, '{') JSONWrite(&b, '{')
if writeObjectJSONValue(&b, o) { if JSONWriteObjectJSONValue(&b, o) {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil
@ -564,7 +564,7 @@ func (m MimeType) MarshalJSON() ([]byte, error) {
return nil, nil return nil, nil
} }
b := make([]byte, 0) b := make([]byte, 0)
writeStringJSONValue(&b, string(m)) JSONWriteStringJSONValue(&b, string(m))
return b, nil return b, nil
} }
@ -728,17 +728,17 @@ func (s *Source) UnmarshalJSON(data []byte) error {
func (s Source) MarshalJSON() ([]byte, error) { func (s Source) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
empty := true empty := true
write(&b, '{') JSONWrite(&b, '{')
if len(s.MediaType) > 0 { if len(s.MediaType) > 0 {
if v, err := s.MediaType.MarshalJSON(); err == nil && len(v) > 0 { if v, err := s.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
empty = !writeJSONProp(&b, "mediaType", v) empty = !JSONWriteJSONProp(&b, "mediaType", v)
} }
} }
if len(s.Content) > 0 { if len(s.Content) > 0 {
empty = !writeNaturalLanguageJSONProp(&b, "content", s.Content) empty = !JSONWriteNaturalLanguageJSONProp(&b, "content", s.Content)
} }
if !empty { if !empty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil

View file

@ -228,27 +228,27 @@ func (o *OrderedCollection) UnmarshalJSON(data []byte) error {
func (o OrderedCollection) MarshalJSON() ([]byte, error) { func (o OrderedCollection) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := false notEmpty := false
write(&b, '{') JSONWrite(&b, '{')
OnObject(o, func(o *Object) error { OnObject(o, func(o *Object) error {
notEmpty = writeObjectJSONValue(&b, *o) notEmpty = JSONWriteObjectJSONValue(&b, *o)
return nil return nil
}) })
if o.Current != nil { if o.Current != nil {
notEmpty = writeItemJSONProp(&b, "current", o.Current) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "current", o.Current) || notEmpty
} }
if o.First != nil { if o.First != nil {
notEmpty = writeItemJSONProp(&b, "first", o.First) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "first", o.First) || notEmpty
} }
if o.Last != nil { if o.Last != nil {
notEmpty = writeItemJSONProp(&b, "last", o.Last) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "last", o.Last) || notEmpty
} }
notEmpty = writeIntJSONProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty notEmpty = JSONWriteIntJSONProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty
if o.OrderedItems != nil { if o.OrderedItems != nil {
notEmpty = writeItemCollectionJSONProp(&b, "orderedItems", o.OrderedItems) || notEmpty notEmpty = JSONWriteItemCollectionJSONProp(&b, "orderedItems", o.OrderedItems) || notEmpty
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil

View file

@ -205,36 +205,36 @@ func (o *OrderedCollectionPage) UnmarshalJSON(data []byte) error {
func (o OrderedCollectionPage) MarshalJSON() ([]byte, error) { func (o OrderedCollectionPage) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := false notEmpty := false
write(&b, '{') JSONWrite(&b, '{')
OnObject(o, func(o *Object) error { OnObject(o, func(o *Object) error {
notEmpty = writeObjectJSONValue(&b, *o) notEmpty = JSONWriteObjectJSONValue(&b, *o)
return nil return nil
}) })
if o.PartOf != nil { if o.PartOf != nil {
notEmpty = writeItemJSONProp(&b, "partOf", o.PartOf) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "partOf", o.PartOf) || notEmpty
} }
if o.Current != nil { if o.Current != nil {
notEmpty = writeItemJSONProp(&b, "current", o.Current) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "current", o.Current) || notEmpty
} }
if o.First != nil { if o.First != nil {
notEmpty = writeItemJSONProp(&b, "first", o.First) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "first", o.First) || notEmpty
} }
if o.Last != nil { if o.Last != nil {
notEmpty = writeItemJSONProp(&b, "last", o.Last) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "last", o.Last) || notEmpty
} }
if o.Next != nil { if o.Next != nil {
notEmpty = writeItemJSONProp(&b, "next", o.Next) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "next", o.Next) || notEmpty
} }
if o.Prev != nil { if o.Prev != nil {
notEmpty = writeItemJSONProp(&b, "prev", o.Prev) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "prev", o.Prev) || notEmpty
} }
notEmpty = writeIntJSONProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty notEmpty = JSONWriteIntJSONProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty
if o.OrderedItems != nil { if o.OrderedItems != nil {
notEmpty = writeItemCollectionJSONProp(&b, "orderedItems", o.OrderedItems) || notEmpty notEmpty = JSONWriteItemCollectionJSONProp(&b, "orderedItems", o.OrderedItems) || notEmpty
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil

View file

@ -170,32 +170,32 @@ func (p *Place) UnmarshalJSON(data []byte) error {
func (p Place) MarshalJSON() ([]byte, error) { func (p Place) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := false notEmpty := false
write(&b, '{') JSONWrite(&b, '{')
OnObject(p, func(o *Object) error { OnObject(p, func(o *Object) error {
notEmpty = writeObjectJSONValue(&b, *o) notEmpty = JSONWriteObjectJSONValue(&b, *o)
return nil return nil
}) })
if p.Accuracy > 0 { if p.Accuracy > 0 {
notEmpty = writeFloatJSONProp(&b, "accuracy", p.Accuracy) || notEmpty notEmpty = JSONWriteFloatJSONProp(&b, "accuracy", p.Accuracy) || notEmpty
} }
if p.Altitude > 0 { if p.Altitude > 0 {
notEmpty = writeFloatJSONProp(&b, "altitude", p.Altitude) || notEmpty notEmpty = JSONWriteFloatJSONProp(&b, "altitude", p.Altitude) || notEmpty
} }
if p.Latitude > 0 { if p.Latitude > 0 {
notEmpty = writeFloatJSONProp(&b, "latitude", p.Latitude) || notEmpty notEmpty = JSONWriteFloatJSONProp(&b, "latitude", p.Latitude) || notEmpty
} }
if p.Longitude > 0 { if p.Longitude > 0 {
notEmpty = writeFloatJSONProp(&b, "longitude", p.Longitude) || notEmpty notEmpty = JSONWriteFloatJSONProp(&b, "longitude", p.Longitude) || notEmpty
} }
if p.Radius > 0 { if p.Radius > 0 {
notEmpty = writeIntJSONProp(&b, "radius", p.Radius) || notEmpty notEmpty = JSONWriteIntJSONProp(&b, "radius", p.Radius) || notEmpty
} }
if len(p.Units) > 0 { if len(p.Units) > 0 {
notEmpty = writeStringJSONProp(&b, "radius", p.Units) || notEmpty notEmpty = JSONWriteStringJSONProp(&b, "radius", p.Units) || notEmpty
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil

View file

@ -156,18 +156,18 @@ func (p *Profile) UnmarshalJSON(data []byte) error {
func (p Profile) MarshalJSON() ([]byte, error) { func (p Profile) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := false notEmpty := false
write(&b, '{') JSONWrite(&b, '{')
OnObject(p, func(o *Object) error { OnObject(p, func(o *Object) error {
return nil return nil
}) })
if p.Describes != nil { if p.Describes != nil {
notEmpty = writeItemJSONProp(&b, "describes", p.Describes) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "describes", p.Describes) || notEmpty
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil

View file

@ -180,12 +180,12 @@ func (q *Question) UnmarshalJSON(data []byte) error {
// MarshalJSON encodes the receiver object to a JSON document. // MarshalJSON encodes the receiver object to a JSON document.
func (q Question) MarshalJSON() ([]byte, error) { func (q Question) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
write(&b, '{') JSONWrite(&b, '{')
if !writeQuestionJSONValue(&b, q) { if !JSONWriteQuestionJSONValue(&b, q) {
return nil, nil return nil, nil
} }
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }

View file

@ -166,25 +166,25 @@ func (r *Relationship) UnmarshalJSON(data []byte) error {
func (r Relationship) MarshalJSON() ([]byte, error) { func (r Relationship) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := false notEmpty := false
write(&b, '{') JSONWrite(&b, '{')
OnObject(r, func(o *Object) error { OnObject(r, func(o *Object) error {
notEmpty = writeObjectJSONValue(&b, *o) notEmpty = JSONWriteObjectJSONValue(&b, *o)
return nil return nil
}) })
if r.Subject != nil { if r.Subject != nil {
notEmpty = writeItemJSONProp(&b, "subject", r.Subject) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "subject", r.Subject) || notEmpty
} }
if r.Object != nil { if r.Object != nil {
notEmpty = writeItemJSONProp(&b, "object", r.Object) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "object", r.Object) || notEmpty
} }
if r.Relationship != nil { if r.Relationship != nil {
notEmpty = writeItemJSONProp(&b, "relationship", r.Relationship) || notEmpty notEmpty = JSONWriteItemJSONProp(&b, "relationship", r.Relationship) || notEmpty
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil

View file

@ -158,22 +158,22 @@ func (t *Tombstone) UnmarshalJSON(data []byte) error {
func (t Tombstone) MarshalJSON() ([]byte, error) { func (t Tombstone) MarshalJSON() ([]byte, error) {
b := make([]byte, 0) b := make([]byte, 0)
notEmpty := false notEmpty := false
write(&b, '{') JSONWrite(&b, '{')
OnObject(t, func(o *Object) error { OnObject(t, func(o *Object) error {
notEmpty = writeObjectJSONValue(&b, *o) notEmpty = JSONWriteObjectJSONValue(&b, *o)
return nil return nil
}) })
if len(t.FormerType) > 0 { if len(t.FormerType) > 0 {
if v, err := t.FormerType.MarshalJSON(); err == nil && len(v) > 0 { if v, err := t.FormerType.MarshalJSON(); err == nil && len(v) > 0 {
notEmpty = writeJSONProp(&b, "formerType", v) || notEmpty notEmpty = JSONWriteJSONProp(&b, "formerType", v) || notEmpty
} }
} }
if !t.Deleted.IsZero() { if !t.Deleted.IsZero() {
notEmpty = writeTimeJSONProp(&b, "deleted", t.Deleted) || notEmpty notEmpty = JSONWriteTimeJSONProp(&b, "deleted", t.Deleted) || notEmpty
} }
if notEmpty { if notEmpty {
write(&b, '}') JSONWrite(&b, '}')
return b, nil return b, nil
} }
return nil, nil return nil, nil