mstdn/ent.CustomEmoji

master
Charles Iliya Krempeaux 2024-08-08 11:22:53 -07:00
parent 422f33eb2c
commit 11de44e7dc
2 changed files with 27 additions and 81 deletions

View File

@ -1,91 +1,17 @@
package ent package ent
import ( import (
"encoding/json"
"github.com/reiver/go-erorr"
"github.com/reiver/go-opt" "github.com/reiver/go-opt"
) )
var _ json.Marshaler = CustomEmoji{}
// CustomEmoji represents a Mastodon API "CustomEmoji". // CustomEmoji represents a Mastodon API "CustomEmoji".
// //
// See: // See:
// https://docs.joinmastodon.org/entities/CustomEmoji/ // https://docs.joinmastodon.org/entities/CustomEmoji/
type CustomEmoji struct { type CustomEmoji struct {
ShortCode opt.Optional[string] `json:"shortcode"` ShortCode opt.Optional[string] `json:"shortcode,omitempty"`
URL opt.Optional[string] `json:"url"` URL opt.Optional[string] `json:"url,omitempty"`
StaticURL opt.Optional[string] `json:"static_url"` StaticURL opt.Optional[string] `json:"static_url,omitempty"`
VisibleInPicker opt.Optional[bool] `json:"visible_in_picker"` VisibleInPicker opt.Optional[bool] `json:"visible_in_picker,omitempty"`
Category opt.Optional[string] `json:"category"` Category opt.Optional[string] `json:"category,omitempty"`
}
func (receiver CustomEmoji) MarshalJSON() ([]byte, error) {
var buffer []byte
buffer = append(buffer, "{"...)
{
buffer = append(buffer, `"shortcode":`...)
marshaled, err := json.Marshal(receiver.ShortCode)
if nil != err {
return nil, erorr.Errorf("mstdn/ent: could not marshal ent.CustomEmoji.ShortCode as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
{
buffer = append(buffer, `,"url":`...)
marshaled, err := json.Marshal(receiver.URL)
if nil != err {
return nil, erorr.Errorf("mstdn/ent: could not marshal ent.CustomEmoji.URL as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
{
buffer = append(buffer, `,"static_url":`...)
marshaled, err := json.Marshal(receiver.StaticURL)
if nil != err {
return nil, erorr.Errorf("mstdn/ent: could not marshal ent.CustomEmoji.StaticURL as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
{
buffer = append(buffer, `,"visible_in_picker":`...)
marshaled, err := json.Marshal(receiver.VisibleInPicker)
if nil != err {
return nil, erorr.Errorf("mstdn/ent: could not marshal ent.CustomEmoji.VisibleInPicker as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
{
if opt.Nothing[string]() != receiver.Category {
buffer = append(buffer, `,"category":`...)
marshaled, err := json.Marshal(receiver.Category)
if nil != err {
return nil, erorr.Errorf("mstdn/ent: could not marshal ent.CustomEmoji.Category as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
}
buffer = append(buffer, "}"...)
return buffer, nil
} }

View File

@ -4,8 +4,8 @@ import (
"testing" "testing"
"bytes" "bytes"
"encoding/json"
"github.com/reiver/go-json"
"github.com/reiver/go-opt" "github.com/reiver/go-opt"
) )
@ -93,11 +93,31 @@ func TestCustomEmoji_MarshalJSON(t *testing.T) {
"\t"+ `"visible_in_picker":` +`true` +"" +"\n"+ "\t"+ `"visible_in_picker":` +`true` +"" +"\n"+
"}" +"\n", "}" +"\n",
}, },
{
Value: CustomEmoji{
ShortCode: opt.Something[string](""),
URL: opt.Something[string](""),
StaticURL: opt.Something[string](""),
VisibleInPicker: opt.Something[bool](false),
Category: opt.Something[string]("super-heroes"),
},
Expected:
"{" +"\n"+
"\t"+ `"shortcode":` +`""` +"," +"\n"+
"\t"+ `"url":` +`""` +"," +"\n"+
"\t"+ `"static_url":` +`""` +"," +"\n"+
"\t"+ `"visible_in_picker":` +`false` +"," +"\n"+
"\t"+ `"category":` +`"super-heroes"` +"" +"\n"+
"}" +"\n",
},
} }
for testNumber, test := range tests { for testNumber, test := range tests {
actualBytes, err := test.Value.MarshalJSON() actualBytes, err := json.Marshal(test.Value)
if nil != err { if nil != err {
t.Errorf("For test #%d, did not expect to get an error but actually got one.", testNumber) t.Errorf("For test #%d, did not expect to get an error but actually got one.", testNumber)
t.Logf("ERROR: (%T) %s", err, err) t.Logf("ERROR: (%T) %s", err, err)