gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/CollectionsOfCollectionPtr.go (about) 1 package corestr 2 3 import ( 4 "encoding/json" 5 "strings" 6 7 "gitlab.com/evatix-go/core/constants" 8 "gitlab.com/evatix-go/core/coredata/corejson" 9 ) 10 11 type CollectionsOfCollectionPtr struct { 12 items []*CollectionPtr 13 } 14 15 func (it *CollectionsOfCollectionPtr) AsJsonContractsBinder() corejson.JsonContractsBinder { 16 return it 17 } 18 19 func (it *CollectionsOfCollectionPtr) IsEmpty() bool { 20 return it == nil || it.items == nil || len(it.items) == 0 21 } 22 23 func (it *CollectionsOfCollectionPtr) HasItems() bool { 24 return it.Length() > 0 25 } 26 27 func (it *CollectionsOfCollectionPtr) HasAnyItem() bool { 28 return it.Length() > 0 29 } 30 31 func (it *CollectionsOfCollectionPtr) Length() int { 32 if it == nil || it.items == nil { 33 return 0 34 } 35 36 return len(it.items) 37 } 38 39 func (it *CollectionsOfCollectionPtr) AllIndividualItemsLength() int { 40 if it.IsEmpty() { 41 return 0 42 } 43 44 allLength := 0 45 46 for _, collection := range it.items { 47 if collection == nil || collection.IsEmpty() { 48 continue 49 } 50 51 allLength += collection.Length() 52 } 53 54 return allLength 55 } 56 57 func (it *CollectionsOfCollectionPtr) ItemsPtr() []*CollectionPtr { 58 return it.items 59 } 60 61 func (it *CollectionsOfCollectionPtr) Items() []*CollectionPtr { 62 return it.items 63 } 64 65 func (it *CollectionsOfCollectionPtr) ListPtr( 66 additionalCapacity int, 67 ) *[]string { 68 allLength := it.AllIndividualItemsLength() 69 list := make([]string, 0, allLength+additionalCapacity) 70 71 if allLength == 0 { 72 return &list 73 } 74 75 for _, collection := range it.items { 76 for _, s := range collection.ListPtr() { 77 list = append(list, *s) 78 } 79 } 80 81 return &list 82 } 83 84 func (it *CollectionsOfCollectionPtr) ToCollection() *Collection { 85 list := it.ListPtr(0) 86 87 return New.Collection.StringsPtr(list) 88 } 89 90 func (it *CollectionsOfCollectionPtr) AddStringsPtr( 91 addCapacity int, 92 stringsItems *[]string, 93 ) *CollectionsOfCollectionPtr { 94 if stringsItems == nil { 95 return it 96 } 97 98 return it.Adds( 99 New.CollectionPtr.StringsPtrPlusCap( 100 addCapacity, 101 stringsItems, 102 )) 103 } 104 105 func (it *CollectionsOfCollectionPtr) AddPointerStrings( 106 pointerStringsItems ...*string, 107 ) *CollectionsOfCollectionPtr { 108 if pointerStringsItems == nil { 109 return it 110 } 111 112 return it.Adds( 113 New.CollectionPtr.PointerStrings( 114 pointerStringsItems, 115 )) 116 } 117 118 func (it *CollectionsOfCollectionPtr) AddsStringsOfStrings( 119 addCapacity int, 120 stringsOfPointerStrings ...*[]string, 121 ) *CollectionsOfCollectionPtr { 122 if stringsOfPointerStrings == nil { 123 return it 124 } 125 126 for _, stringsPointer := range stringsOfPointerStrings { 127 it.AddStringsPtr( 128 addCapacity, 129 stringsPointer, 130 ) 131 } 132 133 return it 134 } 135 136 func (it *CollectionsOfCollectionPtr) AddsStringsOfPointerStrings( 137 addCapacity int, 138 stringsOfPointerStrings *[]*[]string, 139 ) *CollectionsOfCollectionPtr { 140 if stringsOfPointerStrings == nil { 141 return it 142 } 143 144 for _, stringsPointer := range *stringsOfPointerStrings { 145 it.AddStringsPtr( 146 addCapacity, 147 stringsPointer, 148 ) 149 } 150 151 return it 152 } 153 154 func (it *CollectionsOfCollectionPtr) Adds( 155 collections ...*CollectionPtr, 156 ) *CollectionsOfCollectionPtr { 157 if collections == nil { 158 return it 159 } 160 161 return it.AddCollections(&collections) 162 } 163 164 func (it *CollectionsOfCollectionPtr) AddCollections( 165 collections *[]*CollectionPtr, 166 ) *CollectionsOfCollectionPtr { 167 if collections == nil { 168 return it 169 } 170 171 for i := range *collections { 172 it.items = append( 173 it.items, 174 (*collections)[i]) 175 } 176 177 return it 178 } 179 180 func (it *CollectionsOfCollectionPtr) String() string { 181 list := make( 182 []string, 183 0, 184 it.Length()) 185 186 for i, collection := range it.items { 187 list = append( 188 list, 189 collection.SummaryString(i+1)) 190 } 191 192 return strings.Join( 193 list, 194 constants.DoubleNewLine) 195 } 196 197 func (it *CollectionsOfCollectionPtr) JsonModel() *CollectionsOfCollectionPtrModel { 198 return &CollectionsOfCollectionPtrModel{ 199 Items: it.items, 200 } 201 } 202 203 func (it *CollectionsOfCollectionPtr) JsonModelAny() interface{} { 204 return it.JsonModel() 205 } 206 207 func (it *CollectionsOfCollectionPtr) MarshalJSON() ([]byte, error) { 208 return json.Marshal(*it.JsonModel()) 209 } 210 211 func (it *CollectionsOfCollectionPtr) UnmarshalJSON(data []byte) error { 212 var dataModel CollectionsOfCollectionPtrModel 213 214 err := json.Unmarshal(data, &dataModel) 215 216 if err == nil { 217 it.items = dataModel.Items 218 } 219 220 return err 221 } 222 223 func (it CollectionsOfCollectionPtr) Json() corejson.Result { 224 return corejson.New(it) 225 } 226 227 func (it CollectionsOfCollectionPtr) JsonPtr() *corejson.Result { 228 return corejson.NewPtr(it) 229 } 230 231 //goland:noinspection GoLinterLocal 232 func (it *CollectionsOfCollectionPtr) ParseInjectUsingJson( 233 jsonResult *corejson.Result, 234 ) (*CollectionsOfCollectionPtr, error) { 235 err := jsonResult.Unmarshal(it) 236 237 if err != nil { 238 return Empty.CollectionsOfCollectionPtr(), err 239 } 240 241 return it, nil 242 } 243 244 // ParseInjectUsingJsonMust Panic if error 245 //goland:noinspection GoLinterLocal 246 func (it *CollectionsOfCollectionPtr) ParseInjectUsingJsonMust( 247 jsonResult *corejson.Result, 248 ) *CollectionsOfCollectionPtr { 249 newUsingJson, err := 250 it.ParseInjectUsingJson(jsonResult) 251 252 if err != nil { 253 panic(err) 254 } 255 256 return newUsingJson 257 } 258 259 func (it *CollectionsOfCollectionPtr) JsonParseSelfInject( 260 jsonResult *corejson.Result, 261 ) error { 262 _, err := it.ParseInjectUsingJson( 263 jsonResult, 264 ) 265 266 return err 267 } 268 269 func (it *CollectionsOfCollectionPtr) AsJsoner() corejson.Jsoner { 270 return it 271 } 272 273 func (it *CollectionsOfCollectionPtr) AsJsonParseSelfInjector() corejson.JsonParseSelfInjector { 274 return it 275 } 276 277 func (it *CollectionsOfCollectionPtr) AsJsonMarshaller() corejson.JsonMarshaller { 278 return it 279 }