diff --git a/ent/account.go b/ent/account.go index 4c18e00..c3d09cc 100644 --- a/ent/account.go +++ b/ent/account.go @@ -27,9 +27,9 @@ type Account struct { AvatarStatic opt.Optional[string] `json:"avatar_static"` Header opt.Optional[string] `json:"header"` HeaderStatic opt.Optional[string] `json:"header_static"` - Locked opt.Optional[bool] `json:"locked"` Fields []Field `json:"fields"` Emojis []CustomEmoji `json:"emojis"` + Locked opt.Optional[bool] `json:"locked"` Bot opt.Optional[bool] `json:"bot"` Group opt.Optional[bool] `json:"group"` Discoverable nul.Nullable[bool] `json:"discoverable"` @@ -41,7 +41,7 @@ type Account struct { LastStatusAt nul.Nullable[string] `json:"last_status_at"` StatusesCount opt.Optional[jsonint.Int] `json:"statuses_count"` FollowersCount opt.Optional[jsonint.Int] `json:"followers_count"` - FollowingCount opt.Optional[jsonint.Int] `json:"following_count"` + FollowingCount opt.Optional[jsonint.Int] `json:"following_count"` Roles []Role `json:"roles"` MuteExpiresAt nul.Nullable[string] `json:"mute_expires_at"` } @@ -173,21 +173,15 @@ func (receiver Account) MarshalJSON() ([]byte, error) { buffer = append(buffer, marshaled...) } - { - buffer = append(buffer, `,"locked":`...) - - marshaled, err := json.Marshal(receiver.Locked) - if nil != err { - return nil, erorr.Errorf("mstdn/ent: could not marshal ent.Account.Locked as JSON: %w", err) - } - - buffer = append(buffer, marshaled...) - } - { buffer = append(buffer, `,"fields":`...) - marshaled, err := json.Marshal(receiver.Fields) + var src interface{} = receiver.Fields + if nil == receiver.Fields { + src = []Field{} + } + + marshaled, err := json.Marshal(src) if nil != err { return nil, erorr.Errorf("mstdn/ent: could not marshal ent.Account.Fields as JSON: %w", err) } @@ -198,7 +192,12 @@ func (receiver Account) MarshalJSON() ([]byte, error) { { buffer = append(buffer, `,"emojis":`...) - marshaled, err := json.Marshal(receiver.Emojis) + var src interface{} = receiver.Emojis + if nil == receiver.Emojis { + src = []CustomEmoji{} + } + + marshaled, err := json.Marshal(src) if nil != err { return nil, erorr.Errorf("mstdn/ent: could not marshal ent.Account.Emojis as JSON: %w", err) } @@ -206,6 +205,17 @@ func (receiver Account) MarshalJSON() ([]byte, error) { buffer = append(buffer, marshaled...) } + { + buffer = append(buffer, `,"locked":`...) + + marshaled, err := json.Marshal(receiver.Locked) + if nil != err { + return nil, erorr.Errorf("mstdn/ent: could not marshal ent.Account.Locked as JSON: %w", err) + } + + buffer = append(buffer, marshaled...) + } + { buffer = append(buffer, `,"bot":`...) @@ -240,13 +250,13 @@ func (receiver Account) MarshalJSON() ([]byte, error) { } { - if nul.Nothing[bool]() != receiver.Discoverable { + if nul.Nothing[bool]() != receiver.NoIndex { buffer = append(buffer, `,"noindex":`...) - marshaled, err := json.Marshal(receiver.Discoverable) + marshaled, err := json.Marshal(receiver.NoIndex) if nil != err { - return nil, erorr.Errorf("mstdn/ent: could not marshal ent.Account.Discoverable as JSON: %w", err) + return nil, erorr.Errorf("mstdn/ent: could not marshal ent.Account.NoIndex as JSON: %w", err) } buffer = append(buffer, marshaled...) diff --git a/ent/account_marshaljson_test.go b/ent/account_marshaljson_test.go new file mode 100644 index 0000000..f78db56 --- /dev/null +++ b/ent/account_marshaljson_test.go @@ -0,0 +1,119 @@ +package ent_test + +import ( + "testing" + + "encoding/json" + + "sourcecode.social/reiver/go-jsonint" + "sourcecode.social/reiver/go-nul" + "sourcecode.social/reiver/go-opt" + + "sourcecode.social/reiver/go-mstdn/ent" +) + +func TestAccount_MarshalJSON(t *testing.T) { + + tests := []struct{ + Account ent.Account + Expected string + }{ + { + Account: ent.Account{ + ID: opt.Something("47"), + UserName: opt.Something("joeblow"), + Acct: opt.Something("joeblow@example.com"), + URL: opt.Something("https://example.com/@joeblow"), + URI: opt.Something("https://example.com/users/joeblow"), + DisplayName: opt.Something("Joe Blow :-)"), + Note: opt.Something("

Hello world!

"), + Avatar: opt.Something("https://files.example.com/avatar/joeblow.png"), + AvatarStatic: opt.Something("https://files.example.com/avatar-static/joeblow.png"), + Header: opt.Something("https://files.example.com/header/joeblow.png"), + HeaderStatic: opt.Something("https://files.example.com/header-static/joeblow.png"), + //Fields + //Emojis + Locked: opt.Something(false), + Bot: opt.Something(false), + Group: opt.Something(false), + Discoverable: nul.Null[bool](), + CreatedAt: opt.Something("2023-09-27T22:06:19Z"), + LastStatusAt: nul.Null[string](), + StatusesCount: opt.Something(jsonint.Int64(123)), + FollowersCount: opt.Something(jsonint.Int64(24789)), + FollowingCount: opt.Something(jsonint.Int64(355)), + }, + + Expected: `{`+ + `"id":"47"`+ + `,`+ + `"username":"joeblow"`+ + `,`+ + `"acct":"joeblow@example.com"`+ + `,`+ + `"url":"https://example.com/@joeblow"`+ + `,`+ + `"uri":"https://example.com/users/joeblow"`+ + `,`+ + `"display_name":"Joe Blow :-)"`+ + `,`+ + `"note":"\u003cp\u003eHello world!\u003c/p\u003e"`+ + `,`+ + `"avatar":"https://files.example.com/avatar/joeblow.png"`+ + `,`+ + `"avatar_static":"https://files.example.com/avatar-static/joeblow.png"`+ + `,`+ + `"header":"https://files.example.com/header/joeblow.png"`+ + `,`+ + `"header_static":"https://files.example.com/header-static/joeblow.png"`+ + `,`+ + `"fields":[]`+ + `,`+ + `"emojis":[]`+ + `,`+ + `"locked":false`+ + `,`+ + `"bot":false`+ + `,`+ + `"group":false`+ + `,`+ + `"discoverable":null`+ + `,`+ + `"created_at":"2023-09-27T22:06:19Z"`+ + `,`+ + `"last_status_at":null`+ + `,`+ + `"statuses_count":123`+ + `,`+ + `"followers_count":24789`+ + `,`+ + `"following_count":355`+ + `}`, + }, + } + + for testNumber, test := range tests { + + actualBytes, err := json.Marshal(test.Account) + 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("ACCOUNT: %#v", test.Account) + t.Logf("EXPECTED:\n%s", test.Expected) + 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("ACCOUNT: %#v", test.Account) + continue + } + } + } +}