Refactored a couple of the writeX encoding functions

This commit is contained in:
Marius Orcsik 2019-12-21 10:44:32 +01:00
parent dafd82896b
commit 6fe2b97669
No known key found for this signature in database
GPG key ID: 77618B618F79EB72
14 changed files with 38 additions and 38 deletions

View file

@ -799,7 +799,7 @@ func (a Activity) MarshalJSON() ([]byte, error) {
b := make([]byte, 0)
write(&b, '{')
if !writeActivity(&b, a) {
if !writeActivityValue(&b, a) {
return nil, nil
}
write(&b, '}')
@ -811,7 +811,7 @@ func (i IntransitiveActivity) MarshalJSON() ([]byte, error) {
b := make([]byte, 0)
write(&b, '{')
if !writeIntransitiveActivity(&b, i) {
if !writeIntransitiveActivityValue(&b, i) {
return nil, nil
}
write(&b, '}')

View file

@ -392,7 +392,7 @@ func (a Actor) MarshalJSON() ([]byte, error) {
write(&b, '{')
OnObject(a, func(o *Object) error {
notEmpty = writeObject(&b, *o)
notEmpty = writeObjectValue(&b, *o)
return nil
})
if a.Inbox != nil {
@ -422,7 +422,7 @@ func (a Actor) MarshalJSON() ([]byte, error) {
writePropName(&b, "streams")
lNotEmpty := true
for _, ss := range a.Streams {
lNotEmpty = writeItemCollection(&b, ss) || lNotEmpty
lNotEmpty = writeItemCollectionValue(&b, ss) || lNotEmpty
}
notEmpty = lNotEmpty || notEmpty
}

View file

@ -302,7 +302,7 @@ func (c Collection) MarshalJSON() ([]byte, error) {
write(&b, '{')
OnObject(c, func(o *Object) error {
notEmpty = writeObject(&b, *o)
notEmpty = writeObjectValue(&b, *o)
return nil
})
if c.Current != nil {

View file

@ -261,7 +261,7 @@ func (c CollectionPage) MarshalJSON() ([]byte, error) {
write(&b, '{')
OnObject(c, func(o *Object) error {
notEmpty = writeObject(&b, *o)
notEmpty = writeObjectValue(&b, *o)
return nil
})
if c.Current != nil {

View file

@ -108,7 +108,7 @@ func writeItemProp(b *[]byte, n string, i Item) (notEmpty bool) {
return notEmpty
}
func writeString(b *[]byte, s string) (notEmpty bool) {
func writeStringValue(b *[]byte, s string) (notEmpty bool) {
if len(s) == 0 {
return false
}
@ -118,7 +118,7 @@ func writeString(b *[]byte, s string) (notEmpty bool) {
return true
}
func writeItemCollection(b *[]byte, col ItemCollection) (notEmpty bool) {
func writeItemCollectionValue(b *[]byte, col ItemCollection) (notEmpty bool) {
if len(col) == 0 {
return notEmpty
}
@ -146,14 +146,14 @@ func writeItemCollectionProp(b *[]byte, n string, col ItemCollection) (notEmpty
return notEmpty
}
writeComma(b)
success := writePropName(b, n) && writeItemCollection(b, col)
success := writePropName(b, n) && writeItemCollectionValue(b, col)
if !success {
*b = (*b)[:len(*b)-1]
}
return success
}
func writeObject(b *[]byte, o Object) (notEmpty bool) {
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
}
@ -252,12 +252,12 @@ func writeObject(b *[]byte, o Object) (notEmpty bool) {
return notEmpty
}
func writeActivity(b *[]byte, a Activity) (notEmpty bool) {
func writeActivityValue(b *[]byte, a Activity) (notEmpty bool) {
OnIntransitiveActivity(a, func(i *IntransitiveActivity) error {
if i == nil {
return nil
}
notEmpty = writeIntransitiveActivity(b, *i) || notEmpty
notEmpty = writeIntransitiveActivityValue(b, *i) || notEmpty
return nil
})
if a.Object != nil {
@ -266,12 +266,12 @@ func writeActivity(b *[]byte, a Activity) (notEmpty bool) {
return notEmpty
}
func writeIntransitiveActivity(b *[]byte, i IntransitiveActivity) (notEmpty bool) {
func writeIntransitiveActivityValue(b *[]byte, i IntransitiveActivity) (notEmpty bool) {
OnObject(i, func(o *Object) error {
if o == nil {
return nil
}
notEmpty = writeObject(b, *o) || notEmpty
notEmpty = writeObjectValue(b, *o) || notEmpty
return nil
})
if i.Actor != nil {
@ -292,12 +292,12 @@ func writeIntransitiveActivity(b *[]byte, i IntransitiveActivity) (notEmpty bool
return notEmpty
}
func writeQuestion(b *[]byte, q Question) (notEmpty bool) {
func writeQuestionValue(b *[]byte, q Question) (notEmpty bool) {
OnIntransitiveActivity(q, func(i *IntransitiveActivity) error {
if i == nil {
return nil
}
notEmpty = writeIntransitiveActivity(b, *i) || notEmpty
notEmpty = writeIntransitiveActivityValue(b, *i) || notEmpty
return nil
})
if q.OneOf != nil {

View file

@ -36,8 +36,8 @@ func Test_writeActivity(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotNotEmpty := writeActivity(tt.args.b, tt.args.a); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeActivity() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
if gotNotEmpty := writeActivityValue(tt.args.b, tt.args.a); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeActivityValue() = %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 := writeIntransitiveActivity(tt.args.b, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeIntransitiveActivity() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
if gotNotEmpty := writeIntransitiveActivityValue(tt.args.b, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeIntransitiveActivityValue() = %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 := writeItemCollection(tt.args.b, tt.args.col); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeItemCollection() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
if gotNotEmpty := writeItemCollectionValue(tt.args.b, tt.args.col); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeItemCollectionValue() = %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 := writeObject(tt.args.b, tt.args.o); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeObject() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
if gotNotEmpty := writeObjectValue(tt.args.b, tt.args.o); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeObjectValue() = %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 := writeQuestion(tt.args.b, tt.args.q); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeQuestion() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
if gotNotEmpty := writeQuestionValue(tt.args.b, tt.args.q); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeQuestionValue() = %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 := writeString(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeString() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
if gotNotEmpty := writeStringValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
t.Errorf("writeStringValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
}
})
}

View file

@ -35,7 +35,7 @@ func (i ItemCollection) IsObject() bool {
func (i ItemCollection) MarshalJSON() ([]byte, error) {
b := make([]byte, 0)
writeItemCollection(&b, i)
writeItemCollectionValue(&b, i)
return b, nil
}

View file

@ -111,7 +111,7 @@ func (a ActivityVocabularyType) MarshalJSON() ([]byte, error) {
return nil, nil
}
b := make([]byte, 0)
writeString(&b, string(a))
writeStringValue(&b, string(a))
return b, nil
}
@ -323,7 +323,7 @@ func (o Object) MarshalJSON() ([]byte, error) {
notEmpty := false
write(&b, '{')
notEmpty = writeObject(&b, o)
notEmpty = writeObjectValue(&b, o)
if notEmpty {
write(&b, '}')
@ -376,7 +376,7 @@ func (m MimeType) MarshalJSON() ([]byte, error) {
return nil, nil
}
b := make([]byte, 0)
writeString(&b, string(m))
writeStringValue(&b, string(m))
return b, nil
}

View file

@ -298,7 +298,7 @@ func (o OrderedCollection) MarshalJSON() ([]byte, error) {
write(&b, '{')
OnObject(o, func(o *Object) error {
notEmpty = writeObject(&b, *o)
notEmpty = writeObjectValue(&b, *o)
return nil
})
if o.Current != nil {

View file

@ -267,7 +267,7 @@ func (c OrderedCollectionPage) MarshalJSON() ([]byte, error) {
write(&b, '{')
OnObject(c, func(o *Object) error {
notEmpty = writeObject(&b, *o)
notEmpty = writeObjectValue(&b, *o)
return nil
})
if c.Current != nil {

View file

@ -225,7 +225,7 @@ func (p Place) MarshalJSON() ([]byte, error) {
write(&b, '{')
OnObject(p, func(o *Object) error {
notEmpty = writeObject(&b, *o)
notEmpty = writeObjectValue(&b, *o)
return nil
})
if p.Accuracy > 0 {

View file

@ -233,7 +233,7 @@ func (q Question) MarshalJSON() ([]byte, error) {
b := make([]byte, 0)
write(&b, '{')
if !writeQuestion(&b, q) {
if !writeQuestionValue(&b, q) {
return nil, nil
}
write(&b, '}')

View file

@ -218,7 +218,7 @@ func (r Relationship) MarshalJSON() ([]byte, error) {
write(&b, '{')
OnObject(r, func(o *Object) error {
notEmpty = writeObject(&b, *o)
notEmpty = writeObjectValue(&b, *o)
return nil
})

View file

@ -209,7 +209,7 @@ func (t Tombstone) MarshalJSON() ([]byte, error) {
write(&b, '{')
OnObject(t, func(o *Object) error {
notEmpty = writeObject(&b, *o)
notEmpty = writeObjectValue(&b, *o)
return nil
})
if len(t.FormerType) > 0 {