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