initial commits
parent
384f1fe066
commit
1126d81e0a
|
@ -21,14 +21,14 @@ const (
|
||||||
// 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"`
|
WebSite nul.Nullable[string] `json:"website"` // optional — field has JSON null value in JSON if not set
|
||||||
VapidKey opt.Optional[string] `json:"vapid_key"`
|
VapidKey opt.Optional[string] `json:"vapid_key"`
|
||||||
ClientID opt.Optional[string] `json:"client_id"`
|
ClientID opt.Optional[string] `json:"client_id"` // optional — field not included in JSON if not set
|
||||||
ClientSecret opt.Optional[string] `json:"client_secret"`
|
ClientSecret opt.Optional[string] `json:"client_secret"` // optional — field not included in JSON if not set
|
||||||
}
|
}
|
||||||
|
|
||||||
func (receiver Application) MarshalJSON() ([]byte, error) {
|
func (receiver Application) MarshalJSON() ([]byte, error) {
|
||||||
var data map[string]interface{}
|
var data map[string]interface{} = map[string]interface{}{}
|
||||||
|
|
||||||
{
|
{
|
||||||
val, found := receiver.Name.Get()
|
val, found := receiver.Name.Get()
|
||||||
|
@ -45,6 +45,9 @@ func (receiver Application) MarshalJSON() ([]byte, error) {
|
||||||
receiver.WebSite.WhenNull(func(){
|
receiver.WebSite.WhenNull(func(){
|
||||||
data["website"] = nil
|
data["website"] = nil
|
||||||
})
|
})
|
||||||
|
receiver.WebSite.WhenNothing(func(){
|
||||||
|
data["website"] = nil
|
||||||
|
})
|
||||||
|
|
||||||
{
|
{
|
||||||
val, found := receiver.VapidKey.Get()
|
val, found := receiver.VapidKey.Get()
|
||||||
|
|
|
@ -0,0 +1,127 @@
|
||||||
|
package ent_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"sourcecode.social/reiver/go-jsonpp"
|
||||||
|
"sourcecode.social/reiver/go-nul"
|
||||||
|
"sourcecode.social/reiver/go-opt"
|
||||||
|
|
||||||
|
"sourcecode.social/reiver/go-mstdn/ent"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestApplication_MarshalJSON(t *testing.T) {
|
||||||
|
|
||||||
|
tests := []struct{
|
||||||
|
Application ent.Application
|
||||||
|
Expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Application: ent.Application{
|
||||||
|
Name: opt.Something("acme app"),
|
||||||
|
VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"),
|
||||||
|
},
|
||||||
|
Expected:
|
||||||
|
`{`+
|
||||||
|
`"name":"acme app"`+
|
||||||
|
`,`+
|
||||||
|
`"website":null`+
|
||||||
|
`,`+
|
||||||
|
`"vapid_key":"BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"`+
|
||||||
|
`}`,
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
Application: ent.Application{
|
||||||
|
Name: opt.Something("client fish"),
|
||||||
|
WebSite: nul.Something("http://example.com/"),
|
||||||
|
VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"),
|
||||||
|
},
|
||||||
|
Expected:
|
||||||
|
`{`+
|
||||||
|
`"name":"client fish"`+
|
||||||
|
`,`+
|
||||||
|
`"website":"http://example.com/"`+
|
||||||
|
`,`+
|
||||||
|
`"vapid_key":"BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"`+
|
||||||
|
`}`,
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
Application: ent.Application{
|
||||||
|
Name: opt.Something("frontodon"),
|
||||||
|
WebSite: nul.Something("http://example.com/"),
|
||||||
|
VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"),
|
||||||
|
ClientID: opt.Something("22"),
|
||||||
|
},
|
||||||
|
Expected:
|
||||||
|
`{`+
|
||||||
|
`"name":"frontodon"`+
|
||||||
|
`,`+
|
||||||
|
`"website":"http://something.tld/"`+
|
||||||
|
`,`+
|
||||||
|
`"vapid_key":"BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"`+
|
||||||
|
`,`+
|
||||||
|
`"client_id":"22"`+
|
||||||
|
`}`,
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
Application: ent.Application{
|
||||||
|
Name: opt.Something("super-gorilla"),
|
||||||
|
WebSite: nul.Something("http://example.com/"),
|
||||||
|
VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"),
|
||||||
|
ClientID: opt.Something("22"),
|
||||||
|
ClientSecret: opt.Something(""),
|
||||||
|
},
|
||||||
|
Expected:
|
||||||
|
`{`+
|
||||||
|
`"name":"super-gorilla"`+
|
||||||
|
`,`+
|
||||||
|
`"website":"http://something.tld/"`+
|
||||||
|
`,`+
|
||||||
|
`"vapid_key":"BNi9UYyWxKVULR-FEaCWt3NuAHrlyyIz7zYRyIyLP0Q46ePHsiLbd8wHG3VXy-wTAgzFsRb5pGxJZDeX3FROwlE"`+
|
||||||
|
`,`+
|
||||||
|
`"client_id":"22"`+
|
||||||
|
`,`+
|
||||||
|
`"client_secret":"NwlOvca6TQjFutgobDM6voupU8kIqzN0h_oa1pEqBD4"`+
|
||||||
|
`}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for testNumber, test := range tests {
|
||||||
|
|
||||||
|
actualBytes, err := json.Marshal(test.Application)
|
||||||
|
if nil != err {
|
||||||
|
t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber)
|
||||||
|
t.Logf("ERROR: (%T) %s", err, err)
|
||||||
|
t.Logf("EXPECTED:\n%s", test.Expected)
|
||||||
|
t.Logf("EXPECTED:\n%s", jsonpp.SPrettyPrint([]byte(test.Expected)))
|
||||||
|
t.Logf("APPLICATION: %#v", test.Application)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
actual := string(actualBytes)
|
||||||
|
expected := test.Expected
|
||||||
|
|
||||||
|
if expected != actual {
|
||||||
|
t.Errorf("For test #%d, the actual value is not what was expected.", testNumber)
|
||||||
|
t.Logf("EXPECTED:\n%s", expected)
|
||||||
|
t.Logf("ACTUAL:\n%s", actual)
|
||||||
|
t.Logf("EXPECTED:\n%s", jsonpp.SPrettyPrint([]byte(expected)))
|
||||||
|
t.Logf("ACTUAL:\n%s", jsonpp.SPrettyPrint([]byte(actual)))
|
||||||
|
t.Logf("APPLICATION: %#v", test.Application)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue