From 32a3183a1c4423541344f25a30acebab5ded26ae Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Tue, 26 Sep 2023 17:49:01 +0900 Subject: [PATCH] initial commits --- ent/application.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 ent/application.go diff --git a/ent/application.go b/ent/application.go new file mode 100644 index 0000000..c1346dd --- /dev/null +++ b/ent/application.go @@ -0,0 +1,67 @@ +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"` + WebSite nul.Nullable[string] `json:"website"` + VapidKey opt.Optional[string] `json:"vapid_key"` + ClientID opt.Optional[string] `json:"client_id"` + ClientSecret opt.Optional[string] `json:"client_secret"` +} + +func (receiver Application) MarshalJSON() ([]byte, error) { + var data map[string]interface{} + + { + 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 + }) + + { + 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) +}