github.com/Files-com/files-sdk-go/v3@v3.1.81/file/status/status.go (about)

     1  package status
     2  
     3  type Status struct {
     4  	Name  string
     5  	Value int
     6  }
     7  
     8  func (e Status) Status() Status {
     9  	return e
    10  }
    11  
    12  type GetStatus interface {
    13  	Status() Status
    14  }
    15  
    16  var (
    17  	Null        = Status{"", -1}
    18  	Indexed     = Status{"indexed", 0}
    19  	Retrying    = Status{"retrying", 0}
    20  	Queued      = Status{"queued", 1}
    21  	Downloading = Status{"downloading", 2}
    22  	Uploading   = Status{"uploading", 2}
    23  	Skipped     = Status{"skipped", 3}
    24  	Ignored     = Status{"ignored", 3}
    25  	Complete    = Status{"complete", 4}
    26  	Canceled    = Status{"canceled", 5}
    27  	Errored     = Status{"errored", 5}
    28  
    29  	Included = []GetStatus{Indexed, Queued, Retrying, Downloading, Uploading, Complete, Canceled, Errored}
    30  	Excluded = []GetStatus{Skipped, Ignored}
    31  	Valid    = []GetStatus{Indexed, Queued, Retrying, Downloading, Uploading, Complete}
    32  	Invalid  = []GetStatus{Null, Canceled, Errored, Skipped, Ignored}
    33  	Running  = []GetStatus{Downloading, Uploading}
    34  	Ended    = []GetStatus{Complete, Canceled, Errored, Skipped, Ignored}
    35  )
    36  
    37  func (e Status) String() string {
    38  	return e.Name
    39  }
    40  
    41  func (e Status) Has(statuses ...GetStatus) bool {
    42  	return e.Any(statuses...)
    43  }
    44  
    45  func (e Status) Is(statuses ...GetStatus) bool {
    46  	return e.Any(statuses...)
    47  }
    48  
    49  func (e Status) IsNot(statuses ...GetStatus) bool {
    50  	return !e.Any(statuses...)
    51  }
    52  
    53  func (e Status) is(status GetStatus) bool {
    54  	return e.Name == status.Status().Name
    55  }
    56  
    57  func (e Status) Any(statuses ...GetStatus) bool {
    58  	if len(statuses) == 0 {
    59  		return true
    60  	}
    61  	for _, status := range statuses {
    62  		if e.is(status) {
    63  			return true
    64  		}
    65  	}
    66  	return false
    67  }
    68  
    69  func SetStatus(old GetStatus, new GetStatus, err error) (Status, bool) {
    70  	var setError bool
    71  	if err != nil || new.Status().Is(Retrying) {
    72  		setError = true
    73  	}
    74  	if old.Status().Is(Errored) && new.Status().Is(Running...) {
    75  		new = old
    76  	}
    77  
    78  	return new.Status(), setError
    79  }