go-mstdn/ent/role.go

76 lines
1.5 KiB
Go
Raw Normal View History

2023-09-26 05:29:10 +00:00
package ent
2023-09-26 04:51:37 +00:00
import (
2023-09-26 12:07:45 +00:00
"encoding/json"
"github.com/reiver/go-opt"
"github.com/reiver/go-jsonint"
2023-09-26 04:51:37 +00:00
)
2023-09-26 12:12:48 +00:00
var _ json.Marshaler = Role{}
2023-09-26 12:07:45 +00:00
var _ json.Unmarshaler = new(Role)
2023-09-26 04:51:37 +00:00
// Role represents a Mastodon API "Role".
//
// See:
// https://docs.joinmastodon.org/entities/Role/
type Role struct {
2023-09-27 06:34:42 +00:00
ID opt.Optional[jsonint.Int]
Name opt.Optional[string]
Color opt.Optional[string]
Permissions opt.Optional[jsonint.Int]
Highlighted opt.Optional[bool]
2023-09-26 12:07:45 +00:00
}
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"`
2023-09-27 06:34:42 +00:00
Highlighted opt.Optional[bool] `json:"highlighted"`
2023-09-26 12:07:45 +00:00
}
2023-09-26 12:12:48 +00:00
func (receiver Role) MarshalJSON() ([]byte, error) {
var src role
src.ID = receiver.ID
src.Name = receiver.Name
src.Permissions = receiver.Permissions
2023-09-27 06:34:42 +00:00
src.Highlighted = receiver.Highlighted
2023-09-26 12:12:48 +00:00
src.Color = receiver.Color
2023-09-27 06:34:42 +00:00
src.Color.WhenNothing(func(){
2023-09-26 12:12:48 +00:00
src.Color = opt.Something("")
})
return json.Marshal(src)
}
2023-09-26 12:07:45 +00:00
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
2023-09-26 04:51:37 +00:00
}