storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/lifecycle/noncurrentversion.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019-2020 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package lifecycle
    18  
    19  import (
    20  	"encoding/xml"
    21  )
    22  
    23  // NoncurrentVersionExpiration - an action for lifecycle configuration rule.
    24  type NoncurrentVersionExpiration struct {
    25  	XMLName        xml.Name       `xml:"NoncurrentVersionExpiration"`
    26  	NoncurrentDays ExpirationDays `xml:"NoncurrentDays,omitempty"`
    27  	set            bool
    28  }
    29  
    30  // MarshalXML if non-current days not set to non zero value
    31  func (n NoncurrentVersionExpiration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    32  	if n.IsDaysNull() {
    33  		return nil
    34  	}
    35  	type noncurrentVersionExpirationWrapper NoncurrentVersionExpiration
    36  	return e.EncodeElement(noncurrentVersionExpirationWrapper(n), start)
    37  }
    38  
    39  // UnmarshalXML decodes NoncurrentVersionExpiration
    40  func (n *NoncurrentVersionExpiration) UnmarshalXML(d *xml.Decoder, startElement xml.StartElement) error {
    41  	type noncurrentVersionExpirationWrapper NoncurrentVersionExpiration
    42  	var val noncurrentVersionExpirationWrapper
    43  	err := d.DecodeElement(&val, &startElement)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	*n = NoncurrentVersionExpiration(val)
    48  	n.set = true
    49  	return nil
    50  }
    51  
    52  // IsDaysNull returns true if days field is null
    53  func (n NoncurrentVersionExpiration) IsDaysNull() bool {
    54  	return n.NoncurrentDays == ExpirationDays(0)
    55  }
    56  
    57  // Validate returns an error with wrong value
    58  func (n NoncurrentVersionExpiration) Validate() error {
    59  	if !n.set {
    60  		return nil
    61  	}
    62  	val := int(n.NoncurrentDays)
    63  	if val <= 0 {
    64  		return errXMLNotWellFormed
    65  	}
    66  	return nil
    67  }
    68  
    69  // NoncurrentVersionTransition - an action for lifecycle configuration rule.
    70  type NoncurrentVersionTransition struct {
    71  	NoncurrentDays ExpirationDays `xml:"NoncurrentDays"`
    72  	StorageClass   string         `xml:"StorageClass"`
    73  	set            bool
    74  }
    75  
    76  // MarshalXML is extended to leave out
    77  // <NoncurrentVersionTransition></NoncurrentVersionTransition> tags
    78  func (n NoncurrentVersionTransition) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    79  	if n.NoncurrentDays == ExpirationDays(0) {
    80  		return nil
    81  	}
    82  	type noncurrentVersionTransitionWrapper NoncurrentVersionTransition
    83  	return e.EncodeElement(noncurrentVersionTransitionWrapper(n), start)
    84  }
    85  
    86  // IsDaysNull returns true if days field is null
    87  func (n NoncurrentVersionTransition) IsDaysNull() bool {
    88  	return n.NoncurrentDays == ExpirationDays(0)
    89  }
    90  
    91  // UnmarshalXML decodes NoncurrentVersionExpiration
    92  func (n *NoncurrentVersionTransition) UnmarshalXML(d *xml.Decoder, startElement xml.StartElement) error {
    93  	type noncurrentVersionTransitionWrapper NoncurrentVersionTransition
    94  	var val noncurrentVersionTransitionWrapper
    95  	err := d.DecodeElement(&val, &startElement)
    96  	if err != nil {
    97  		return err
    98  	}
    99  	*n = NoncurrentVersionTransition(val)
   100  	n.set = true
   101  	return nil
   102  }
   103  
   104  // Validate returns an error with wrong value
   105  func (n NoncurrentVersionTransition) Validate() error {
   106  	if !n.set {
   107  		return nil
   108  	}
   109  	if int(n.NoncurrentDays) <= 0 || n.StorageClass == "" {
   110  		return errXMLNotWellFormed
   111  	}
   112  	return nil
   113  }