Skip to content

Commit

Permalink
Merge pull request #264 from StupidScience/fix-channel-json-tags
Browse files Browse the repository at this point in the history
Fix logentry.channel json marshaling/unmarshaling
  • Loading branch information
theckman committed Feb 24, 2021
2 parents 69ade4b + 74887a4 commit 5bedfd1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
14 changes: 13 additions & 1 deletion log_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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.
Expand Down Expand Up @@ -120,3 +120,15 @@ func (c *Channel) UnmarshalJSON(b []byte) error {

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 != "" {
for k, v := range c.Raw {
raw[k] = v
}
}

return json.Marshal(raw)
}
49 changes: 49 additions & 0 deletions log_entry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pagerduty

import (
"encoding/json"
"net/http"
"testing"
)
Expand Down Expand Up @@ -72,3 +73,51 @@ 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{}{
"type": "web_trigger",
"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)
}

0 comments on commit 5bedfd1

Please sign in to comment.