mstdn/ent.Application

master
Charles Iliya Krempeaux 2024-08-08 12:49:22 -07:00
parent 91a4c8f6af
commit d28393b1dc
2 changed files with 6 additions and 115 deletions

View File

@ -1,127 +1,18 @@
package ent package ent
import ( import (
"encoding/json"
"github.com/reiver/go-erorr"
"github.com/reiver/go-opt" "github.com/reiver/go-opt"
"github.com/reiver/go-nul" "github.com/reiver/go-nul"
) )
var _ json.Marshaler = Application{}
const (
errCannotMashalApplicationAsJSONNoName = erorr.Error("cannot marshal ent.Application to JSON — no name set")
)
// Appication represents a Mastodon API "Appication". // Appication represents a Mastodon API "Appication".
// //
// See: // See:
// https://docs.joinmastodon.org/entities/Application/ // https://docs.joinmastodon.org/entities/Application/
type Application struct { type Application struct {
Name opt.Optional[string] `json:"name"` Name opt.Optional[string] `json:"name"`
WebSite nul.Nullable[string] `json:"website"` // optional — field has JSON null value in JSON if not set WebSite nul.Nullable[string] `json:"website,omitempty"` // optional — field has JSON null value in JSON if not set
VapidKey opt.Optional[string] `json:"vapid_key"` // optional — field not included in JSON if not set VapidKey opt.Optional[string] `json:"vapid_key,omitempty"` // optional — field not included in JSON if not set
ClientID opt.Optional[string] `json:"client_id"` // optional — field not included in JSON if not set ClientID opt.Optional[string] `json:"client_id,omitempty"` // 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 ClientSecret opt.Optional[string] `json:"client_secret,omitempty"` // optional — field not included in JSON if not set
}
func (receiver Application) MarshalJSON() ([]byte, error) {
var array [512]byte
var buffer []byte = array[0:0]
buffer = append(buffer, "{"...)
{
val, found := receiver.Name.Get()
if !found {
return nil, errCannotMashalApplicationAsJSONNoName
}
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...)
}
}
{
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...)
}
}
{
val, found := receiver.VapidKey.Get()
if found {
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...)
}
}
}
{
val, found := receiver.ClientID.Get()
if found {
buffer = append(buffer, `,"client_id":`...)
{
marshaled, err := json.Marshal(val)
if nil != err {
return nil, erorr.Errorf("ent: could not marshal ent.Application.ClientID as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
}
}
{
val, found := receiver.ClientSecret.Get()
if found {
buffer = append(buffer, `,"client_secret":`...)
{
marshaled, err := json.Marshal(val)
if nil != err {
return nil, erorr.Errorf("ent: could not marshal ent.Application.ClientSecret as JSON: %w", err)
}
buffer = append(buffer, marshaled...)
}
}
}
buffer = append(buffer, "}"...)
return buffer, nil
} }

View File

@ -3,8 +3,7 @@ package ent_test
import ( import (
"testing" "testing"
"encoding/json" "github.com/reiver/go-json"
"github.com/reiver/go-jsonpp" "github.com/reiver/go-jsonpp"
"github.com/reiver/go-nul" "github.com/reiver/go-nul"
"github.com/reiver/go-opt" "github.com/reiver/go-opt"
@ -21,6 +20,7 @@ func TestApplication_MarshalJSON(t *testing.T) {
{ {
Application: ent.Application{ Application: ent.Application{
Name: opt.Something("acme app"), Name: opt.Something("acme app"),
WebSite: nul.Null[string](),
VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"), VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"),
}, },
Expected: Expected: