gitlab.com/evatix-go/core@v1.3.55/coredata/corejson/newResultsCollectionCreator.go (about)

     1  package corejson
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/constants"
     5  )
     6  
     7  type newResultsCollectionCreator struct{}
     8  
     9  // UnmarshalUsingBytes
    10  //
    11  //  Aka. alias for DeserializeUsingBytes
    12  //
    13  //  Should be used when ResultsCollection itself is Serialized
    14  //  and save to somewhere and then unmarshal or deserialize
    15  func (it newResultsCollectionCreator) UnmarshalUsingBytes(
    16  	deserializingBytes []byte,
    17  ) (*ResultsCollection, error) {
    18  	return it.DeserializeUsingBytes(deserializingBytes)
    19  }
    20  
    21  // DeserializeUsingBytes
    22  //
    23  //  Should be used when ResultsCollection itself is Serialized
    24  //  and save to somewhere and then unmarshal or deserialize
    25  func (it newResultsCollectionCreator) DeserializeUsingBytes(
    26  	deserializingBytes []byte,
    27  ) (*ResultsCollection, error) {
    28  	empty := it.Empty()
    29  
    30  	err := Deserialize.
    31  		UsingBytes(deserializingBytes, empty)
    32  
    33  	if err == nil {
    34  		return empty, nil
    35  	}
    36  
    37  	return nil, err
    38  }
    39  
    40  func (it newResultsCollectionCreator) DeserializeUsingResult(
    41  	jsonResult *Result,
    42  ) (*ResultsCollection, error) {
    43  	if jsonResult.HasIssuesOrEmpty() {
    44  		return nil, jsonResult.MeaningfulError()
    45  	}
    46  
    47  	empty := it.Empty()
    48  
    49  	err := Deserialize.
    50  		UsingBytes(jsonResult.SafeBytes(), empty)
    51  
    52  	if err == nil {
    53  		return empty, nil
    54  	}
    55  
    56  	return nil, err
    57  }
    58  
    59  func (it newResultsCollectionCreator) Empty() *ResultsCollection {
    60  	return &ResultsCollection{
    61  		Items: []Result{},
    62  	}
    63  }
    64  
    65  func (it newResultsCollectionCreator) Default() *ResultsCollection {
    66  	list := make([]Result, 0, constants.Capacity8)
    67  
    68  	return &ResultsCollection{
    69  		Items: list,
    70  	}
    71  }
    72  
    73  func (it newResultsCollectionCreator) UsingCap(
    74  	capacity int,
    75  ) *ResultsCollection {
    76  	list := make([]Result, 0, capacity)
    77  
    78  	return &ResultsCollection{
    79  		Items: list,
    80  	}
    81  }
    82  
    83  func (it newResultsCollectionCreator) AnyItems(
    84  	anyItems ...interface{},
    85  ) *ResultsCollection {
    86  	list := make(
    87  		[]Result,
    88  		0,
    89  		len(anyItems)+constants.Capacity5)
    90  
    91  	collection := &ResultsCollection{
    92  		Items: list,
    93  	}
    94  
    95  	return collection.AddAnyItems(
    96  		anyItems...)
    97  }
    98  
    99  func (it newResultsCollectionCreator) AnyItemsPlusCap(
   100  	addCapacity int,
   101  	anyItems ...interface{},
   102  ) *ResultsCollection {
   103  	length := addCapacity
   104  
   105  	if len(anyItems) == 0 {
   106  		return it.UsingCap(length)
   107  	}
   108  
   109  	additionalCapacity := len(anyItems)
   110  	length += additionalCapacity
   111  	list := it.UsingCap(length)
   112  
   113  	return list.
   114  		AddAnyItems(anyItems...)
   115  }
   116  
   117  func (it newResultsCollectionCreator) UsingJsonersOption(
   118  	isIgnoreNilOrError bool,
   119  	addCapacity int,
   120  	jsoners ...Jsoner,
   121  ) *ResultsCollection {
   122  	length := addCapacity
   123  	if jsoners == nil {
   124  		return it.UsingCap(length)
   125  	}
   126  
   127  	actualLength := len(jsoners)
   128  	length += actualLength
   129  	list := it.UsingCap(length)
   130  
   131  	return list.
   132  		AddJsoners(
   133  			isIgnoreNilOrError,
   134  			jsoners...)
   135  }
   136  
   137  func (it newResultsCollectionCreator) UsingJsonersNonNull(
   138  	addCapacity int,
   139  	jsoners ...Jsoner,
   140  ) *ResultsCollection {
   141  	return it.UsingJsonersOption(
   142  		true,
   143  		addCapacity,
   144  		jsoners...)
   145  }
   146  
   147  func (it newResultsCollectionCreator) UsingJsoners(
   148  	jsoners ...Jsoner,
   149  ) *ResultsCollection {
   150  	return it.UsingJsonersOption(
   151  		true,
   152  		constants.Capacity2,
   153  		jsoners...)
   154  }
   155  
   156  func (it newResultsCollectionCreator) UsingResultsPtrPlusCap(
   157  	addCapacity int,
   158  	results ...*Result,
   159  ) *ResultsCollection {
   160  	length := addCapacity
   161  
   162  	if results == nil {
   163  		return it.UsingCap(length)
   164  	}
   165  
   166  	actualLength := len(results)
   167  	length += actualLength
   168  	list := it.UsingCap(length)
   169  
   170  	if actualLength == 0 {
   171  		return list
   172  	}
   173  
   174  	return list.
   175  		AddNonNilItemsPtr(results...)
   176  }
   177  
   178  func (it newResultsCollectionCreator) UsingResultsPtr(
   179  	results ...*Result,
   180  ) *ResultsCollection {
   181  	return it.UsingResultsPtrPlusCap(
   182  		constants.Capacity2,
   183  		results...)
   184  }
   185  
   186  func (it newResultsCollectionCreator) UsingResultsPlusCap(
   187  	addCapacity int,
   188  	results ...Result,
   189  ) *ResultsCollection {
   190  	length := addCapacity
   191  
   192  	if results == nil {
   193  		return it.UsingCap(length)
   194  	}
   195  
   196  	actualLength := len(results)
   197  	length += actualLength
   198  	list := it.UsingCap(length)
   199  
   200  	if actualLength == 0 {
   201  		return list
   202  	}
   203  
   204  	return list.
   205  		Adds(results...)
   206  }
   207  
   208  func (it newResultsCollectionCreator) UsingResults(
   209  	results ...Result,
   210  ) *ResultsCollection {
   211  	return it.UsingResultsPlusCap(
   212  		constants.Capacity2,
   213  		results...)
   214  }
   215  
   216  func (it newResultsCollectionCreator) Serializers(
   217  	serializers ...bytesSerializer,
   218  ) *ResultsCollection {
   219  	if len(serializers) == 0 {
   220  		return it.Empty()
   221  	}
   222  
   223  	collection := it.UsingCap(len(serializers))
   224  
   225  	return collection.AddSerializers(
   226  		serializers...)
   227  }
   228  
   229  func (it newResultsCollectionCreator) SerializerFunctions(
   230  	serializerFunctions ...func() ([]byte, error),
   231  ) *ResultsCollection {
   232  	if len(serializerFunctions) == 0 {
   233  		return it.Empty()
   234  	}
   235  
   236  	collection := it.UsingCap(len(serializerFunctions))
   237  
   238  	return collection.AddSerializerFunctions(
   239  		serializerFunctions...)
   240  }