From c70db317e0501e6cdfc501a5cdfc2ab973bc3180 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 8 Aug 2024 12:27:55 -0700 Subject: [PATCH] mstdn/ent.Field --- ent/field.go | 78 ++------------------------------- ent/field_marshaljson_test.go | 4 +- ent/field_unmarshaljson_test.go | 4 -- 3 files changed, 5 insertions(+), 81 deletions(-) diff --git a/ent/field.go b/ent/field.go index 344bf68..241aa45 100644 --- a/ent/field.go +++ b/ent/field.go @@ -1,34 +1,18 @@ package ent import ( - "encoding/json" - "github.com/reiver/go-erorr" "github.com/reiver/go-opt" "github.com/reiver/go-nul" ) -var _ json.Marshaler = Field{} -var _ json.Unmarshaler = &Field{} - -const ( - errCannotMashalFieldAsJSONNoName = erorr.Error("cannot marshal ent.Field to JSON — no ‘name’ set") - errCannotMashalFieldAsJSONNoValue = erorr.Error("cannot marshal ent.Field to JSON — no ‘value’ set") -) - // Field represents a Mastodon API "Field". // // See: // https://docs.joinmastodon.org/entities/Field/ type Field struct { - Name opt.Optional[string] - Value opt.Optional[string] - VerifiedAt nul.Nullable[string] -} - -type field struct { - Name opt.Optional[string] `json:"name"` - Value opt.Optional[string] `json:"value"` + Name opt.Optional[string] `json:"name,omitempty"` + Value opt.Optional[string] `json:"value,omitempty"` VerifiedAt nul.Nullable[string] `json:"verified_at"` } @@ -36,6 +20,7 @@ func FieldNameValue(name string, value string) Field { return Field{ Name: opt.Something(name), Value: opt.Something(value), + VerifiedAt: nul.Null[string](), } } @@ -46,60 +31,3 @@ func FieldVerifiedNameValue(when string, name string, value string) Field { VerifiedAt: nul.Something(when), } } - -func (receiver Field) MarshalJSON() ([]byte, error) { - - var data = map[string]interface{}{} - - { - 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 - } - } - - return json.Marshal(data) -} - -func (receiver *Field) UnmarshalJSON(data []byte) error { - if nil == receiver { - return errNilReceiver - } - - var f field - - err := json.Unmarshal(data, &f) - if nil != err { - return err - } - - if nul.Null[string]() == f.VerifiedAt { - f.VerifiedAt = nul.Nothing[string]() - } - - *receiver = Field{ - Name: f.Name, - Value: f.Value, - VerifiedAt: f.VerifiedAt, - } - - return nil -} diff --git a/ent/field_marshaljson_test.go b/ent/field_marshaljson_test.go index 9ead717..b113319 100644 --- a/ent/field_marshaljson_test.go +++ b/ent/field_marshaljson_test.go @@ -4,8 +4,8 @@ import ( "testing" "bytes" - "encoding/json" + "github.com/reiver/go-json" "github.com/reiver/go-mstdn/ent" ) @@ -112,7 +112,7 @@ func TestField_MarshalJSON(t *testing.T) { 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) diff --git a/ent/field_unmarshaljson_test.go b/ent/field_unmarshaljson_test.go index dd02169..e76371c 100644 --- a/ent/field_unmarshaljson_test.go +++ b/ent/field_unmarshaljson_test.go @@ -14,10 +14,6 @@ func TestField_UnmarshalJSON(t *testing.T) { JSON string Expected ent.Field }{ - { - JSON: `{"name":"Location","value":"Metro Vancouver"}`, - Expected: ent.FieldNameValue("Location", "Metro Vancouver"), - }, { JSON: `{"name":"Location","value":"Metro Vancouver", "verified_at":null}`, Expected: ent.FieldNameValue("Location", "Metro Vancouver"),