mstdn/ent.Role

master
Charles Iliya Krempeaux 2024-08-08 12:46:06 -07:00
parent c70db317e0
commit 4e25b5277e
2 changed files with 6 additions and 62 deletions

View File

@ -1,75 +1,18 @@
package ent package ent
import ( import (
"encoding/json"
"github.com/reiver/go-opt" "github.com/reiver/go-opt"
"github.com/reiver/go-jsonint" "github.com/reiver/go-jsonint"
) )
var _ json.Marshaler = Role{}
var _ json.Unmarshaler = new(Role)
// Role represents a Mastodon API "Role". // Role represents a Mastodon API "Role".
// //
// See: // See:
// https://docs.joinmastodon.org/entities/Role/ // https://docs.joinmastodon.org/entities/Role/
type Role struct { type Role struct {
ID opt.Optional[jsonint.Int] ID opt.Optional[jsonint.Int] `json:"id,omitempty"`
Name opt.Optional[string] Name opt.Optional[string] `json:"name,omitempty"`
Color opt.Optional[string] Color opt.Optional[string] `json:"color,omitempty"`
Permissions opt.Optional[jsonint.Int] Permissions opt.Optional[jsonint.Int] `json:"permissions,omitempty"`
Highlighted opt.Optional[bool] Highlighted opt.Optional[bool] `json:"highlighted,omitempty"`
}
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
} }

View File

@ -66,6 +66,7 @@ func TestRole_MarshalJSON(t *testing.T) {
Expected: ent.Role{ Expected: ent.Role{
ID: opt.Something(jsonint.Int64(87)), ID: opt.Something(jsonint.Int64(87)),
Name: opt.Something("QA Specialist"), Name: opt.Something("QA Specialist"),
Color: opt.Something(""),
Permissions: opt.Something(jsonint.Int64(218)), Permissions: opt.Something(jsonint.Int64(218)),
Highlighted: opt.Something(true), Highlighted: opt.Something(true),
}, },