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

     1  package corejson
     2  
     3  type newBytesCollectionCreator struct{}
     4  
     5  // UnmarshalUsingBytes
     6  //
     7  //  Aka. alias for DeserializeUsingBytes
     8  //
     9  //  Should be used when ResultsPtrCollection itself is Serialized
    10  //  and save to somewhere and then unmarshal or deserialize
    11  func (it newBytesCollectionCreator) UnmarshalUsingBytes(
    12  	deserializingBytes []byte,
    13  ) (*BytesCollection, error) {
    14  	return it.DeserializeUsingBytes(deserializingBytes)
    15  }
    16  
    17  // DeserializeUsingBytes
    18  //
    19  //  Should be used when BytesCollection itself is Serialized
    20  //  and save to somewhere and then unmarshal or deserialize
    21  func (it newBytesCollectionCreator) DeserializeUsingBytes(
    22  	deserializingBytes []byte,
    23  ) (*BytesCollection, error) {
    24  	empty := it.Empty()
    25  
    26  	err := Deserialize.
    27  		UsingBytes(deserializingBytes, empty)
    28  
    29  	if err == nil {
    30  		return empty, nil
    31  	}
    32  
    33  	return nil, err
    34  }
    35  
    36  func (it newBytesCollectionCreator) DeserializeUsingResult(
    37  	jsonResult *Result,
    38  ) (*BytesCollection, error) {
    39  	if jsonResult.HasIssuesOrEmpty() {
    40  		return nil, jsonResult.MeaningfulError()
    41  	}
    42  
    43  	empty := it.Empty()
    44  
    45  	err := Deserialize.
    46  		UsingBytes(jsonResult.SafeBytes(), empty)
    47  
    48  	if err == nil {
    49  		return empty, nil
    50  	}
    51  
    52  	return nil, err
    53  }
    54  
    55  func (it newBytesCollectionCreator) Empty() *BytesCollection {
    56  	return it.UsingCap(0)
    57  }
    58  
    59  func (it newBytesCollectionCreator) UsingCap(
    60  	capacity int,
    61  ) *BytesCollection {
    62  	list := make([][]byte, 0, capacity)
    63  
    64  	return &BytesCollection{
    65  		Items: list,
    66  	}
    67  }
    68  
    69  func (it newBytesCollectionCreator) AnyItems(
    70  	anyItems ...interface{},
    71  ) (*BytesCollection, error) {
    72  	length := len(anyItems)
    73  	collection := it.UsingCap(length)
    74  	err := collection.AddAnyItems(
    75  		anyItems...)
    76  
    77  	return collection, err
    78  }
    79  
    80  func (it newBytesCollectionCreator) JsonersPlusCap(
    81  	isIgnoreNilOrErr bool,
    82  	capacity int,
    83  	jsoners ...Jsoner,
    84  ) *BytesCollection {
    85  	length := capacity + len(jsoners)
    86  
    87  	if length == 0 || len(jsoners) == 0 {
    88  		return it.UsingCap(length)
    89  	}
    90  
    91  	collection := it.UsingCap(length)
    92  
    93  	return collection.AddJsoners(
    94  		isIgnoreNilOrErr,
    95  		jsoners...)
    96  }
    97  
    98  func (it newBytesCollectionCreator) Jsoners(
    99  	jsoners ...Jsoner,
   100  ) *BytesCollection {
   101  	return it.JsonersPlusCap(
   102  		true,
   103  		0,
   104  		jsoners...)
   105  }
   106  
   107  func (it newBytesCollectionCreator) Serializers(
   108  	serializers ...bytesSerializer,
   109  ) *BytesCollection {
   110  	if len(serializers) == 0 {
   111  		return it.Empty()
   112  	}
   113  
   114  	collection := it.UsingCap(
   115  		len(serializers))
   116  
   117  	for _, serializer := range serializers {
   118  		collection.AddSerializer(serializer)
   119  	}
   120  
   121  	return collection
   122  }