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

     1  package coreinstruction
     2  
     3  import "gitlab.com/evatix-go/core/constants"
     4  
     5  type BaseRequestIds struct {
     6  	RequestIds []IdentifierWithIsGlobal `json:"RequestIds,omitempty"`
     7  }
     8  
     9  func NewBaseRequestIds(
    10  	isGlobal bool,
    11  	ids ...string,
    12  ) *BaseRequestIds {
    13  	return &BaseRequestIds{
    14  		RequestIds: *NewRequestIds(
    15  			isGlobal,
    16  			ids...),
    17  	}
    18  }
    19  
    20  func NewRequestIds(
    21  	isGlobal bool,
    22  	ids ...string,
    23  ) *[]IdentifierWithIsGlobal {
    24  	slice := make([]IdentifierWithIsGlobal, len(ids))
    25  	if len(ids) == 0 {
    26  		return &slice
    27  	}
    28  
    29  	for i, id := range ids {
    30  		slice[i] = IdentifierWithIsGlobal{
    31  			BaseIdentifier: BaseIdentifier{Id: id},
    32  			IsGlobal:       isGlobal,
    33  		}
    34  	}
    35  
    36  	return &slice
    37  }
    38  
    39  func NewRequestId(
    40  	isGlobal bool,
    41  	id string,
    42  ) *IdentifierWithIsGlobal {
    43  	return &IdentifierWithIsGlobal{
    44  		BaseIdentifier: BaseIdentifier{Id: id},
    45  		IsGlobal:       isGlobal,
    46  	}
    47  }
    48  
    49  func (b *BaseRequestIds) RequestIdsLength() int {
    50  	if b != nil && b.RequestIds != nil {
    51  		return constants.Zero
    52  	}
    53  
    54  	return len(b.RequestIds)
    55  }
    56  
    57  func (b *BaseRequestIds) AddReqId(
    58  	requestId IdentifierWithIsGlobal,
    59  ) *BaseRequestIds {
    60  	b.RequestIds = append(
    61  		b.RequestIds,
    62  		requestId)
    63  
    64  	return b
    65  }
    66  
    67  func (b *BaseRequestIds) AddIds(
    68  	isGlobal bool,
    69  	ids ...string,
    70  ) *BaseRequestIds {
    71  	if len(ids) == 0 {
    72  		return b
    73  	}
    74  
    75  	for _, id := range ids {
    76  		b.RequestIds = append(b.RequestIds, IdentifierWithIsGlobal{
    77  			BaseIdentifier: BaseIdentifier{id},
    78  			IsGlobal:       isGlobal,
    79  		})
    80  	}
    81  
    82  	return b
    83  }
    84  
    85  func (b *BaseRequestIds) IsEmptyRequestIds() bool {
    86  	return b.RequestIdsLength() == 0
    87  }
    88  
    89  func (b *BaseRequestIds) HasRequestIds() bool {
    90  	return b != nil && b.RequestIds != nil && len(b.RequestIds) > 0
    91  }
    92  
    93  func (b *BaseRequestIds) Clone() *BaseRequestIds {
    94  	if b == nil {
    95  		return nil
    96  	}
    97  
    98  	length := b.RequestIdsLength()
    99  	slice := make(
   100  		[]IdentifierWithIsGlobal,
   101  		length)
   102  
   103  	if length == 0 {
   104  		return &BaseRequestIds{
   105  			RequestIds: slice,
   106  		}
   107  	}
   108  
   109  	for i, reqId := range b.RequestIds {
   110  		slice[i] = IdentifierWithIsGlobal{
   111  			BaseIdentifier: BaseIdentifier{Id: reqId.Id},
   112  			IsGlobal:       reqId.IsGlobal,
   113  		}
   114  	}
   115  
   116  	return &BaseRequestIds{
   117  		RequestIds: slice,
   118  	}
   119  }