gitlab.com/evatix-go/core@v1.3.55/coreinstruction/IdentifiersWithGlobals.go (about)

     1  package coreinstruction
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/constants"
     5  )
     6  
     7  type IdentifiersWithGlobals struct {
     8  	IdentifierWithIsGlobals []IdentifierWithIsGlobal `json:"IdentifierWithIsGlobals"`
     9  }
    10  
    11  func EmptyIdentifiersWithGlobals() *IdentifiersWithGlobals {
    12  	return &IdentifiersWithGlobals{
    13  		IdentifierWithIsGlobals: []IdentifierWithIsGlobal{},
    14  	}
    15  }
    16  
    17  func NewIdentifiersWithGlobals(
    18  	isGlobal bool,
    19  	ids ...string,
    20  ) *IdentifiersWithGlobals {
    21  	slice := make(
    22  		[]IdentifierWithIsGlobal,
    23  		len(ids))
    24  
    25  	if len(ids) == 0 {
    26  		return &IdentifiersWithGlobals{
    27  			IdentifierWithIsGlobals: slice,
    28  		}
    29  	}
    30  
    31  	for i, id := range ids {
    32  		slice[i] = IdentifierWithIsGlobal{
    33  			BaseIdentifier: BaseIdentifier{
    34  				Id: id,
    35  			},
    36  			IsGlobal: isGlobal,
    37  		}
    38  	}
    39  
    40  	return &IdentifiersWithGlobals{
    41  		IdentifierWithIsGlobals: slice,
    42  	}
    43  }
    44  
    45  func (receiver *IdentifiersWithGlobals) Length() int {
    46  	return len(receiver.IdentifierWithIsGlobals)
    47  }
    48  
    49  func (receiver *IdentifiersWithGlobals) IsEmpty() bool {
    50  	return receiver.Length() == 0
    51  }
    52  
    53  func (receiver *IdentifiersWithGlobals) IndexOf(id string) int {
    54  	if id == constants.EmptyString || receiver.IsEmpty() {
    55  		return constants.InvalidNotFoundCase
    56  	}
    57  
    58  	for index, identifierWithIsGlobal := range receiver.IdentifierWithIsGlobals {
    59  		if identifierWithIsGlobal.Id == id {
    60  			return index
    61  		}
    62  	}
    63  
    64  	return constants.InvalidNotFoundCase
    65  }
    66  
    67  func (receiver *IdentifiersWithGlobals) GetById(id string) *IdentifierWithIsGlobal {
    68  	if id == constants.EmptyString || receiver.IsEmpty() {
    69  		return nil
    70  	}
    71  
    72  	for _, identifierWithIsGlobal := range receiver.IdentifierWithIsGlobals {
    73  		if identifierWithIsGlobal.Id == id {
    74  			return &identifierWithIsGlobal
    75  		}
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func (receiver *IdentifiersWithGlobals) Add(
    82  	isGlobal bool,
    83  	id string,
    84  ) *IdentifiersWithGlobals {
    85  	if id == constants.EmptyString {
    86  		return receiver
    87  	}
    88  
    89  	receiver.IdentifierWithIsGlobals = append(
    90  		receiver.IdentifierWithIsGlobals,
    91  		*NewIdentifierWithIsGlobal(id, isGlobal))
    92  
    93  	return receiver
    94  }
    95  
    96  func (receiver *IdentifiersWithGlobals) Adds(
    97  	isGlobal bool,
    98  	ids ...string,
    99  ) *IdentifiersWithGlobals {
   100  	if len(ids) == 0 {
   101  		return receiver
   102  	}
   103  
   104  	for _, id := range ids {
   105  		receiver.IdentifierWithIsGlobals = append(
   106  			receiver.IdentifierWithIsGlobals,
   107  			*NewIdentifierWithIsGlobal(id, isGlobal))
   108  	}
   109  
   110  	return receiver
   111  }
   112  
   113  func (receiver *IdentifiersWithGlobals) HasAnyItem() bool {
   114  	return receiver.Length() > 0
   115  }
   116  
   117  func (receiver *IdentifiersWithGlobals) Clone() *IdentifiersWithGlobals {
   118  	length := receiver.Length()
   119  
   120  	slice := make(
   121  		[]IdentifierWithIsGlobal,
   122  		length)
   123  
   124  	if length == 0 {
   125  		return &IdentifiersWithGlobals{
   126  			IdentifierWithIsGlobals: slice,
   127  		}
   128  	}
   129  
   130  	for i, idWithIsGlobal := range receiver.IdentifierWithIsGlobals {
   131  		slice[i] = *idWithIsGlobal.Clone()
   132  	}
   133  
   134  	return &IdentifiersWithGlobals{
   135  		IdentifierWithIsGlobals: slice,
   136  	}
   137  }