Skip to content

Commit

Permalink
lifecycle: fix json encoding of Expiration action (#1692)
Browse files Browse the repository at this point in the history
  • Loading branch information
krisis committed Sep 6, 2022
1 parent da55d86 commit f028f20
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/lifecycle/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ type Expiration struct {
XMLName xml.Name `xml:"Expiration,omitempty" json:"-"`
Date ExpirationDate `xml:"Date,omitempty" json:"Date,omitempty"`
Days ExpirationDays `xml:"Days,omitempty" json:"Days,omitempty"`
DeleteMarker ExpireDeleteMarker `xml:"ExpiredObjectDeleteMarker,omitempty"`
DeleteMarker ExpireDeleteMarker `xml:"ExpiredObjectDeleteMarker,omitempty" json:"ExpiredObjectDeleteMarker,omitempty"`
}

// MarshalJSON customizes json encoding by removing empty day/date specification.
func (e Expiration) MarshalJSON() ([]byte, error) {
type expiration struct {
Date *ExpirationDate `json:"Date,omitempty"`
Days *ExpirationDays `json:"Days,omitempty"`
DeleteMarker ExpireDeleteMarker
Date *ExpirationDate `json:"Date,omitempty"`
Days *ExpirationDays `json:"Days,omitempty"`
DeleteMarker ExpireDeleteMarker `json:"ExpiredObjectDeleteMarker,omitempty"`
}

newexp := expiration{
Expand Down
34 changes: 34 additions & 0 deletions pkg/lifecycle/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package lifecycle

import (
"bytes"
"encoding/json"
"encoding/xml"
"testing"
Expand Down Expand Up @@ -201,6 +202,13 @@ func TestLifecycleJSONRoundtrip(t *testing.T) {
ID: "rule-5",
Status: "Enabled",
},
{
Expiration: Expiration{
DeleteMarker: true,
},
ID: "rule-6",
Status: "Enabled",
},
},
}

Expand All @@ -222,6 +230,9 @@ func TestLifecycleJSONRoundtrip(t *testing.T) {
if !lc.Rules[i].Transition.equals(got.Rules[i].Transition) {
t.Fatalf("expected %#v got %#v", lc.Rules[i].Transition, got.Rules[i].Transition)
}
if lc.Rules[i].Expiration != got.Rules[i].Expiration {
t.Fatalf("expected %#v got %#v", lc.Rules[i].Expiration, got.Rules[i].Expiration)
}
}
}

Expand Down Expand Up @@ -299,3 +310,26 @@ func (n NoncurrentVersionTransition) equals(m NoncurrentVersionTransition) bool
func (t Transition) equals(u Transition) bool {
return t.Days == u.Days && t.Date.Equal(u.Date.Time) && t.StorageClass == u.StorageClass
}

func TestExpiredObjectDeleteMarker(t *testing.T) {
expected := []byte(`{"Rules":[{"Expiration":{"ExpiredObjectDeleteMarker":true},"ID":"expired-object-delete-marker","Status":"Enabled"}]}`)
lc := Configuration{
Rules: []Rule{
{
Expiration: Expiration{
DeleteMarker: true,
},
ID: "expired-object-delete-marker",
Status: "Enabled",
},
},
}

got, err := json.Marshal(lc)
if err != nil {
t.Fatalf("Failed to marshal due to %v", err)
}
if !bytes.Equal(expected, got) {
t.Fatalf("Expected %s but got %s", expected, got)
}
}

0 comments on commit f028f20

Please sign in to comment.