gitlab.com/evatix-go/core@v1.3.55/coredata/coreonce/BytesErrorOnce.go (about)

     1  package coreonce
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"reflect"
     7  	"strings"
     8  
     9  	"gitlab.com/evatix-go/core/constants"
    10  )
    11  
    12  type BytesErrorOnce struct {
    13  	innerData       []byte
    14  	err             error
    15  	initializerFunc func() ([]byte, error)
    16  	isInitialized   bool
    17  }
    18  
    19  func NewBytesErrorOnce(initializerFunc func() ([]byte, error)) BytesErrorOnce {
    20  	return BytesErrorOnce{
    21  		initializerFunc: initializerFunc,
    22  	}
    23  }
    24  
    25  func NewBytesErrorOncePtr(initializerFunc func() ([]byte, error)) *BytesErrorOnce {
    26  	return &BytesErrorOnce{
    27  		initializerFunc: initializerFunc,
    28  	}
    29  }
    30  
    31  func (it *BytesErrorOnce) HandleError() {
    32  	err := it.Error()
    33  
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  }
    38  
    39  func (it *BytesErrorOnce) MustBeEmptyError() {
    40  	err := it.Error()
    41  
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  }
    46  
    47  func (it *BytesErrorOnce) MustHaveSafeItems() {
    48  	err := it.Error()
    49  
    50  	if err != nil {
    51  		panic(err)
    52  	}
    53  
    54  	if it.IsBytesEmpty() {
    55  		panic("values cannot be null or empty!")
    56  	}
    57  }
    58  
    59  // Error
    60  //
    61  //  Runs the execution and returns the error.
    62  func (it *BytesErrorOnce) Error() error {
    63  	if it.isInitialized {
    64  		return it.err
    65  	}
    66  
    67  	_, err := it.Value()
    68  
    69  	return err
    70  }
    71  
    72  func (it *BytesErrorOnce) HasError() bool {
    73  	return it.Error() != nil
    74  }
    75  
    76  func (it *BytesErrorOnce) HasIssuesOrEmpty() bool {
    77  	if it == nil {
    78  		return true
    79  	}
    80  
    81  	val, err := it.Value()
    82  
    83  	return err != nil || len(val) == 0
    84  }
    85  
    86  func (it *BytesErrorOnce) HasSafeItems() bool {
    87  	return !it.HasIssuesOrEmpty()
    88  }
    89  
    90  func (it *BytesErrorOnce) IsEmptyError() bool {
    91  	return it.Error() == nil
    92  }
    93  
    94  // IsEmpty
    95  //
    96  //  represent values and error both empty
    97  func (it *BytesErrorOnce) IsEmpty() bool {
    98  	return it == nil || it.IsNull() && it.IsEmptyError()
    99  }
   100  
   101  // IsEmptyBytes
   102  //
   103  //  represent values
   104  func (it *BytesErrorOnce) IsEmptyBytes() bool {
   105  	return it == nil || it.Length() == 0
   106  }
   107  
   108  func (it *BytesErrorOnce) Length() int {
   109  	return len(it.ValueOnly())
   110  }
   111  
   112  func (it *BytesErrorOnce) HasAnyItem() bool {
   113  	return it.Length() > 0
   114  }
   115  
   116  func (it *BytesErrorOnce) IsDefined() bool {
   117  	return !it.IsEmpty()
   118  }
   119  
   120  // IsInvalid
   121  //
   122  //  represents has error
   123  func (it *BytesErrorOnce) IsInvalid() bool {
   124  	return it.HasError()
   125  }
   126  
   127  // IsValid
   128  //
   129  //  represents empty error
   130  func (it *BytesErrorOnce) IsValid() bool {
   131  	return it.IsEmptyError()
   132  }
   133  
   134  // IsSuccess
   135  //
   136  //  represents empty error
   137  func (it *BytesErrorOnce) IsSuccess() bool {
   138  	return it.IsEmptyError()
   139  }
   140  
   141  // IsFailed
   142  //
   143  //  represents has error
   144  func (it *BytesErrorOnce) IsFailed() bool {
   145  	return !it.IsEmptyError()
   146  }
   147  
   148  func (it *BytesErrorOnce) Execute() ([]byte, error) {
   149  	return it.Value()
   150  }
   151  
   152  func (it *BytesErrorOnce) Deserialize(
   153  	toPtr interface{},
   154  ) error {
   155  	rawBytes, err := it.Value()
   156  	var valString string
   157  
   158  	if len(rawBytes) > 0 {
   159  		valString = string(rawBytes)
   160  	}
   161  
   162  	var typeNameString string
   163  
   164  	if toPtr != nil {
   165  		// actual nil-ness
   166  		// not required
   167  		typeNameString =
   168  			reflect.
   169  				TypeOf(toPtr).
   170  				String()
   171  	}
   172  
   173  	if err != nil {
   174  		return errors.New(
   175  			"existing error cannot deserialize, " +
   176  				err.Error() +
   177  				", payload : " + valString + "," +
   178  				" to type:" + typeNameString)
   179  	}
   180  
   181  	jsonUnmarshalErr := json.Unmarshal(rawBytes, toPtr)
   182  
   183  	if err == nil {
   184  		return nil
   185  	}
   186  
   187  	// has error
   188  	return errors.New(
   189  		"deserialize failed, " +
   190  			jsonUnmarshalErr.Error() +
   191  			", payload : " + valString + "," +
   192  			" to type:" + typeNameString)
   193  }
   194  
   195  func (it *BytesErrorOnce) DeserializeMust(
   196  	toPtr interface{},
   197  ) {
   198  	err := it.Deserialize(toPtr)
   199  
   200  	if err != nil {
   201  		panic(err)
   202  	}
   203  }
   204  
   205  func (it *BytesErrorOnce) ValueWithError() ([]byte, error) {
   206  	return it.Value()
   207  }
   208  
   209  func (it *BytesErrorOnce) Value() ([]byte, error) {
   210  	if it.isInitialized {
   211  		return it.innerData, it.err
   212  	}
   213  
   214  	it.innerData, it.err = it.initializerFunc()
   215  	it.isInitialized = true
   216  
   217  	return it.innerData, it.err
   218  }
   219  
   220  func (it *BytesErrorOnce) ValueOnly() []byte {
   221  	if it.isInitialized {
   222  		return it.innerData
   223  	}
   224  
   225  	val, _ := it.Value()
   226  
   227  	return val
   228  }
   229  
   230  func (it *BytesErrorOnce) IsInitialized() bool {
   231  	return it.isInitialized
   232  }
   233  
   234  func (it *BytesErrorOnce) IsBytesEmpty() bool {
   235  	return it.Length() == 0
   236  }
   237  
   238  func (it *BytesErrorOnce) IsNull() bool {
   239  	return it.ValueOnly() == nil
   240  }
   241  
   242  func (it *BytesErrorOnce) IsStringEmpty() bool {
   243  	return it.String() == ""
   244  }
   245  
   246  func (it *BytesErrorOnce) IsStringEmptyOrWhitespace() bool {
   247  	return strings.TrimSpace(it.String()) == ""
   248  }
   249  
   250  func (it *BytesErrorOnce) String() string {
   251  	if it.IsNull() {
   252  		return constants.EmptyString
   253  	}
   254  
   255  	return string(it.ValueOnly())
   256  }
   257  
   258  func (it *BytesErrorOnce) MarshalJSON() ([]byte, error) {
   259  	return it.Value()
   260  }
   261  
   262  func (it *BytesErrorOnce) Serialize() ([]byte, error) {
   263  	return it.Value()
   264  }
   265  
   266  func (it *BytesErrorOnce) SerializeMust() []byte {
   267  	allBytes, err := it.Serialize()
   268  
   269  	if err != nil {
   270  		panic(err)
   271  	}
   272  
   273  	return allBytes
   274  }