initial commits

master
Charles Iliya Krempeaux 2023-09-27 17:52:38 +09:00
parent 1126d81e0a
commit 147d0ff043
2 changed files with 99 additions and 29 deletions

View File

@ -28,7 +28,11 @@ type Application struct {
} }
func (receiver Application) MarshalJSON() ([]byte, error) { 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() val, found := receiver.Name.Get()
@ -36,18 +40,38 @@ func (receiver Application) MarshalJSON() ([]byte, error) {
return nil, errCannotMashalApplicationAsJSONNoName 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 buffer = append(buffer, `,"website":`...)
})
receiver.WebSite.WhenNull(func(){ switch receiver.WebSite {
data["website"] = nil case nul.Nothing[string](), nul.Null[string]():
}) buffer = append(buffer, `null`...)
receiver.WebSite.WhenNothing(func(){ default:
data["website"] = nil 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() val, found := receiver.VapidKey.Get()
@ -55,16 +79,62 @@ func (receiver Application) MarshalJSON() ([]byte, error) {
return nil, errCannotMashalApplicationAsJSONNoVapidKey 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){ buffer = append(buffer, `,"client_id":`...)
data["client_secret"] = value
})
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
} }

View File

@ -38,14 +38,14 @@ func TestApplication_MarshalJSON(t *testing.T) {
{ {
Application: ent.Application{ Application: ent.Application{
Name: opt.Something("client fish"), Name: opt.Something("client fish"),
WebSite: nul.Something("http://example.com/"), WebSite: nul.Something("http://example.fish/"),
VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"), VapidKey: opt.Something("BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"),
}, },
Expected: Expected:
`{`+ `{`+
`"name":"client fish"`+ `"name":"client fish"`+
`,`+ `,`+
`"website":"http://example.com/"`+ `"website":"http://example.fish/"`+
`,`+ `,`+
`"vapid_key":"BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"`+ `"vapid_key":"BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"`+
`}`, `}`,
@ -56,8 +56,8 @@ func TestApplication_MarshalJSON(t *testing.T) {
{ {
Application: ent.Application{ Application: ent.Application{
Name: opt.Something("frontodon"), Name: opt.Something("frontodon"),
WebSite: nul.Something("http://example.com/"), WebSite: nul.Something("http://something.tld/"),
VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"), VapidKey: opt.Something("BLCtzyFc2xtnAqWYW3m4M0v47Uym9mKd5yQMvY3FLWzdXN2vpjzF3iQ413fKwNOITkhJ6_tlvTZELL876uokpM4"),
ClientID: opt.Something("22"), ClientID: opt.Something("22"),
}, },
Expected: Expected:
@ -66,7 +66,7 @@ func TestApplication_MarshalJSON(t *testing.T) {
`,`+ `,`+
`"website":"http://something.tld/"`+ `"website":"http://something.tld/"`+
`,`+ `,`+
`"vapid_key":"BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"`+ `"vapid_key":"BLCtzyFc2xtnAqWYW3m4M0v47Uym9mKd5yQMvY3FLWzdXN2vpjzF3iQ413fKwNOITkhJ6_tlvTZELL876uokpM4"`+
`,`+ `,`+
`"client_id":"22"`+ `"client_id":"22"`+
`}`, `}`,
@ -77,16 +77,16 @@ func TestApplication_MarshalJSON(t *testing.T) {
{ {
Application: ent.Application{ Application: ent.Application{
Name: opt.Something("super-gorilla"), Name: opt.Something("super-gorilla"),
WebSite: nul.Something("http://example.com/"), WebSite: nul.Something("http://example.gorilla/"),
VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"), VapidKey: opt.Something("BNi9UYyWxKVULR-FEaCWt3NuAHrlyyIz7zYRyIyLP0Q46ePHsiLbd8wHG3VXy-wTAgzFsRb5pGxJZDeX3FROwlE"),
ClientID: opt.Something("22"), ClientID: opt.Something("22"),
ClientSecret: opt.Something(""), ClientSecret: opt.Something("NwlOvca6TQjFutgobDM6voupU8kIqzN0h_oa1pEqBD4"),
}, },
Expected: Expected:
`{`+ `{`+
`"name":"super-gorilla"`+ `"name":"super-gorilla"`+
`,`+ `,`+
`"website":"http://something.tld/"`+ `"website":"http://example.gorilla/"`+
`,`+ `,`+
`"vapid_key":"BNi9UYyWxKVULR-FEaCWt3NuAHrlyyIz7zYRyIyLP0Q46ePHsiLbd8wHG3VXy-wTAgzFsRb5pGxJZDeX3FROwlE"`+ `"vapid_key":"BNi9UYyWxKVULR-FEaCWt3NuAHrlyyIz7zYRyIyLP0Q46ePHsiLbd8wHG3VXy-wTAgzFsRb5pGxJZDeX3FROwlE"`+
`,`+ `,`+