go-mstdn/ent/application.go

71 lines
1.8 KiB
Go
Raw Normal View History

2023-09-26 08:49:01 +00:00
package ent
import (
"encoding/json"
"sourcecode.social/reiver/go-erorr"
"sourcecode.social/reiver/go-opt"
"sourcecode.social/reiver/go-nul"
)
var _ json.Marshaler = Application{}
const (
errCannotMashalApplicationAsJSONNoName = erorr.Error("cannot marshal ent.Application to JSON — no name set")
errCannotMashalApplicationAsJSONNoVapidKey = erorr.Error("cannot marshal ent.Application to JSON — no vapid_key set")
)
// Appication represents a Mastodon API "Appication".
//
// See:
// https://docs.joinmastodon.org/entities/Application/
type Application struct {
Name opt.Optional[string] `json:"name"`
2023-09-27 07:33:04 +00:00
WebSite nul.Nullable[string] `json:"website"` // optional — field has JSON null value in JSON if not set
2023-09-26 08:49:01 +00:00
VapidKey opt.Optional[string] `json:"vapid_key"`
2023-09-27 07:33:04 +00:00
ClientID opt.Optional[string] `json:"client_id"` // optional — field not included in JSON if not set
ClientSecret opt.Optional[string] `json:"client_secret"` // optional — field not included in JSON if not set
2023-09-26 08:49:01 +00:00
}
func (receiver Application) MarshalJSON() ([]byte, error) {
2023-09-27 07:33:04 +00:00
var data map[string]interface{} = map[string]interface{}{}
2023-09-26 08:49:01 +00:00
{
val, found := receiver.Name.Get()
if !found {
return nil, errCannotMashalApplicationAsJSONNoName
}
data["name"] = val
}
receiver.WebSite.WhenSomething(func(value string){
data["website"] = value
})
receiver.WebSite.WhenNull(func(){
data["website"] = nil
})
2023-09-27 07:33:04 +00:00
receiver.WebSite.WhenNothing(func(){
data["website"] = nil
})
2023-09-26 08:49:01 +00:00
{
val, found := receiver.VapidKey.Get()
if !found {
return nil, errCannotMashalApplicationAsJSONNoVapidKey
}
data["vapid_key"] = val
}
receiver.ClientID.WhenSomething(func(value string){
data["client_id"] = value
})
receiver.ClientSecret.WhenSomething(func(value string){
data["client_secret"] = value
})
return json.Marshal(data)
}