From 4e25b5277e30256a138ba0e916cde5e3cf25cbf8 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 8 Aug 2024 12:46:06 -0700 Subject: [PATCH] mstdn/ent.Role --- ent/role.go | 67 +++------------------------------- ent/role_unmarshaljson_test.go | 1 + 2 files changed, 6 insertions(+), 62 deletions(-) diff --git a/ent/role.go b/ent/role.go index 952fa12..7ac7f27 100644 --- a/ent/role.go +++ b/ent/role.go @@ -1,75 +1,18 @@ package ent import ( - "encoding/json" - "github.com/reiver/go-opt" "github.com/reiver/go-jsonint" ) -var _ json.Marshaler = Role{} -var _ json.Unmarshaler = new(Role) - // Role represents a Mastodon API "Role". // // See: // https://docs.joinmastodon.org/entities/Role/ type Role struct { - ID opt.Optional[jsonint.Int] - Name opt.Optional[string] - Color opt.Optional[string] - Permissions opt.Optional[jsonint.Int] - Highlighted opt.Optional[bool] -} - -type role struct { - ID opt.Optional[jsonint.Int] `json:"id"` - Name opt.Optional[string] `json:"name"` - Color opt.Optional[string] `json:"color"` - Permissions opt.Optional[jsonint.Int] `json:"permissions"` - Highlighted opt.Optional[bool] `json:"highlighted"` -} - -func (receiver Role) MarshalJSON() ([]byte, error) { - - var src role - - src.ID = receiver.ID - src.Name = receiver.Name - src.Permissions = receiver.Permissions - src.Highlighted = receiver.Highlighted - - src.Color = receiver.Color - src.Color.WhenNothing(func(){ - src.Color = opt.Something("") - }) - - return json.Marshal(src) -} - -func (receiver *Role) UnmarshalJSON(data []byte) error { - if nil == receiver { - return errNilReceiver - } - - var dst role - - if err := json.Unmarshal(data, &dst); nil != err { - return err - } - - receiver.ID = dst.ID - receiver.Name = dst.Name - receiver.Permissions = dst.Permissions - receiver.Highlighted = dst.Highlighted - - dst.Color.WhenSomething(func(value string){ - if "" == value { - return - } - - receiver.Color = opt.Something(value) - }) - - return nil + ID opt.Optional[jsonint.Int] `json:"id,omitempty"` + Name opt.Optional[string] `json:"name,omitempty"` + Color opt.Optional[string] `json:"color,omitempty"` + Permissions opt.Optional[jsonint.Int] `json:"permissions,omitempty"` + Highlighted opt.Optional[bool] `json:"highlighted,omitempty"` } diff --git a/ent/role_unmarshaljson_test.go b/ent/role_unmarshaljson_test.go index 59433a9..bbc3123 100644 --- a/ent/role_unmarshaljson_test.go +++ b/ent/role_unmarshaljson_test.go @@ -66,6 +66,7 @@ func TestRole_MarshalJSON(t *testing.T) { Expected: ent.Role{ ID: opt.Something(jsonint.Int64(87)), Name: opt.Something("QA Specialist"), + Color: opt.Something(""), Permissions: opt.Something(jsonint.Int64(218)), Highlighted: opt.Something(true), },