From 686fae85989385d1cbf66b4c39fd825333d075f3 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Tue, 26 Sep 2023 13:26:53 +0900 Subject: [PATCH] initial commits --- field.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 field.go diff --git a/field.go b/field.go new file mode 100644 index 0000000..bc107ad --- /dev/null +++ b/field.go @@ -0,0 +1,64 @@ +package mstdn + +import ( + "encoding/json" + + "sourcecode.social/reiver/go-opt" + "sourcecode.social/reiver/go-nul" +) + +var _ json.Marshaler = Field{} +var _ json.Unmarshaler = &Field{} + +// Field represents a Mastodon API "Field". +// +// See: +// https://docs.joinmastodon.org/entities/Field/ +type Field struct { + Name opt.Optional[string] `json:"name"` + Value opt.Optional[string] `json:"value"` + VerifiedAt nul.Nullable[string] `json:"verified_at"` +} + +func FieldNameValue(name string, value string) Field { + return Field{ + Name: opt.Something(name), + Value: opt.Something(value), + } +} + +func FieldVerifiedNameValue(when string, name string, value string) Field { + return Field{ + Name: opt.Something(name), + Value: opt.Something(value), + VerifiedAt: nul.Something(when), + } +} + +func (receiver Field) MarshalJSON() ([]byte, error) { + + duplicate := receiver + + duplicate.VerifiedAt.WhenNothing(func(){ + duplicate.VerifiedAt = nul.Null[string]() + }) + + return json.Marshal(duplicate) +} + +func (receiver *Field) UnmarshalJSON(data []byte) error { + if nil == receiver { + return errNilReceiver + } + + err := json.Unmarshal(data, receiver) + if nil != err { + return err + } + + if nul.Null[string]() == receiver.VerifiedAt { + receiver.VerifiedAt = nul.Nothing[string]() + } + + return nil +}