commit c26e0f9dab045063f9fb15a64233322532b18dcb
parent 717a9e7823638c994c6620883fcee2ef98da294b
Author: Oliver Lowe <o@olowe.co>
Date: Thu, 3 Feb 2022 01:54:23 +1100
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.
Diffstat:
7 files changed, 12 insertions(+), 109 deletions(-)
diff --git a/host.go b/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})
-}
diff --git a/host_test.go b/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",
diff --git a/object.go b/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:
diff --git a/service.go b/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
diff --git a/service_test.go b/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)
- }
-}
diff --git a/user.go b/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
}
diff --git a/user_test.go b/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)
- }
-}