Compare commits

...

2 Commits

Author SHA1 Message Date
Charles Iliya Krempeaux 7dabefe14b initial commits 2023-09-26 13:41:23 +09:00
Charles Iliya Krempeaux 16a8523f6f initial commits 2023-09-26 13:39:21 +09:00
2 changed files with 36 additions and 9 deletions

View File

@ -10,10 +10,10 @@ import (
var _ json.Marshaler = CustomEmoji{} var _ json.Marshaler = CustomEmoji{}
const ( const (
errCannotMashalCustomEmojiAsJSONNoShortCode = erorr.Error("cannot marshal mstdn.Emoji to JSON — no shortcode set") errCannotMashalCustomEmojiAsJSONNoShortCode = erorr.Error("cannot marshal mstdn.CustomEmoji to JSON — no shortcode set")
errCannotMashalCustomEmojiAsJSONNoURL = erorr.Error("cannot marshal mstdn.Emoji to JSON — no url set") errCannotMashalCustomEmojiAsJSONNoURL = erorr.Error("cannot marshal mstdn.CustomEmoji to JSON — no url set")
errCannotMashalCustomEmojiAsJSONNoStaticURL = erorr.Error("cannot marshal mstdn.Emoji to JSON — no static_url set") errCannotMashalCustomEmojiAsJSONNoStaticURL = erorr.Error("cannot marshal mstdn.CustomEmoji to JSON — no static_url set")
errCannotMashalCustomEmojiAsJSONNoVisibleInPicker = erorr.Error("cannot marshal mstdn.Emoji to JSON — no visible_in_picker set") errCannotMashalCustomEmojiAsJSONNoVisibleInPicker = erorr.Error("cannot marshal mstdn.CustomEmoji to JSON — no visible_in_picker set")
) )
// CustomEmoji represents a Mastodon API "CustomEmoji". // CustomEmoji represents a Mastodon API "CustomEmoji".

View File

@ -3,6 +3,7 @@ package mstdn
import ( import (
"encoding/json" "encoding/json"
"sourcecode.social/reiver/go-erorr"
"sourcecode.social/reiver/go-opt" "sourcecode.social/reiver/go-opt"
"sourcecode.social/reiver/go-nul" "sourcecode.social/reiver/go-nul"
) )
@ -10,6 +11,11 @@ import (
var _ json.Marshaler = Field{} var _ json.Marshaler = Field{}
var _ json.Unmarshaler = &Field{} var _ json.Unmarshaler = &Field{}
const (
errCannotMashalFieldAsJSONNoName = erorr.Error("cannot marshal mstdn.Field to JSON — no name set")
errCannotMashalFieldAsJSONNoValue = erorr.Error("cannot marshal mstdn.Field to JSON — no value set")
)
// Field represents a Mastodon API "Field". // Field represents a Mastodon API "Field".
// //
// See: // See:
@ -37,13 +43,34 @@ func FieldVerifiedNameValue(when string, name string, value string) Field {
func (receiver Field) MarshalJSON() ([]byte, error) { func (receiver Field) MarshalJSON() ([]byte, error) {
duplicate := receiver var data = map[string]interface{}{}
duplicate.VerifiedAt.WhenNothing(func(){ {
duplicate.VerifiedAt = nul.Null[string]() val, found := receiver.Name.Get()
}) if !found {
return nil, errCannotMashalFieldAsJSONNoName
}
return json.Marshal(duplicate) data["name"] = val
}
{
val, found := receiver.Value.Get()
if !found {
return nil, errCannotMashalFieldAsJSONNoValue
}
data["value"] = val
}
{
val, found := receiver.VerifiedAt.Get()
if !found {
data["verified_at"] = nil
} else {
data["verified_at"] = val
}
}
return json.Marshal(data)
} }
func (receiver *Field) UnmarshalJSON(data []byte) error { func (receiver *Field) UnmarshalJSON(data []byte) error {