gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/CollectionsOfCollection.go (about)

     1  package corestr
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"sync"
     7  
     8  	"gitlab.com/evatix-go/core/constants"
     9  	"gitlab.com/evatix-go/core/converters"
    10  	"gitlab.com/evatix-go/core/coredata/corejson"
    11  )
    12  
    13  type CollectionsOfCollection struct {
    14  	items []*Collection
    15  	sync.Mutex
    16  }
    17  
    18  func (it *CollectionsOfCollection) AsJsonContractsBinder() corejson.JsonContractsBinder {
    19  	return it
    20  }
    21  
    22  func (it *CollectionsOfCollection) IsEmpty() bool {
    23  	return it.items == nil || len(it.items) == 0
    24  }
    25  
    26  func (it *CollectionsOfCollection) HasItems() bool {
    27  	return it.items != nil && len(it.items) > 0
    28  }
    29  
    30  func (it *CollectionsOfCollection) Length() int {
    31  	if it.items == nil {
    32  		return 0
    33  	}
    34  
    35  	return len(it.items)
    36  }
    37  
    38  func (it *CollectionsOfCollection) AllIndividualItemsLength() int {
    39  	if it.IsEmpty() {
    40  		return 0
    41  	}
    42  
    43  	allLength := 0
    44  
    45  	for _, collection := range it.items {
    46  		if collection == nil || collection.IsEmpty() {
    47  			continue
    48  		}
    49  
    50  		allLength += collection.Length()
    51  	}
    52  
    53  	return allLength
    54  }
    55  
    56  func (it *CollectionsOfCollection) ItemsPtr() *[]*Collection {
    57  	return &it.items
    58  }
    59  
    60  func (it *CollectionsOfCollection) Items() []*Collection {
    61  	return it.items
    62  }
    63  
    64  func (it *CollectionsOfCollection) ListPtr(additionalCapacity int) *[]string {
    65  	allLength := it.AllIndividualItemsLength()
    66  	list := make([]string, 0, allLength+additionalCapacity)
    67  
    68  	if allLength == 0 {
    69  		return &list
    70  	}
    71  
    72  	for _, collection := range it.items {
    73  
    74  		for _, s := range *collection.ListPtr() {
    75  			list = append(list, s)
    76  		}
    77  	}
    78  
    79  	return &list
    80  }
    81  
    82  func (it *CollectionsOfCollection) ToCollection() *Collection {
    83  	list := it.ListPtr(0)
    84  
    85  	return New.Collection.StringsPtr(list)
    86  }
    87  
    88  func (it *CollectionsOfCollection) AddStringsPtr(
    89  	isCloneAdd bool,
    90  	stringsItems *[]string,
    91  ) *CollectionsOfCollection {
    92  	if stringsItems == nil || len(*stringsItems) == 0 {
    93  		return it
    94  	}
    95  
    96  	return it.Adds(New.Collection.StringsOptions(isCloneAdd, *stringsItems))
    97  }
    98  
    99  func (it *CollectionsOfCollection) AddPointerStringsPtr(
   100  	pointerStringsItems *[]*string,
   101  ) *CollectionsOfCollection {
   102  	if pointerStringsItems == nil {
   103  		return it
   104  	}
   105  
   106  	stringsItems := converters.PointerStringsToStrings(pointerStringsItems)
   107  
   108  	return it.Adds(New.Collection.StringsOptions(false, *stringsItems))
   109  }
   110  
   111  func (it *CollectionsOfCollection) AddsStringsOfStrings(
   112  	isMakeClone bool,
   113  	stringsOfPointerStrings ...*[]string,
   114  ) *CollectionsOfCollection {
   115  	if stringsOfPointerStrings == nil {
   116  		return it
   117  	}
   118  
   119  	for _, stringsPointer := range stringsOfPointerStrings {
   120  		it.AddStringsPtr(isMakeClone, stringsPointer)
   121  	}
   122  
   123  	return it
   124  }
   125  
   126  func (it *CollectionsOfCollection) AddsStringsOfPointerStrings(
   127  	isMakeClone bool,
   128  	stringsOfPointerStrings *[]*[]string,
   129  ) *CollectionsOfCollection {
   130  	if stringsOfPointerStrings == nil {
   131  		return it
   132  	}
   133  
   134  	for _, stringsPointer := range *stringsOfPointerStrings {
   135  		it.AddStringsPtr(isMakeClone, stringsPointer)
   136  	}
   137  
   138  	return it
   139  }
   140  
   141  // AddAsyncFuncItems must add all the lengths to the wg
   142  func (it *CollectionsOfCollection) AddAsyncFuncItems(
   143  	wg *sync.WaitGroup,
   144  	isMakeClone bool,
   145  	asyncFunctions ...func() []string,
   146  ) *CollectionsOfCollection {
   147  	if asyncFunctions == nil {
   148  		return it
   149  	}
   150  
   151  	asyncFuncWrap := func(asyncFunc func() []string) {
   152  		items := asyncFunc()
   153  
   154  		if len(items) == 0 {
   155  			wg.Done()
   156  
   157  			return
   158  		}
   159  
   160  		it.Lock()
   161  		it.AddStringsPtr(
   162  			isMakeClone,
   163  			&items,
   164  		)
   165  		it.Unlock()
   166  
   167  		wg.Done()
   168  	}
   169  
   170  	for _, function := range asyncFunctions {
   171  		go asyncFuncWrap(function)
   172  	}
   173  
   174  	wg.Wait()
   175  
   176  	return it
   177  }
   178  
   179  // AddAsyncFuncItemsPointer must add all the lengths to the wg
   180  func (it *CollectionsOfCollection) AddAsyncFuncItemsPointer(
   181  	wg *sync.WaitGroup,
   182  	isMakeClone bool,
   183  	asyncFunctions ...func() *[]string,
   184  ) *CollectionsOfCollection {
   185  	if asyncFunctions == nil {
   186  		return it
   187  	}
   188  
   189  	asyncFuncWrap := func(asyncFunc func() *[]string) {
   190  		items := asyncFunc()
   191  
   192  		if items == nil || len(*items) == 0 {
   193  			wg.Done()
   194  
   195  			return
   196  		}
   197  
   198  		it.Lock()
   199  		it.AddStringsPtr(
   200  			isMakeClone,
   201  			items,
   202  		)
   203  		it.Unlock()
   204  
   205  		wg.Done()
   206  	}
   207  
   208  	for _, function := range asyncFunctions {
   209  		go asyncFuncWrap(function)
   210  	}
   211  
   212  	wg.Wait()
   213  
   214  	return it
   215  }
   216  
   217  func (it *CollectionsOfCollection) Adds(
   218  	collections ...*Collection,
   219  ) *CollectionsOfCollection {
   220  	if collections == nil {
   221  		return it
   222  	}
   223  
   224  	return it.AddCollections(&collections)
   225  }
   226  
   227  func (it *CollectionsOfCollection) AddCollections(
   228  	collections *[]*Collection,
   229  ) *CollectionsOfCollection {
   230  	if collections == nil {
   231  		return it
   232  	}
   233  
   234  	for i := range *collections {
   235  		it.items = append(it.items, (*collections)[i])
   236  	}
   237  
   238  	return it
   239  }
   240  
   241  func (it *CollectionsOfCollection) String() string {
   242  	list := make(
   243  		[]string,
   244  		0,
   245  		it.Length())
   246  
   247  	for i, collection := range it.items {
   248  		list = append(
   249  			list,
   250  			collection.SummaryString(i+1))
   251  	}
   252  
   253  	return strings.Join(
   254  		list,
   255  		constants.DoubleNewLine)
   256  }
   257  
   258  func (it *CollectionsOfCollection) JsonModel() *CollectionsOfCollectionModel {
   259  	return &CollectionsOfCollectionModel{
   260  		Items: it.items,
   261  	}
   262  }
   263  
   264  func (it *CollectionsOfCollection) JsonModelAny() interface{} {
   265  	return it.JsonModel()
   266  }
   267  
   268  func (it *CollectionsOfCollection) MarshalJSON() ([]byte, error) {
   269  	return json.Marshal(*it.JsonModel())
   270  }
   271  
   272  func (it *CollectionsOfCollection) UnmarshalJSON(data []byte) error {
   273  	var dataModel CollectionsOfCollectionModel
   274  
   275  	err := json.Unmarshal(data, &dataModel)
   276  
   277  	if err == nil {
   278  		it.items = dataModel.Items
   279  	}
   280  
   281  	return err
   282  }
   283  
   284  func (it CollectionsOfCollection) Json() corejson.Result {
   285  	return corejson.New(it)
   286  }
   287  
   288  func (it CollectionsOfCollection) JsonPtr() *corejson.Result {
   289  	return corejson.NewPtr(it)
   290  }
   291  
   292  //goland:noinspection GoLinterLocal
   293  func (it *CollectionsOfCollection) ParseInjectUsingJson(
   294  	jsonResult *corejson.Result,
   295  ) (*CollectionsOfCollection, error) {
   296  	err := jsonResult.Unmarshal(it)
   297  
   298  	if err != nil {
   299  		return Empty.CollectionsOfCollection(), err
   300  	}
   301  
   302  	return it, nil
   303  }
   304  
   305  // ParseInjectUsingJsonMust Panic if error
   306  //goland:noinspection GoLinterLocal
   307  func (it *CollectionsOfCollection) ParseInjectUsingJsonMust(
   308  	jsonResult *corejson.Result,
   309  ) *CollectionsOfCollection {
   310  	newUsingJson, err :=
   311  		it.ParseInjectUsingJson(jsonResult)
   312  
   313  	if err != nil {
   314  		panic(err)
   315  	}
   316  
   317  	return newUsingJson
   318  }
   319  
   320  func (it *CollectionsOfCollection) JsonParseSelfInject(
   321  	jsonResult *corejson.Result,
   322  ) error {
   323  	_, err := it.ParseInjectUsingJson(
   324  		jsonResult,
   325  	)
   326  
   327  	return err
   328  }
   329  
   330  func (it *CollectionsOfCollection) AsJsoner() corejson.Jsoner {
   331  	return it
   332  }
   333  
   334  func (it *CollectionsOfCollection) AsJsonParseSelfInjector() corejson.JsonParseSelfInjector {
   335  	return it
   336  }
   337  
   338  func (it *CollectionsOfCollection) AsJsonMarshaller() corejson.JsonMarshaller {
   339  	return it
   340  }