You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package s3api
  2. import (
  3. "encoding/xml"
  4. "time"
  5. )
  6. // Status represents lifecycle configuration status
  7. type ruleStatus string
  8. // Supported status types
  9. const (
  10. Enabled ruleStatus = "Enabled"
  11. Disabled ruleStatus = "Disabled"
  12. )
  13. // Lifecycle - Configuration for bucket lifecycle.
  14. type Lifecycle struct {
  15. XMLName xml.Name `xml:"LifecycleConfiguration"`
  16. Rules []Rule `xml:"Rule"`
  17. }
  18. // Rule - a rule for lifecycle configuration.
  19. type Rule struct {
  20. XMLName xml.Name `xml:"Rule"`
  21. ID string `xml:"ID,omitempty"`
  22. Status ruleStatus `xml:"Status"`
  23. Filter Filter `xml:"Filter,omitempty"`
  24. Prefix Prefix `xml:"Prefix,omitempty"`
  25. Expiration Expiration `xml:"Expiration,omitempty"`
  26. Transition Transition `xml:"Transition,omitempty"`
  27. }
  28. // Filter - a filter for a lifecycle configuration Rule.
  29. type Filter struct {
  30. XMLName xml.Name `xml:"Filter"`
  31. set bool
  32. Prefix Prefix
  33. And And
  34. andSet bool
  35. Tag Tag
  36. tagSet bool
  37. }
  38. // Prefix holds the prefix xml tag in <Rule> and <Filter>
  39. type Prefix struct {
  40. string
  41. set bool
  42. }
  43. // MarshalXML - decodes XML data.
  44. func (p Prefix) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  45. if !p.set {
  46. return nil
  47. }
  48. return e.EncodeElement(p.string, startElement)
  49. }
  50. func (f Filter) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  51. if err := e.EncodeToken(start); err != nil {
  52. return err
  53. }
  54. if err := e.EncodeElement(f.Prefix, xml.StartElement{Name: xml.Name{Local: "Prefix"}}); err != nil {
  55. return err
  56. }
  57. return e.EncodeToken(xml.EndElement{Name: start.Name})
  58. }
  59. // And - a tag to combine a prefix and multiple tags for lifecycle configuration rule.
  60. type And struct {
  61. XMLName xml.Name `xml:"And"`
  62. Prefix Prefix `xml:"Prefix,omitempty"`
  63. Tags []Tag `xml:"Tag,omitempty"`
  64. }
  65. // Expiration - expiration actions for a rule in lifecycle configuration.
  66. type Expiration struct {
  67. XMLName xml.Name `xml:"Expiration"`
  68. Days int `xml:"Days,omitempty"`
  69. Date ExpirationDate `xml:"Date,omitempty"`
  70. DeleteMarker ExpireDeleteMarker `xml:"ExpiredObjectDeleteMarker"`
  71. set bool
  72. }
  73. // MarshalXML encodes expiration field into an XML form.
  74. func (e Expiration) MarshalXML(enc *xml.Encoder, startElement xml.StartElement) error {
  75. if !e.set {
  76. return nil
  77. }
  78. type expirationWrapper Expiration
  79. return enc.EncodeElement(expirationWrapper(e), startElement)
  80. }
  81. // ExpireDeleteMarker represents value of ExpiredObjectDeleteMarker field in Expiration XML element.
  82. type ExpireDeleteMarker struct {
  83. val bool
  84. set bool
  85. }
  86. // MarshalXML encodes delete marker boolean into an XML form.
  87. func (b ExpireDeleteMarker) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  88. if !b.set {
  89. return nil
  90. }
  91. return e.EncodeElement(b.val, startElement)
  92. }
  93. // ExpirationDate is a embedded type containing time.Time to unmarshal
  94. // Date in Expiration
  95. type ExpirationDate struct {
  96. time.Time
  97. }
  98. // MarshalXML encodes expiration date if it is non-zero and encodes
  99. // empty string otherwise
  100. func (eDate ExpirationDate) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
  101. if eDate.Time.IsZero() {
  102. return nil
  103. }
  104. return e.EncodeElement(eDate.Format(time.RFC3339), startElement)
  105. }
  106. // Transition - transition actions for a rule in lifecycle configuration.
  107. type Transition struct {
  108. XMLName xml.Name `xml:"Transition"`
  109. Days int `xml:"Days,omitempty"`
  110. Date time.Time `xml:"Date,omitempty"`
  111. StorageClass string `xml:"StorageClass,omitempty"`
  112. set bool
  113. }
  114. // MarshalXML encodes transition field into an XML form.
  115. func (t Transition) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
  116. if !t.set {
  117. return nil
  118. }
  119. type transitionWrapper Transition
  120. return enc.EncodeElement(transitionWrapper(t), start)
  121. }
  122. // TransitionDays is a type alias to unmarshal Days in Transition
  123. type TransitionDays int