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

     1  package coreonce
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"reflect"
     8  	"strings"
     9  
    10  	"gitlab.com/evatix-go/core/constants"
    11  )
    12  
    13  type AnyErrorOnce struct {
    14  	innerData       interface{}
    15  	err             error
    16  	initializerFunc func() (interface{}, error)
    17  	compiledString  *string
    18  	isInitialized   bool
    19  }
    20  
    21  func NewAnyErrorOnce(initializerFunc func() (interface{}, error)) AnyErrorOnce {
    22  	return AnyErrorOnce{
    23  		initializerFunc: initializerFunc,
    24  	}
    25  }
    26  
    27  func NewAnyErrorOncePtr(initializerFunc func() (interface{}, error)) *AnyErrorOnce {
    28  	return &AnyErrorOnce{
    29  		initializerFunc: initializerFunc,
    30  	}
    31  }
    32  
    33  // Error
    34  //
    35  //  Runs the execution and returns the error.
    36  func (it *AnyErrorOnce) Error() error {
    37  	if it.isInitialized {
    38  		return it.err
    39  	}
    40  
    41  	_, err := it.Value()
    42  
    43  	return err
    44  }
    45  
    46  func (it *AnyErrorOnce) HasError() bool {
    47  	return it.Error() != nil
    48  }
    49  
    50  func (it *AnyErrorOnce) IsEmptyError() bool {
    51  	return it.Error() == nil
    52  }
    53  
    54  // IsEmpty
    55  //
    56  //  represent values and error both empty
    57  func (it *AnyErrorOnce) IsEmpty() bool {
    58  	return it == nil || it.IsNull() && it.IsEmptyError()
    59  }
    60  
    61  func (it *AnyErrorOnce) HasAnyItem() bool {
    62  	return !it.IsEmpty()
    63  }
    64  
    65  func (it *AnyErrorOnce) IsDefined() bool {
    66  	return !it.IsEmpty()
    67  }
    68  
    69  // IsInvalid
    70  //
    71  //  represents has error
    72  func (it *AnyErrorOnce) IsInvalid() bool {
    73  	return it.HasError()
    74  }
    75  
    76  // IsValid
    77  //
    78  //  represents empty error
    79  func (it *AnyErrorOnce) IsValid() bool {
    80  	return it.IsEmptyError()
    81  }
    82  
    83  // IsSuccess
    84  //
    85  //  represents empty error
    86  func (it *AnyErrorOnce) IsSuccess() bool {
    87  	return it.IsEmptyError()
    88  }
    89  
    90  // IsFailed
    91  //
    92  //  represents has error
    93  func (it *AnyErrorOnce) IsFailed() bool {
    94  	return !it.IsEmptyError()
    95  }
    96  
    97  func (it *AnyErrorOnce) ValueWithError() (interface{}, error) {
    98  	return it.Value()
    99  }
   100  
   101  func (it *AnyErrorOnce) Execute() (interface{}, error) {
   102  	return it.Value()
   103  }
   104  
   105  func (it *AnyErrorOnce) ExecuteMust() interface{} {
   106  	val, err := it.Value()
   107  
   108  	if err != nil {
   109  		panic(err)
   110  	}
   111  
   112  	return val
   113  }
   114  
   115  func (it *AnyErrorOnce) ValueMust() interface{} {
   116  	val, err := it.Value()
   117  
   118  	if err != nil {
   119  		panic(err)
   120  	}
   121  
   122  	return val
   123  }
   124  
   125  func (it *AnyErrorOnce) Value() (interface{}, error) {
   126  	if it.isInitialized {
   127  		return it.innerData, it.err
   128  	}
   129  
   130  	it.innerData, it.err = it.initializerFunc()
   131  	it.isInitialized = true
   132  
   133  	return it.innerData, it.err
   134  }
   135  
   136  // ValueStringOnly
   137  //
   138  //  Usages SPrintf to get the string,
   139  //  mostly use the String() func to get the value
   140  func (it *AnyErrorOnce) ValueStringOnly() (val string) {
   141  	val, _ = it.ValueString()
   142  
   143  	return val
   144  }
   145  
   146  // SafeString
   147  //
   148  //  Usages SPrintf to get the string,
   149  //  mostly use the String() func to get the value
   150  func (it *AnyErrorOnce) SafeString() (val string) {
   151  	return it.ValueStringOnly()
   152  }
   153  
   154  // ValueStringMust
   155  //
   156  //  Usages SPrintf to get the string,
   157  //  mostly use the String() func to get the value
   158  //
   159  //  Panic if error exist.
   160  func (it *AnyErrorOnce) ValueStringMust() (val string) {
   161  	val, err := it.ValueString()
   162  
   163  	if err != nil {
   164  		panic(err)
   165  	}
   166  
   167  	return val
   168  }
   169  
   170  // ValueString
   171  //
   172  //  Usages SPrintf to get the string,
   173  //  mostly use the String() func to get the value
   174  func (it *AnyErrorOnce) ValueString() (val string, err error) {
   175  	if it.compiledString != nil {
   176  		return *it.compiledString, it.err
   177  	}
   178  
   179  	valInf, err := it.Value()
   180  
   181  	if valInf == nil {
   182  		return constants.NilAngelBracket, err
   183  	}
   184  
   185  	toString := fmt.Sprintf(
   186  		constants.SprintPropertyNameValueFormat,
   187  		valInf)
   188  
   189  	it.compiledString = &toString
   190  
   191  	return *it.compiledString, err
   192  }
   193  
   194  func (it *AnyErrorOnce) CastValueString() (
   195  	val string, err error, isSuccess bool,
   196  ) {
   197  	valInf, err := it.Execute()
   198  	toString, isSuccess := valInf.(string)
   199  
   200  	return toString, err, isSuccess
   201  }
   202  
   203  func (it *AnyErrorOnce) CastValueStrings() (
   204  	valueStrings []string, err error, isSuccess bool,
   205  ) {
   206  	valInf, err := it.Execute()
   207  	toStrings, isSuccess := valInf.([]string)
   208  
   209  	return toStrings, err, isSuccess
   210  }
   211  
   212  func (it *AnyErrorOnce) CastValueHashmapMap() (
   213  	valueMap map[string]string,
   214  	err error,
   215  	isSuccess bool,
   216  ) {
   217  	valInf, err := it.Execute()
   218  	toStrings, isSuccess := valInf.(map[string]string)
   219  
   220  	return toStrings, err, isSuccess
   221  }
   222  
   223  func (it *AnyErrorOnce) CastValueMapStringAnyMap() (
   224  	valueMap map[string]interface{},
   225  	err error,
   226  	isSuccess bool,
   227  ) {
   228  	valInf, err := it.Execute()
   229  	toStrings, isSuccess := valInf.(map[string]interface{})
   230  
   231  	return toStrings, err, isSuccess
   232  }
   233  
   234  func (it *AnyErrorOnce) CastValueBytes() (
   235  	rawBytes []byte,
   236  	err error,
   237  	isSuccess bool,
   238  ) {
   239  	valInf, err := it.Execute()
   240  	toStrings, isSuccess := valInf.([]byte)
   241  
   242  	return toStrings, err, isSuccess
   243  }
   244  
   245  func (it *AnyErrorOnce) ValueOnly() interface{} {
   246  	if it.isInitialized {
   247  		return it.innerData
   248  	}
   249  
   250  	val, _ := it.Value()
   251  
   252  	return val
   253  }
   254  
   255  func (it *AnyErrorOnce) IsInitialized() bool {
   256  	return it.isInitialized
   257  }
   258  
   259  func (it *AnyErrorOnce) IsNull() bool {
   260  	return it.ValueOnly() == nil
   261  }
   262  
   263  func (it *AnyErrorOnce) IsStringEmpty() bool {
   264  	return it.String() == ""
   265  }
   266  
   267  func (it *AnyErrorOnce) IsStringEmptyOrWhitespace() bool {
   268  	return strings.TrimSpace(it.String()) == ""
   269  }
   270  
   271  func (it *AnyErrorOnce) String() string {
   272  	if it.IsNull() {
   273  		return constants.EmptyString
   274  	}
   275  
   276  	return fmt.Sprintf(
   277  		constants.SprintPropertyNameValueFormat,
   278  		it.ValueOnly())
   279  }
   280  
   281  func (it *AnyErrorOnce) Deserialize(toPtr interface{}) error {
   282  	allBytes, err := it.Serialize()
   283  
   284  	if err != nil {
   285  		return err
   286  	}
   287  
   288  	unmarshallErr := json.Unmarshal(allBytes, toPtr)
   289  
   290  	if err == nil {
   291  		return err
   292  	}
   293  
   294  	var safeString string
   295  	if len(allBytes) > 0 {
   296  		safeString = string(allBytes)
   297  	}
   298  
   299  	var typeSafeName string
   300  	if toPtr != nil {
   301  		typeSafeName = reflect.TypeOf(toPtr).String()
   302  	}
   303  
   304  	message :=
   305  		"deserializing failed: " + unmarshallErr.Error() +
   306  			", json payload:" + safeString +
   307  			", type: " + typeSafeName
   308  
   309  	// has err
   310  	return errors.New(message)
   311  }
   312  
   313  func (it *AnyErrorOnce) Serialize() ([]byte, error) {
   314  	value, err := it.Value()
   315  
   316  	if err != nil {
   317  		return nil, errors.New(
   318  			"cannot serialize on existing error, " + err.Error() +
   319  				"value string : " + it.SafeString())
   320  	}
   321  
   322  	allBytes, marshalErr := json.Marshal(value)
   323  
   324  	if marshalErr == nil {
   325  		return allBytes, nil
   326  	}
   327  
   328  	return nil, errors.New(
   329  		"unmarshalling error, " + marshalErr.Error() +
   330  			"value string : " + it.SafeString())
   331  }
   332  
   333  func (it *AnyErrorOnce) SerializeSkipExistingError() ([]byte, error) {
   334  	return json.Marshal(it.ValueOnly())
   335  }
   336  
   337  func (it *AnyErrorOnce) SerializeMust() []byte {
   338  	allBytes, err := it.Serialize()
   339  
   340  	if err != nil {
   341  		panic(err)
   342  	}
   343  
   344  	return allBytes
   345  }