From 1e327c0bc49915a7082c19f476d74359186f6410 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Tue, 26 Sep 2023 21:07:45 +0900 Subject: [PATCH] initial commits --- ent/role.go | 41 +++++++++++- ent/role_unmarshaljson_test.go | 115 +++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 ent/role_unmarshaljson_test.go diff --git a/ent/role.go b/ent/role.go index 3687dea..3653ebf 100644 --- a/ent/role.go +++ b/ent/role.go @@ -1,10 +1,14 @@ package ent import ( + "encoding/json" + "sourcecode.social/reiver/go-opt" "sourcecode.social/reiver/go-jsonint" ) +var _ json.Unmarshaler = new(Role) + // Role represents a Mastodon API "Role". // // See: @@ -14,5 +18,40 @@ type Role struct { Name opt.Optional[string] `json:"name"` Color opt.Optional[string] `json:"color"` 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 } diff --git a/ent/role_unmarshaljson_test.go b/ent/role_unmarshaljson_test.go new file mode 100644 index 0000000..f4bf53f --- /dev/null +++ b/ent/role_unmarshaljson_test.go @@ -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 + } + } + } +}