gitlab.com/evatix-go/core@v1.3.55/coredata/corepayload/newPayloadsCollectionCreator.go (about)

     1  package corepayload
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/constants"
     5  	"gitlab.com/evatix-go/core/coredata/corejson"
     6  	"gitlab.com/evatix-go/core/errcore"
     7  )
     8  
     9  type newPayloadsCollectionCreator struct{}
    10  
    11  func (it newPayloadsCollectionCreator) Empty() *PayloadsCollection {
    12  	return &PayloadsCollection{
    13  		Items: []*PayloadWrapper{},
    14  	}
    15  }
    16  
    17  func (it newPayloadsCollectionCreator) Deserialize(
    18  	rawBytes []byte,
    19  ) (*PayloadsCollection, error) {
    20  	empty := it.Empty()
    21  
    22  	err := corejson.
    23  		Deserialize.
    24  		UsingBytes(
    25  			rawBytes, empty)
    26  
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	return empty, nil
    32  }
    33  
    34  func (it newPayloadsCollectionCreator) DeserializeMust(
    35  	rawBytes []byte,
    36  ) *PayloadsCollection {
    37  	collection, err := it.Deserialize(rawBytes)
    38  	errcore.HandleErr(err)
    39  
    40  	return collection
    41  }
    42  
    43  func (it newPayloadsCollectionCreator) DeserializeToMany(
    44  	rawBytes []byte,
    45  ) (payloadsSlice []*PayloadsCollection, err error) {
    46  	err = corejson.
    47  		Deserialize.
    48  		UsingBytes(
    49  			rawBytes,
    50  			&payloadsSlice)
    51  
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	return payloadsSlice, nil
    57  }
    58  
    59  func (it newPayloadsCollectionCreator) DeserializeUsingJsonResult(
    60  	jsonResult *corejson.Result,
    61  ) (*PayloadsCollection, error) {
    62  	empty := it.Empty()
    63  
    64  	err := corejson.
    65  		Deserialize.
    66  		Apply(jsonResult, empty)
    67  
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	return empty, nil
    73  }
    74  
    75  func (it newPayloadsCollectionCreator) UsingCap(
    76  	capacity int,
    77  ) *PayloadsCollection {
    78  	return &PayloadsCollection{
    79  		Items: make([]*PayloadWrapper, 0, capacity),
    80  	}
    81  }
    82  
    83  func (it newPayloadsCollectionCreator) UsingWrappers(
    84  	payloadsWrappers ...*PayloadWrapper,
    85  ) *PayloadsCollection {
    86  	if len(payloadsWrappers) == 0 {
    87  		return it.Empty()
    88  	}
    89  
    90  	collection := it.UsingCap(
    91  		len(payloadsWrappers) +
    92  			constants.Capacity3)
    93  
    94  	return collection.AddsPtr(payloadsWrappers...)
    95  }