go-mstdn/ent/application.go

141 lines
3.5 KiB
Go
Raw Normal View History

2023-09-26 08:49:01 +00:00
package ent
import (
"encoding/json"
"github.com/reiver/go-erorr"
"github.com/reiver/go-opt"
"github.com/reiver/go-nul"
2023-09-26 08:49:01 +00:00
)
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 08:52:38 +00:00
var array [512]byte
var buffer []byte = array[0:0]
buffer = append(buffer, "{"...)
2023-09-26 08:49:01 +00:00
{
val, found := receiver.Name.Get()
if !found {
return nil, errCannotMashalApplicationAsJSONNoName
}
2023-09-27 08:52:38 +00:00
buffer = append(buffer, `"name":`...)
{
marshaled, err := json.Marshal(val)
if nil != err {
return nil, erorr.Errorf("ent: could not marshal ent.Application.Name as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
2023-09-26 08:49:01 +00:00
}
2023-09-27 08:52:38 +00:00
{
buffer = append(buffer, `,"website":`...)
switch receiver.WebSite {
case nul.Nothing[string](), nul.Null[string]():
buffer = append(buffer, `null`...)
default:
website, found := receiver.WebSite.Get()
if !found {
return nil, erorr.Error("ent: could not marshal ent.Application.WebSite as JSON: internal error")
}
marshaled, err := json.Marshal(website)
if nil != err {
return nil, erorr.Errorf("ent: could not marshal ent.Application.WebSite as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
}
2023-09-26 08:49:01 +00:00
{
val, found := receiver.VapidKey.Get()
if !found {
return nil, errCannotMashalApplicationAsJSONNoVapidKey
}
2023-09-27 08:52:38 +00:00
buffer = append(buffer, `,"vapid_key":`...)
{
marshaled, err := json.Marshal(val)
if nil != err {
return nil, erorr.Errorf("ent: could not marshal ent.Application.VapidKey as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
}
{
switch receiver.ClientID {
case opt.Nothing[string]():
// Nothing here.
default:
clientID, found := receiver.ClientID.Get()
if !found {
return nil, erorr.Error("ent: could not marshal ent.Application.ClientID as JSON: internal error")
}
buffer = append(buffer, `,"client_id":`...)
marshaled, err := json.Marshal(clientID)
if nil != err {
return nil, erorr.Errorf("ent: could not marshal ent.Application.ClientID as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
}
{
switch receiver.ClientSecret {
case opt.Nothing[string]():
// Nothing here.
default:
clientSecret, found := receiver.ClientSecret.Get()
if !found {
return nil, erorr.Error("ent: could not marshal ent.Application.ClientSecret as JSON: internal error")
}
buffer = append(buffer, `,"client_secret":`...)
marshaled, err := json.Marshal(clientSecret)
if nil != err {
return nil, erorr.Errorf("ent: could not marshal ent.Application.ClientSecret as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
2023-09-26 08:49:01 +00:00
}
2023-09-27 08:52:38 +00:00
buffer = append(buffer, "}"...)
2023-09-26 08:49:01 +00:00
2023-09-27 08:52:38 +00:00
return buffer, nil
2023-09-26 08:49:01 +00:00
}