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

     1  package coreonce
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"strings"
     7  
     8  	"gitlab.com/evatix-go/core/constants"
     9  )
    10  
    11  type StringOnce struct {
    12  	innerData       string
    13  	initializerFunc func() string
    14  	isInitialized   bool
    15  }
    16  
    17  func NewStringOnce(initializerFunc func() string) StringOnce {
    18  	return StringOnce{
    19  		initializerFunc: initializerFunc,
    20  	}
    21  }
    22  
    23  func NewStringOncePtr(initializerFunc func() string) *StringOnce {
    24  	return &StringOnce{
    25  		initializerFunc: initializerFunc,
    26  	}
    27  }
    28  
    29  func (it *StringOnce) MarshalJSON() ([]byte, error) {
    30  	return json.Marshal(it.Value())
    31  }
    32  
    33  func (it *StringOnce) UnmarshalJSON(data []byte) error {
    34  	it.isInitialized = true
    35  
    36  	return json.Unmarshal(data, &it.innerData)
    37  }
    38  
    39  func (it *StringOnce) ValuePtr() *string {
    40  	val := it.Value()
    41  
    42  	return &val
    43  }
    44  
    45  func (it *StringOnce) Value() string {
    46  	if it.isInitialized {
    47  		return it.innerData
    48  	}
    49  
    50  	it.innerData = it.initializerFunc()
    51  	it.isInitialized = true
    52  
    53  	return it.innerData
    54  }
    55  
    56  func (it *StringOnce) Execute() string {
    57  	return it.Value()
    58  }
    59  
    60  func (it *StringOnce) IsEqual(equalString string) bool {
    61  	return it.Value() == equalString
    62  }
    63  
    64  func (it *StringOnce) HasPrefix(prefix string) bool {
    65  	return strings.HasPrefix(
    66  		it.Value(), prefix)
    67  }
    68  
    69  func (it *StringOnce) IsStartsWith(startsWith string) bool {
    70  	return strings.HasPrefix(
    71  		it.Value(), startsWith)
    72  }
    73  
    74  func (it *StringOnce) HasSuffix(suffix string) bool {
    75  	return strings.HasSuffix(
    76  		it.Value(), suffix)
    77  }
    78  
    79  func (it *StringOnce) IsEndsWith(
    80  	endsWith string,
    81  ) bool {
    82  	return strings.HasSuffix(
    83  		it.Value(), endsWith)
    84  }
    85  
    86  func (it *StringOnce) SplitBy(
    87  	splitter string,
    88  ) []string {
    89  	return strings.Split(it.Value(), splitter)
    90  }
    91  
    92  func (it *StringOnce) SplitLeftRightTrim(
    93  	splitter string,
    94  ) (left, right string) {
    95  	left, right = it.SplitLeftRight(splitter)
    96  
    97  	return strings.TrimSpace(left), strings.TrimSpace(right)
    98  }
    99  
   100  func (it *StringOnce) SplitLeftRight(
   101  	splitter string,
   102  ) (left, right string) {
   103  	items := strings.SplitN(
   104  		it.Value(),
   105  		splitter,
   106  		constants.Two)
   107  
   108  	if len(items) == 2 {
   109  		return items[0], items[1]
   110  	}
   111  
   112  	if len(items) > 2 {
   113  		return items[0], items[len(items)]
   114  	}
   115  
   116  	// len <= 1
   117  	return items[0], ""
   118  }
   119  
   120  func (it *StringOnce) IsContains(equalString string) bool {
   121  	return strings.Contains(it.Value(), equalString)
   122  }
   123  
   124  func (it *StringOnce) IsEmpty() bool {
   125  	return it.Value() == ""
   126  }
   127  
   128  func (it *StringOnce) IsEmptyOrWhitespace() bool {
   129  	return strings.TrimSpace(it.Value()) == ""
   130  }
   131  
   132  func (it *StringOnce) Bytes() []byte {
   133  	return []byte(it.Value())
   134  }
   135  
   136  func (it *StringOnce) Error() error {
   137  	return errors.New(it.Value())
   138  }
   139  
   140  func (it *StringOnce) String() string {
   141  	return it.Value()
   142  }
   143  
   144  func (it *StringOnce) Serialize() ([]byte, error) {
   145  	value := it.Value()
   146  
   147  	return json.Marshal(value)
   148  }