From 1126d81e0a1a3055fbe316aa59e3b08ca3a65e31 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Wed, 27 Sep 2023 16:33:04 +0900 Subject: [PATCH] initial commits --- ent/application.go | 11 ++- ent/application_marshaljson_test.go | 127 ++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 ent/application_marshaljson_test.go diff --git a/ent/application.go b/ent/application.go index c1346dd..5535d3c 100644 --- a/ent/application.go +++ b/ent/application.go @@ -21,14 +21,14 @@ const ( // https://docs.joinmastodon.org/entities/Application/ type Application struct { 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"` - ClientID opt.Optional[string] `json:"client_id"` - ClientSecret opt.Optional[string] `json:"client_secret"` + 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 } func (receiver Application) MarshalJSON() ([]byte, error) { - var data map[string]interface{} + var data map[string]interface{} = map[string]interface{}{} { val, found := receiver.Name.Get() @@ -45,6 +45,9 @@ func (receiver Application) MarshalJSON() ([]byte, error) { receiver.WebSite.WhenNull(func(){ data["website"] = nil }) + receiver.WebSite.WhenNothing(func(){ + data["website"] = nil + }) { val, found := receiver.VapidKey.Get() diff --git a/ent/application_marshaljson_test.go b/ent/application_marshaljson_test.go new file mode 100644 index 0000000..bf667cd --- /dev/null +++ b/ent/application_marshaljson_test.go @@ -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 + } + } + } +}