initial commits

master
Charles Iliya Krempeaux 2023-09-26 21:07:45 +09:00
parent 1047623091
commit 1e327c0bc4
2 changed files with 155 additions and 1 deletions

View File

@ -1,10 +1,14 @@
package ent package ent
import ( import (
"encoding/json"
"sourcecode.social/reiver/go-opt" "sourcecode.social/reiver/go-opt"
"sourcecode.social/reiver/go-jsonint" "sourcecode.social/reiver/go-jsonint"
) )
var _ json.Unmarshaler = new(Role)
// Role represents a Mastodon API "Role". // Role represents a Mastodon API "Role".
// //
// See: // See:
@ -14,5 +18,40 @@ type Role struct {
Name opt.Optional[string] `json:"name"` Name opt.Optional[string] `json:"name"`
Color opt.Optional[string] `json:"color"` Color opt.Optional[string] `json:"color"`
Permissions opt.Optional[jsonint.Int] `json:"permissions"` Permissions opt.Optional[jsonint.Int] `json:"permissions"`
Highlighted opt.Optional[bool] `json:"color"` Highlighted opt.Optional[bool] `json:"highlighted"`
}
type role struct {
ID opt.Optional[jsonint.Int] `json:"id"`
Name opt.Optional[string] `json:"name"`
Color opt.Optional[string] `json:"color"`
Permissions opt.Optional[jsonint.Int] `json:"permissions"`
Highlighted opt.Optional[bool] `json:"Highlighted"`
}
func (receiver *Role) UnmarshalJSON(data []byte) error {
if nil == receiver {
return errNilReceiver
}
var dst role
if err := json.Unmarshal(data, &dst); nil != err {
return err
}
receiver.ID = dst.ID
receiver.Name = dst.Name
receiver.Permissions = dst.Permissions
receiver.Highlighted = dst.Highlighted
dst.Color.WhenSomething(func(value string){
if "" == value {
return
}
receiver.Color = opt.Something(value)
})
return nil
} }

View File

@ -0,0 +1,115 @@
package ent_test
import (
"testing"
"encoding/json"
"sourcecode.social/reiver/go-jsonint"
"sourcecode.social/reiver/go-opt"
"sourcecode.social/reiver/go-mstdn/ent"
)
func TestRole_MarshalJSON(t *testing.T) {
tests := []struct{
JSON string
Expected ent.Role
}{
{
JSON:
`{`+
`"id":12345,`+
`"name":"Master",`+
`"color":"#1a2b3c",`+
`"permissions":256,`+
`"highlighted":true`+
`}`,
Expected: ent.Role{
ID: opt.Something(jsonint.Int64(12345)),
Name: opt.Something("Master"),
Color: opt.Something("#1a2b3c"),
Permissions: opt.Something(jsonint.Int64(256)),
Highlighted: opt.Something(true),
},
},
{
JSON:
`{`+
`"id":71727374,`+
`"name":"Student",`+
`"color":"#76cd54",`+
`"permissions":664,`+
`"highlighted":false`+
`}`,
Expected: ent.Role{
ID: opt.Something(jsonint.Int64(71727374)),
Name: opt.Something("Student"),
Color: opt.Something("#76cd54"),
Permissions: opt.Something(jsonint.Int64(664)),
Highlighted: opt.Something(false),
},
},
{
JSON:
`{`+
`"id":87,`+
`"name":"QA Specialist",`+
`"color":"",`+
`"permissions":218,`+
`"highlighted":true`+
`}`,
Expected: ent.Role{
ID: opt.Something(jsonint.Int64(87)),
Name: opt.Something("QA Specialist"),
Permissions: opt.Something(jsonint.Int64(218)),
Highlighted: opt.Something(true),
},
},
{
JSON:
`{`+
`"id":87,`+
`"name":"QA Specialist",`+
`"permissions":218,`+
`"highlighted":true`+
`}`,
Expected: ent.Role{
ID: opt.Something(jsonint.Int64(87)),
Name: opt.Something("QA Specialist"),
Permissions: opt.Something(jsonint.Int64(218)),
Highlighted: opt.Something(true),
},
},
}
for testNumber, test := range tests {
var actual ent.Role
err := json.Unmarshal([]byte(test.JSON), &actual)
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("JSON:\n%s", test.JSON)
t.Logf("EXPECTED:\n%#v", test.Expected)
continue
}
{
var expected ent.Role = test.Expected
if expected != actual {
t.Errorf("For test #%d, the actual value is not what was expected.", testNumber)
t.Logf("EXPECTED:\n%#v", expected)
t.Logf("ACTUAL:\n%#v", actual)
t.Logf("JSON:\n%s", test.JSON)
continue
}
}
}
}