gitlab.com/evatix-go/core@v1.3.55/chmodhelper/Attribute.go (about)

     1  package chmodhelper
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/conditional"
     5  	"gitlab.com/evatix-go/core/constants"
     6  )
     7  
     8  type Attribute struct {
     9  	IsRead    bool
    10  	IsWrite   bool
    11  	IsExecute bool
    12  }
    13  
    14  func (it *Attribute) IsNull() bool {
    15  	return it == nil
    16  }
    17  
    18  func (it *Attribute) IsAnyNull() bool {
    19  	return it == nil
    20  }
    21  
    22  func (it *Attribute) IsEmpty() bool {
    23  	return it == nil ||
    24  		!it.IsRead &&
    25  			!it.IsWrite &&
    26  			!it.IsExecute
    27  }
    28  
    29  func (it *Attribute) IsZero() bool {
    30  	return it.IsEmpty()
    31  }
    32  
    33  func (it *Attribute) IsInvalid() bool {
    34  	return it.IsEmpty()
    35  }
    36  
    37  func (it *Attribute) IsDefined() bool {
    38  	return !it.IsEmpty()
    39  }
    40  
    41  func (it *Attribute) HasAnyItem() bool {
    42  	return !it.IsEmpty()
    43  }
    44  
    45  func (it *Attribute) ToAttributeValue() AttributeValue {
    46  	read, write, exe, sum := it.ToSpecificBytes()
    47  
    48  	return AttributeValue{
    49  		Read:    read,
    50  		Write:   write,
    51  		Execute: exe,
    52  		Sum:     sum,
    53  	}
    54  }
    55  
    56  func (it *Attribute) ToSpecificBytes() (read, write, exe, sum byte) {
    57  	read = conditional.Byte(it.IsRead, ReadValue, constants.Zero)
    58  	write = conditional.Byte(it.IsWrite, WriteValue, constants.Zero)
    59  	exe = conditional.Byte(it.IsExecute, ExecuteValue, constants.Zero)
    60  
    61  	return read, write, exe, read + write + exe
    62  }
    63  
    64  // ToByte refers to the compiled byte value in between 0-7
    65  func (it *Attribute) ToByte() byte {
    66  	r := conditional.Byte(it.IsRead, ReadValue, constants.Zero)
    67  	w := conditional.Byte(it.IsWrite, WriteValue, constants.Zero)
    68  	e := conditional.Byte(it.IsExecute, ExecuteValue, constants.Zero)
    69  
    70  	return r + w + e
    71  }
    72  
    73  // ToSum refers to the compiled byte value in between 0-7
    74  func (it *Attribute) ToSum() byte {
    75  	return it.ToByte()
    76  }
    77  
    78  func (it *Attribute) ToRwx() [3]byte {
    79  	return [3]byte{
    80  		conditional.Byte(it.IsRead, ReadChar, constants.HyphenChar),
    81  		conditional.Byte(it.IsWrite, WriteChar, constants.HyphenChar),
    82  		conditional.Byte(it.IsExecute, ExecuteChar, constants.HyphenChar),
    83  	}
    84  }
    85  
    86  // ToRwxString returns "rwx"
    87  func (it *Attribute) ToRwxString() string {
    88  	rwxBytes := it.ToRwx()
    89  
    90  	return string(rwxBytes[:])
    91  }
    92  
    93  func (it *Attribute) ToVariant() AttrVariant {
    94  	b := it.ToByte()
    95  
    96  	return AttrVariant(b)
    97  }
    98  
    99  // ToStringByte returns the compiled byte value as Char byte value
   100  //
   101  // It is not restricted between 0-7 but 0-7 + char '0', which makes it string 0-7
   102  func (it *Attribute) ToStringByte() byte {
   103  	return it.ToByte() + constants.ZeroChar
   104  }
   105  
   106  func (it *Attribute) Clone() *Attribute {
   107  	if it == nil {
   108  		return nil
   109  	}
   110  
   111  	return &Attribute{
   112  		IsRead:    it.IsRead,
   113  		IsWrite:   it.IsWrite,
   114  		IsExecute: it.IsExecute,
   115  	}
   116  }
   117  
   118  func (it *Attribute) IsEqualPtr(next *Attribute) bool {
   119  	if it == nil && next == nil {
   120  		return true
   121  	}
   122  
   123  	if it == nil || next == nil {
   124  		return false
   125  	}
   126  
   127  	isRead := it.IsRead == next.IsRead
   128  	isWrite := it.IsWrite == next.IsWrite
   129  	isExecute := it.IsExecute == next.IsExecute
   130  
   131  	return isRead &&
   132  		isWrite &&
   133  		isExecute
   134  }
   135  
   136  func (it Attribute) IsEqual(next Attribute) bool {
   137  	isRead := it.IsRead == next.IsRead
   138  	isWrite := it.IsWrite == next.IsWrite
   139  	isExecute := it.IsExecute == next.IsExecute
   140  
   141  	return isRead &&
   142  		isWrite &&
   143  		isExecute
   144  }