From 635d11aa9dd99bafa69900e7ea1859d08481a357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAnton?= Date: Mon, 22 Feb 2021 17:47:45 +0300 Subject: [PATCH 1/2] Add logentry.channel json custom marshalling --- log_entry.go | 21 ++++++++++++++++++++- log_entry_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/log_entry.go b/log_entry.go index d3c0905e..9c94fc43 100644 --- a/log_entry.go +++ b/log_entry.go @@ -40,7 +40,7 @@ type CommonLogEntryField struct { // LogEntry is a list of all of the events that happened to an incident. type LogEntry struct { CommonLogEntryField - Incident Incident + Incident Incident `json:"incident"` } // ListLogEntryResponse is the response data when calling the ListLogEntry API endpoint. @@ -115,8 +115,27 @@ func (c *Channel) UnmarshalJSON(b []byte) error { ct, ok := raw["type"] if ok { c.Type = ct.(string) + delete(raw, "type") c.Raw = raw } return nil } + +// MarshalJSON Expands the LogEntry.Channel object to correctly marshal it back +func (c *Channel) MarshalJSON() ([]byte, error) { + raw := map[string]interface{}{} + if c != nil && c.Type != "" { + raw["type"] = c.Type + for k, v := range c.Raw { + raw[k] = v + } + } + + b, err := json.Marshal(raw) + if err != nil { + return nil, err + } + + return b, nil +} diff --git a/log_entry_test.go b/log_entry_test.go index fbc6e03d..21a99d28 100644 --- a/log_entry_test.go +++ b/log_entry_test.go @@ -1,6 +1,7 @@ package pagerduty import ( + "encoding/json" "net/http" "testing" ) @@ -72,3 +73,50 @@ func TestLogEntry_Get(t *testing.T) { } testEqual(t, want, res) } + +func TestChannel_MarhalUnmarshal(t *testing.T) { + logEntryRaw := []byte(`{ + "id": "1", + "type": "trigger_log_entry", + "summary": "foo", + "channel": { + "type": "web_trigger", + "summary": "My new incident", + "details_omitted": false + } + }`) + want := &LogEntry{ + CommonLogEntryField: CommonLogEntryField{ + APIObject: APIObject{ + ID: "1", + Type: "trigger_log_entry", + Summary: "foo", + }, + Channel: Channel{ + Type: "web_trigger", + Raw: map[string]interface{}{ + "summary": "My new incident", + "details_omitted": false, + }, + }, + }, + } + + logEntry := &LogEntry{} + if err := json.Unmarshal(logEntryRaw, logEntry); err != nil { + t.Fatal(err) + } + + testEqual(t, want, logEntry) + + newLogEntryRaw, err := json.Marshal(logEntry) + if err != nil { + t.Fatal(err) + } + + newLogEntry := &LogEntry{} + if err := json.Unmarshal(newLogEntryRaw, newLogEntry); err != nil { + t.Fatal(err) + } + testEqual(t, want, newLogEntry) +} From 74887a40e651f6e88c592847a59bf3f17c1b1880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAnton?= Date: Tue, 23 Feb 2021 12:56:12 +0300 Subject: [PATCH 2/2] Do not delete type field from raw channel data --- log_entry.go | 9 +-------- log_entry_test.go | 1 + 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/log_entry.go b/log_entry.go index 9c94fc43..d01bf60a 100644 --- a/log_entry.go +++ b/log_entry.go @@ -115,7 +115,6 @@ func (c *Channel) UnmarshalJSON(b []byte) error { ct, ok := raw["type"] if ok { c.Type = ct.(string) - delete(raw, "type") c.Raw = raw } @@ -126,16 +125,10 @@ func (c *Channel) UnmarshalJSON(b []byte) error { func (c *Channel) MarshalJSON() ([]byte, error) { raw := map[string]interface{}{} if c != nil && c.Type != "" { - raw["type"] = c.Type for k, v := range c.Raw { raw[k] = v } } - b, err := json.Marshal(raw) - if err != nil { - return nil, err - } - - return b, nil + return json.Marshal(raw) } diff --git a/log_entry_test.go b/log_entry_test.go index 21a99d28..ee2114ae 100644 --- a/log_entry_test.go +++ b/log_entry_test.go @@ -95,6 +95,7 @@ func TestChannel_MarhalUnmarshal(t *testing.T) { Channel: Channel{ Type: "web_trigger", Raw: map[string]interface{}{ + "type": "web_trigger", "summary": "My new incident", "details_omitted": false, },