From 11de44e7dcef26fa6d04330450300eeaebd5e9bf Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 8 Aug 2024 11:22:53 -0700 Subject: [PATCH] mstdn/ent.CustomEmoji --- ent/customemoji.go | 84 ++--------------------------- ent/customemoji_marshaljson_test.go | 24 ++++++++- 2 files changed, 27 insertions(+), 81 deletions(-) diff --git a/ent/customemoji.go b/ent/customemoji.go index 9e31512..fb86d84 100644 --- a/ent/customemoji.go +++ b/ent/customemoji.go @@ -1,91 +1,17 @@ package ent import ( - "encoding/json" - - "github.com/reiver/go-erorr" "github.com/reiver/go-opt" ) -var _ json.Marshaler = CustomEmoji{} - // CustomEmoji represents a Mastodon API "CustomEmoji". // // See: // https://docs.joinmastodon.org/entities/CustomEmoji/ type CustomEmoji struct { - ShortCode opt.Optional[string] `json:"shortcode"` - URL opt.Optional[string] `json:"url"` - StaticURL opt.Optional[string] `json:"static_url"` - VisibleInPicker opt.Optional[bool] `json:"visible_in_picker"` - Category opt.Optional[string] `json:"category"` -} - -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 + ShortCode opt.Optional[string] `json:"shortcode,omitempty"` + URL opt.Optional[string] `json:"url,omitempty"` + StaticURL opt.Optional[string] `json:"static_url,omitempty"` + VisibleInPicker opt.Optional[bool] `json:"visible_in_picker,omitempty"` + Category opt.Optional[string] `json:"category,omitempty"` } diff --git a/ent/customemoji_marshaljson_test.go b/ent/customemoji_marshaljson_test.go index 2a40a2b..35268df 100644 --- a/ent/customemoji_marshaljson_test.go +++ b/ent/customemoji_marshaljson_test.go @@ -4,8 +4,8 @@ import ( "testing" "bytes" - "encoding/json" + "github.com/reiver/go-json" "github.com/reiver/go-opt" ) @@ -93,11 +93,31 @@ func TestCustomEmoji_MarshalJSON(t *testing.T) { "\t"+ `"visible_in_picker":` +`true` +"" +"\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 { - actualBytes, err := test.Value.MarshalJSON() + actualBytes, err := json.Marshal(test.Value) if nil != err { t.Errorf("For test #%d, did not expect to get an error but actually got one.", testNumber) t.Logf("ERROR: (%T) %s", err, err)