From 147d0ff043c06af15bcfe82da910ba5bc3933d16 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Wed, 27 Sep 2023 17:52:38 +0900 Subject: [PATCH] initial commits --- ent/application.go | 108 +++++++++++++++++++++++----- ent/application_marshaljson_test.go | 20 +++--- 2 files changed, 99 insertions(+), 29 deletions(-) diff --git a/ent/application.go b/ent/application.go index 5535d3c..13a1674 100644 --- a/ent/application.go +++ b/ent/application.go @@ -28,7 +28,11 @@ type Application struct { } func (receiver Application) MarshalJSON() ([]byte, error) { - var data map[string]interface{} = map[string]interface{}{} + + var array [512]byte + var buffer []byte = array[0:0] + + buffer = append(buffer, "{"...) { val, found := receiver.Name.Get() @@ -36,18 +40,38 @@ func (receiver Application) MarshalJSON() ([]byte, error) { return nil, errCannotMashalApplicationAsJSONNoName } - data["name"] = val + 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...) + } } - receiver.WebSite.WhenSomething(func(value string){ - data["website"] = value - }) - receiver.WebSite.WhenNull(func(){ - data["website"] = nil - }) - receiver.WebSite.WhenNothing(func(){ - data["website"] = nil - }) + { + 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() @@ -55,16 +79,62 @@ func (receiver Application) MarshalJSON() ([]byte, error) { return nil, errCannotMashalApplicationAsJSONNoVapidKey } - data["vapid_key"] = val + 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...) + } } - receiver.ClientID.WhenSomething(func(value string){ - data["client_id"] = value - }) + { + 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") + } - receiver.ClientSecret.WhenSomething(func(value string){ - data["client_secret"] = value - }) + buffer = append(buffer, `,"client_id":`...) - return json.Marshal(data) + 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...) + } + } + + buffer = append(buffer, "}"...) + + + return buffer, nil } diff --git a/ent/application_marshaljson_test.go b/ent/application_marshaljson_test.go index bf667cd..a26f7a3 100644 --- a/ent/application_marshaljson_test.go +++ b/ent/application_marshaljson_test.go @@ -38,14 +38,14 @@ func TestApplication_MarshalJSON(t *testing.T) { { Application: ent.Application{ Name: opt.Something("client fish"), - WebSite: nul.Something("http://example.com/"), - VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"), + WebSite: nul.Something("http://example.fish/"), + VapidKey: opt.Something("BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"), }, Expected: `{`+ `"name":"client fish"`+ `,`+ - `"website":"http://example.com/"`+ + `"website":"http://example.fish/"`+ `,`+ `"vapid_key":"BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"`+ `}`, @@ -56,8 +56,8 @@ func TestApplication_MarshalJSON(t *testing.T) { { Application: ent.Application{ Name: opt.Something("frontodon"), - WebSite: nul.Something("http://example.com/"), - VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"), + WebSite: nul.Something("http://something.tld/"), + VapidKey: opt.Something("BLCtzyFc2xtnAqWYW3m4M0v47Uym9mKd5yQMvY3FLWzdXN2vpjzF3iQ413fKwNOITkhJ6_tlvTZELL876uokpM4"), ClientID: opt.Something("22"), }, Expected: @@ -66,7 +66,7 @@ func TestApplication_MarshalJSON(t *testing.T) { `,`+ `"website":"http://something.tld/"`+ `,`+ - `"vapid_key":"BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"`+ + `"vapid_key":"BLCtzyFc2xtnAqWYW3m4M0v47Uym9mKd5yQMvY3FLWzdXN2vpjzF3iQ413fKwNOITkhJ6_tlvTZELL876uokpM4"`+ `,`+ `"client_id":"22"`+ `}`, @@ -77,16 +77,16 @@ func TestApplication_MarshalJSON(t *testing.T) { { Application: ent.Application{ Name: opt.Something("super-gorilla"), - WebSite: nul.Something("http://example.com/"), - VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"), + WebSite: nul.Something("http://example.gorilla/"), + VapidKey: opt.Something("BNi9UYyWxKVULR-FEaCWt3NuAHrlyyIz7zYRyIyLP0Q46ePHsiLbd8wHG3VXy-wTAgzFsRb5pGxJZDeX3FROwlE"), ClientID: opt.Something("22"), - ClientSecret: opt.Something(""), + ClientSecret: opt.Something("NwlOvca6TQjFutgobDM6voupU8kIqzN0h_oa1pEqBD4"), }, Expected: `{`+ `"name":"super-gorilla"`+ `,`+ - `"website":"http://something.tld/"`+ + `"website":"http://example.gorilla/"`+ `,`+ `"vapid_key":"BNi9UYyWxKVULR-FEaCWt3NuAHrlyyIz7zYRyIyLP0Q46ePHsiLbd8wHG3VXy-wTAgzFsRb5pGxJZDeX3FROwlE"`+ `,`+