2023-09-26 05:29:10 +00:00
|
|
|
|
package ent
|
2023-09-26 04:26:53 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
2024-08-01 22:35:46 +00:00
|
|
|
|
"github.com/reiver/go-erorr"
|
2024-08-01 22:29:50 +00:00
|
|
|
|
"github.com/reiver/go-opt"
|
2023-09-26 04:26:53 +00:00
|
|
|
|
"sourcecode.social/reiver/go-nul"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var _ json.Marshaler = Field{}
|
|
|
|
|
var _ json.Unmarshaler = &Field{}
|
|
|
|
|
|
2023-09-26 04:41:23 +00:00
|
|
|
|
const (
|
2023-09-26 05:29:10 +00:00
|
|
|
|
errCannotMashalFieldAsJSONNoName = erorr.Error("cannot marshal ent.Field to JSON — no ‘name’ set")
|
|
|
|
|
errCannotMashalFieldAsJSONNoValue = erorr.Error("cannot marshal ent.Field to JSON — no ‘value’ set")
|
2023-09-26 04:41:23 +00:00
|
|
|
|
)
|
|
|
|
|
|
2023-09-26 04:26:53 +00:00
|
|
|
|
// Field represents a Mastodon API "Field".
|
|
|
|
|
//
|
|
|
|
|
// See:
|
|
|
|
|
// https://docs.joinmastodon.org/entities/Field/
|
|
|
|
|
type Field struct {
|
2023-09-26 04:48:44 +00:00
|
|
|
|
Name opt.Optional[string]
|
|
|
|
|
Value opt.Optional[string]
|
|
|
|
|
VerifiedAt nul.Nullable[string]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type field struct {
|
2023-09-26 04:26:53 +00:00
|
|
|
|
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) {
|
|
|
|
|
|
2023-09-26 04:41:23 +00:00
|
|
|
|
var data = map[string]interface{}{}
|
2023-09-26 04:26:53 +00:00
|
|
|
|
|
2023-09-26 04:41:23 +00:00
|
|
|
|
{
|
|
|
|
|
val, found := receiver.Name.Get()
|
|
|
|
|
if !found {
|
|
|
|
|
return nil, errCannotMashalFieldAsJSONNoName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-26 04:26:53 +00:00
|
|
|
|
|
2023-09-26 04:41:23 +00:00
|
|
|
|
return json.Marshal(data)
|
2023-09-26 04:26:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (receiver *Field) UnmarshalJSON(data []byte) error {
|
|
|
|
|
if nil == receiver {
|
|
|
|
|
return errNilReceiver
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 04:48:44 +00:00
|
|
|
|
var f field
|
|
|
|
|
|
|
|
|
|
err := json.Unmarshal(data, &f)
|
2023-09-26 04:26:53 +00:00
|
|
|
|
if nil != err {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 04:48:44 +00:00
|
|
|
|
if nul.Null[string]() == f.VerifiedAt {
|
|
|
|
|
f.VerifiedAt = nul.Nothing[string]()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*receiver = Field{
|
|
|
|
|
Name: f.Name,
|
|
|
|
|
Value: f.Value,
|
|
|
|
|
VerifiedAt: f.VerifiedAt,
|
2023-09-26 04:26:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|