gitlab.com/evatix-go/core@v1.3.55/coredata/coreonce/AnyOnce.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 AnyOnce struct {
    14  	innerData       interface{}
    15  	initializerFunc func() interface{}
    16  	compiledString  *string
    17  	isInitialized   bool
    18  }
    19  
    20  func NewAnyOnce(initializerFunc func() interface{}) AnyOnce {
    21  	return AnyOnce{
    22  		initializerFunc: initializerFunc,
    23  	}
    24  }
    25  
    26  func NewAnyOncePtr(initializerFunc func() interface{}) *AnyOnce {
    27  	return &AnyOnce{
    28  		initializerFunc: initializerFunc,
    29  	}
    30  }
    31  
    32  func (it *AnyOnce) Value() interface{} {
    33  	if it.isInitialized {
    34  		return it.innerData
    35  	}
    36  
    37  	it.innerData = it.initializerFunc()
    38  	it.isInitialized = true
    39  
    40  	return it.innerData
    41  }
    42  
    43  func (it *AnyOnce) Execute() interface{} {
    44  	return it.Value()
    45  }
    46  
    47  // ValueStringOnly
    48  //
    49  //  Usages SPrintf to get the string,
    50  //  mostly use the String() func to get the value
    51  func (it *AnyOnce) ValueStringOnly() (val string) {
    52  	return it.ValueString()
    53  }
    54  
    55  // SafeString
    56  //
    57  //  Usages SPrintf to get the string,
    58  //  mostly use the String() func to get the value
    59  func (it *AnyOnce) SafeString() (val string) {
    60  	return it.ValueStringOnly()
    61  }
    62  
    63  // ValueStringMust
    64  //
    65  //  Usages SPrintf to get the string,
    66  //  mostly use the String() func to get the value
    67  //
    68  //  Panic if error exist.
    69  func (it *AnyOnce) ValueStringMust() (val string) {
    70  	return it.ValueString()
    71  }
    72  
    73  // ValueString
    74  //
    75  //  Usages SPrintf to get the string,
    76  //  mostly use the String() func to get the value
    77  func (it *AnyOnce) ValueString() (val string) {
    78  	if it.compiledString != nil {
    79  		return *it.compiledString
    80  	}
    81  
    82  	valInf := it.Value()
    83  
    84  	if valInf == nil {
    85  		return constants.NilAngelBracket
    86  	}
    87  
    88  	toString := fmt.Sprintf(
    89  		constants.SprintPropertyNameValueFormat,
    90  		valInf)
    91  
    92  	it.compiledString = &toString
    93  
    94  	return *it.compiledString
    95  }
    96  
    97  func (it *AnyOnce) CastValueString() (
    98  	val string, isSuccess bool,
    99  ) {
   100  	valInf := it.Execute()
   101  	toString, isSuccess := valInf.(string)
   102  
   103  	return toString, isSuccess
   104  }
   105  
   106  func (it *AnyOnce) CastValueStrings() (
   107  	valueStrings []string, isSuccess bool,
   108  ) {
   109  	valInf := it.Execute()
   110  	toStrings, isSuccess := valInf.([]string)
   111  
   112  	return toStrings, isSuccess
   113  }
   114  
   115  func (it *AnyOnce) CastValueHashmapMap() (
   116  	valueMap map[string]string,
   117  	isSuccess bool,
   118  ) {
   119  	valInf := it.Execute()
   120  	toStrings, isSuccess := valInf.(map[string]string)
   121  
   122  	return toStrings, isSuccess
   123  }
   124  
   125  func (it *AnyOnce) CastValueMapStringAnyMap() (
   126  	valueMap map[string]interface{},
   127  	isSuccess bool,
   128  ) {
   129  	valInf := it.Execute()
   130  	toStrings, isSuccess := valInf.(map[string]interface{})
   131  
   132  	return toStrings, isSuccess
   133  }
   134  
   135  func (it *AnyOnce) CastValueBytes() (
   136  	rawBytes []byte,
   137  	isSuccess bool,
   138  ) {
   139  	valInf := it.Execute()
   140  	toStrings, isSuccess := valInf.([]byte)
   141  
   142  	return toStrings, isSuccess
   143  }
   144  
   145  func (it *AnyOnce) ValueOnly() interface{} {
   146  	return it.Value()
   147  }
   148  
   149  func (it *AnyOnce) IsInitialized() bool {
   150  	return it.isInitialized
   151  }
   152  
   153  func (it *AnyOnce) IsNull() bool {
   154  	return it.Value() == nil
   155  }
   156  
   157  func (it *AnyOnce) IsStringEmpty() bool {
   158  	return it.String() == ""
   159  }
   160  
   161  func (it *AnyOnce) IsStringEmptyOrWhitespace() bool {
   162  	return strings.TrimSpace(it.String()) == ""
   163  }
   164  
   165  func (it *AnyOnce) String() string {
   166  	if it.IsNull() {
   167  		return constants.EmptyString
   168  	}
   169  
   170  	return fmt.Sprintf(constants.SprintValueFormat, it.Value())
   171  }
   172  
   173  func (it *AnyOnce) Deserialize(toPtr interface{}) error {
   174  	allBytes, err := it.Serialize()
   175  
   176  	if err != nil {
   177  		return err
   178  	}
   179  
   180  	unmarshallErr := json.Unmarshal(allBytes, toPtr)
   181  
   182  	if err == nil {
   183  		return err
   184  	}
   185  
   186  	var safeString string
   187  	if len(allBytes) > 0 {
   188  		safeString = string(allBytes)
   189  	}
   190  
   191  	var typeSafeName string
   192  	if toPtr != nil {
   193  		typeSafeName = reflect.TypeOf(toPtr).String()
   194  	}
   195  
   196  	message :=
   197  		"deserializing failed: " + unmarshallErr.Error() +
   198  			", json payload:" + safeString +
   199  			", type: " + typeSafeName
   200  
   201  	// has err
   202  	return errors.New(message)
   203  }
   204  
   205  func (it *AnyOnce) Serialize() ([]byte, error) {
   206  	value := it.Value()
   207  	allBytes, marshalErr := json.Marshal(value)
   208  
   209  	if marshalErr == nil {
   210  		return allBytes, nil
   211  	}
   212  
   213  	return nil, errors.New(
   214  		"unmarshalling error, " + marshalErr.Error() +
   215  			"value string : " + it.SafeString())
   216  }
   217  
   218  func (it *AnyOnce) SerializeSkipExistingError() ([]byte, error) {
   219  	return it.Serialize()
   220  }
   221  
   222  func (it *AnyOnce) SerializeMust() []byte {
   223  	value := it.Value()
   224  
   225  	jsonByes, err := json.Marshal(value)
   226  	if err != nil {
   227  		panic(err)
   228  	}
   229  
   230  	return jsonByes
   231  }