commit c26e0f9dab045063f9fb15a64233322532b18dcb from: Oliver Lowe date: Wed Feb 02 15:01:42 2022 UTC Use default MarshalJSON method There's no point doing so much manual work and testing just to keep fields under a "attrs" key when marshalling to JSON. When creating the object we can just: 1. Marshal the object to JSON 2. Put it all under a "attrs" key. Now it's easier to imagine the mapping of the structs to JSON, since they follow the same rules as everything else in Go. commit - 717a9e7823638c994c6620883fcee2ef98da294b commit + c26e0f9dab045063f9fb15a64233322532b18dcb blob - cebae45cbcad614a015ead354171c783af804a6c blob + acd4390c9d48fa0fd5d47a465bf3e067da19816c --- host.go +++ host.go @@ -55,12 +55,6 @@ func (hg HostGroup) path() string { return "/objects/hostgroups/" + hg.Name } -func (h Host) MarshalJSON() ([]byte, error) { - type alias Host - a := alias(h) - return json.Marshal(map[string]interface{}{"attrs": a}) -} - // UnmarhsalJSON unmarshals host attributes into more meaningful Host field types. func (h *Host) UnmarshalJSON(data []byte) error { type alias Host @@ -78,9 +72,3 @@ func (h *Host) UnmarshalJSON(data []byte) error { } return nil } - -func (hg HostGroup) MarshalJSON() ([]byte, error) { - type alias HostGroup - a := alias(hg) - return json.Marshal(map[string]interface{}{"attrs": a}) -} blob - 981b6c85fc470333686a0a81cd94f54d8da819e3 blob + 18943250b3644b6953b6f9f2ba10819721dcc176 --- host_test.go +++ host_test.go @@ -1,40 +1,11 @@ package icinga import ( - "encoding/json" "os" "reflect" "testing" ) -func TestHostMarshal(t *testing.T) { - b := []byte(`{"attrs":{"address":"192.0.2.1","address6":"2001:db8::","check_command":"dummy","display_name":"Example host","groups":["test"]}}`) - want := make(map[string]interface{}) - if err := json.Unmarshal(b, &want); err != nil { - t.Fatal(err) - } - - p, err := json.Marshal(Host{ - Name: "example.com", - Address: "192.0.2.1", - Address6: "2001:db8::", - Groups: []string{"test"}, - StateType: StateSoft, - CheckCommand: "dummy", - DisplayName: "Example host", - }) - if err != nil { - t.Fatal(err) - } - got := make(map[string]interface{}) - if err := json.Unmarshal(p, &got); err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(want, got) { - t.Error("want", want, "got", got) - } -} - func TestHostUnmarshal(t *testing.T) { want := Host{ Name: "VuS9jZ8u.example.org", blob - faef3a21b4d7a2dd7c852d98da51a75a11c30355 blob + c9d34273251c4488a2de996b0fefc36114881964 --- object.go +++ object.go @@ -59,7 +59,9 @@ func (c *Client) createObject(obj object) error { buf := &bytes.Buffer{} switch v := obj.(type) { case Host, Service, User, HostGroup: - if err := json.NewEncoder(buf).Encode(v); err != nil { + m := make(map[string]interface{}) + m["attrs"] = v + if err := json.NewEncoder(buf).Encode(m); err != nil { return err } default: blob - 3d3a9245b3ff138b1503e66793d7f0faaa7be94f blob + 06a63ce3ee17549ea8e373106a3ad58600e9e86f --- service.go +++ service.go @@ -12,14 +12,14 @@ func (s Service) path() string { // Service represents a Service object. type Service struct { - Name string `json:"-"` - Groups []string `json:"groups,omitempty"` - State ServiceState - StateType StateType `json:"state_type"` - CheckCommand string `json:"check_command"` - DisplayName string `json:"display_name,omitempty"` - LastCheckResult CheckResult `json:"last_check_result,omitempty"` - Acknowledgement bool `json:",omitempty"` + Name string `json:"-"` + Groups []string `json:"groups,omitempty"` + State ServiceState `json:"state,omitempty"` + StateType StateType `json:"state_type,omitempty"` + CheckCommand string `json:"check_command"` + DisplayName string `json:"display_name,omitempty"` + LastCheckResult *CheckResult `json:"last_check_result,omitempty"` + Acknowledgement bool `json:",omitempty"` } type CheckResult struct { @@ -47,19 +47,6 @@ func (state ServiceState) String() string { return "ServiceUnknown" } -func (s Service) MarshalJSON() ([]byte, error) { - attrs := make(map[string]interface{}) - if len(s.Groups) > 0 { - attrs["groups"] = s.Groups - } - attrs["check_command"] = s.CheckCommand - attrs["display_name"] = s.DisplayName - jservice := &struct { - Attrs map[string]interface{} `json:"attrs"` - }{Attrs: attrs} - return json.Marshal(jservice) -} - // UnmarshalJSON unmarshals service attributes into more meaningful Service field types. func (s *Service) UnmarshalJSON(data []byte) error { type alias Service blob - 66fe315b19691f68e64f8f90ba8971310d7fffbb blob + b1658421cdbb8d78aba72e7a5683d1e94b840947 --- service_test.go +++ service_test.go @@ -1,7 +1,6 @@ package icinga import ( - "encoding/json" "os" "reflect" "testing" @@ -24,7 +23,7 @@ func TestServiceUnmarshal(t *testing.T) { StateType: StateHard, CheckCommand: "http", DisplayName: "http", - LastCheckResult: CheckResult{ + LastCheckResult: &CheckResult{ Output: "HTTP OK: HTTP/1.1 200 OK - 1714 bytes in 1.083 second response time ", }, } @@ -38,26 +37,3 @@ func TestServiceUnmarshal(t *testing.T) { t.Errorf("want %+v, got %+v", want, got) } } - -func TestServiceMarshal(t *testing.T) { - want := `{"attrs":{"check_command":"http","display_name":"http"}}` - - b, err := json.Marshal(Service{ - Name: "9p.io!http", - Groups: []string{}, - State: ServiceOK, - StateType: StateHard, - CheckCommand: "http", - DisplayName: "http", - LastCheckResult: CheckResult{ - Output: "HTTP OK: HTTP/1.1 200 OK - 1714 bytes in 0.703 second response time ", - }, - }) - if err != nil { - t.Error(err) - } - got := string(b) - if want != got { - t.Error("want", want, "got", got) - } -} blob - 740b452988d7f0b545fbfb14b7880844d4cf5edb blob + ca94556b9c34783670c817a97810129b60dc06ac --- user.go +++ user.go @@ -1,7 +1,5 @@ package icinga -import "encoding/json" - // User represents a User object. // Note that this is different from an ApiUser. type User struct { @@ -10,12 +8,6 @@ type User struct { Groups []string `json:"groups,omitempty"` } -func (u User) MarshalJSON() ([]byte, error) { - type alias User - a := alias(u) - return json.Marshal(map[string]interface{}{"attrs": a}) -} - func (u User) name() string { return u.Name } blob - 9b67b01b2c6089f55ee6c002f992a2b07d971139 blob + cc85c8bc8cfc9ee0b85ba89d77039b94dc9d8e3b --- user_test.go +++ user_test.go @@ -1,7 +1,6 @@ package icinga import ( - "encoding/json" "os" "reflect" "testing" @@ -23,15 +22,3 @@ func TestUser(t *testing.T) { t.Errorf("want: %+v, got %+v", want, got) } } - -func TestUserMarshal(t *testing.T) { - user := &User{Name: "test", Email: "test@example.com", Groups: []string{}} - want := `{"attrs":{"email":"test@example.com"}}` - got, err := json.Marshal(user) - if err != nil { - t.Fatal(err) - } - if string(got) != want { - t.Errorf("want %s got %s", want, got) - } -}