Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ type JSONAttachment struct {
// MarshalJSON implements the json.Marshaler interface.
// The Duration field is marshaled in seconds, all other fields are marshaled
// based upon the definitions in struct tags.
func (a *JSONAttachment) MarshalJSON() ([]byte, error) {
func (a JSONAttachment) MarshalJSON() ([]byte, error) {
type EmbeddedJSONAttachment JSONAttachment
return json.Marshal(&struct {
Duration float64 `json:"duration_in_seconds,omitempty"`
*EmbeddedJSONAttachment
}{
EmbeddedJSONAttachment: (*EmbeddedJSONAttachment)(a),
EmbeddedJSONAttachment: (*EmbeddedJSONAttachment)(&a),
Duration: a.Duration.Seconds(),
})
}
Expand Down
27 changes: 27 additions & 0 deletions json_duration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package feeds

import (
"encoding/json"
"testing"
"time"
)

func TestJSONAttachmentDurationValue(t *testing.T) {
attachment := JSONAttachment{Duration: 1500 * time.Millisecond}
for _, value := range []interface{}{attachment, &attachment, map[string]JSONAttachment{"audio": attachment}} {
data, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
var wire map[string]interface{}
if err := json.Unmarshal(data, &wire); err != nil {
t.Fatal(err)
}
if nested, ok := wire["audio"]; ok {
wire = nested.(map[string]interface{})
}
if got := wire["duration_in_seconds"]; got != 1.5 {
t.Errorf("duration = %v, want 1.5 (%T)", got, value)
}
}
}
Loading