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

     1  package coreonce
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"sort"
     7  	"strings"
     8  	"sync"
     9  
    10  	"gitlab.com/evatix-go/core/constants"
    11  	"gitlab.com/evatix-go/core/errcore"
    12  )
    13  
    14  type MapStringStringOnce struct {
    15  	innerData                                          map[string]string
    16  	initializerFunc                                    func() map[string]string
    17  	isInitialized                                      bool
    18  	compiledStrings                                    []string
    19  	allKeys, allValues, allKeysSorted, allValuesSorted []string
    20  	sync.Mutex
    21  }
    22  
    23  func NewMapStringStringOnce(initializerFunc func() map[string]string) MapStringStringOnce {
    24  	return MapStringStringOnce{
    25  		initializerFunc: initializerFunc,
    26  	}
    27  }
    28  
    29  func NewMapStringStringOncePtr(initializerFunc func() map[string]string) *MapStringStringOnce {
    30  	return &MapStringStringOnce{
    31  		initializerFunc: initializerFunc,
    32  	}
    33  }
    34  
    35  func (it *MapStringStringOnce) MarshalJSON() ([]byte, error) {
    36  	return json.Marshal(it.Value())
    37  }
    38  
    39  func (it *MapStringStringOnce) UnmarshalJSON(data []byte) error {
    40  	it.isInitialized = true
    41  
    42  	return json.Unmarshal(data, &it.innerData)
    43  }
    44  
    45  func (it *MapStringStringOnce) Strings() []string {
    46  	if it.compiledStrings != nil {
    47  		return it.compiledStrings
    48  	}
    49  
    50  	listMap := it.ItemsMap()
    51  
    52  	if len(listMap) == 0 {
    53  		return []string{}
    54  	}
    55  
    56  	allKeyValues := make([]string, len(listMap))
    57  	index := 0
    58  	for key, value := range listMap {
    59  		allKeyValues[index] = fmt.Sprintf(
    60  			constants.KeyValJsonFormat,
    61  			key,
    62  			value)
    63  
    64  		index++
    65  	}
    66  
    67  	it.compiledStrings = allKeyValues
    68  
    69  	return it.compiledStrings
    70  }
    71  
    72  func (it *MapStringStringOnce) List() map[string]string {
    73  	return it.Value()
    74  }
    75  
    76  func (it *MapStringStringOnce) ItemsMap() map[string]string {
    77  	return it.Value()
    78  }
    79  
    80  func (it *MapStringStringOnce) Values() map[string]string {
    81  	return it.Value()
    82  }
    83  
    84  func (it *MapStringStringOnce) ValuesPtr() *map[string]string {
    85  	values := it.Value()
    86  
    87  	return &values
    88  }
    89  
    90  func (it *MapStringStringOnce) Value() map[string]string {
    91  	if it.isInitialized == true {
    92  		return it.innerData
    93  	}
    94  
    95  	it.innerData = it.initializerFunc()
    96  	it.isInitialized = true
    97  
    98  	return it.innerData
    99  }
   100  
   101  func (it *MapStringStringOnce) Execute() map[string]string {
   102  	return it.Value()
   103  }
   104  
   105  func (it *MapStringStringOnce) Length() int {
   106  	values := it.Value()
   107  
   108  	if values == nil {
   109  		return 0
   110  	}
   111  
   112  	return len(values)
   113  }
   114  
   115  func (it *MapStringStringOnce) HasAnyItem() bool {
   116  	return !it.IsEmpty()
   117  }
   118  
   119  // IsEmpty returns true if zero
   120  func (it *MapStringStringOnce) IsEmpty() bool {
   121  	if it == nil || it.initializerFunc == nil {
   122  		return true
   123  	}
   124  
   125  	values := it.Value()
   126  
   127  	return values == nil || len(values) == 0
   128  }
   129  
   130  func (it *MapStringStringOnce) HasAll(searchTerms ...string) bool {
   131  	for _, term := range searchTerms {
   132  		if !it.IsContains(term) {
   133  			return false
   134  		}
   135  	}
   136  
   137  	return true
   138  }
   139  
   140  func (it *MapStringStringOnce) Has(search string) bool {
   141  	return it.IsContains(search)
   142  }
   143  
   144  func (it *MapStringStringOnce) IsContains(search string) bool {
   145  	itemsMap := it.Values()
   146  	_, has := itemsMap[search]
   147  
   148  	return has
   149  }
   150  
   151  func (it *MapStringStringOnce) IsMissing(search string) bool {
   152  	itemsMap := it.Values()
   153  	_, has := itemsMap[search]
   154  
   155  	return !has
   156  }
   157  
   158  func (it *MapStringStringOnce) GetValue(key string) (val string) {
   159  	itemsMap := it.Values()
   160  
   161  	return itemsMap[key]
   162  }
   163  
   164  func (it *MapStringStringOnce) GetValueWithStatus(key string) (val string, hasItem bool) {
   165  	itemsMap := it.Values()
   166  	val, hasItem = itemsMap[key]
   167  
   168  	return val, hasItem
   169  }
   170  
   171  func (it *MapStringStringOnce) AllKeys() []string {
   172  	if it.allKeys != nil {
   173  		return it.allKeys
   174  	}
   175  
   176  	listMap := it.ItemsMap()
   177  
   178  	if len(listMap) == 0 {
   179  		return []string{}
   180  	}
   181  
   182  	allKeys := make([]string, len(listMap))
   183  	index := 0
   184  	for key := range listMap {
   185  		allKeys[index] = key
   186  		index++
   187  	}
   188  
   189  	it.allKeys = allKeys
   190  
   191  	return it.allKeys
   192  }
   193  
   194  func (it *MapStringStringOnce) AllValues() []string {
   195  	if it.allValues != nil {
   196  		return it.allValues
   197  	}
   198  
   199  	listMap := it.ItemsMap()
   200  
   201  	if len(listMap) == 0 {
   202  		return []string{}
   203  	}
   204  
   205  	allValues := make([]string, len(listMap))
   206  	index := 0
   207  	for _, value := range listMap {
   208  		allValues[index] = value
   209  		index++
   210  	}
   211  
   212  	it.allValues = allValues
   213  
   214  	return it.allValues
   215  }
   216  
   217  func (it *MapStringStringOnce) AllKeysSorted() []string {
   218  	if it.allKeysSorted != nil {
   219  		return it.allKeysSorted
   220  	}
   221  
   222  	listMap := it.ItemsMap()
   223  	if len(listMap) == 0 {
   224  		return []string{}
   225  	}
   226  
   227  	allKeys := make([]string, len(listMap))
   228  	index := 0
   229  	for key := range listMap {
   230  		allKeys[index] = key
   231  		index++
   232  	}
   233  
   234  	sort.Strings(allKeys)
   235  	it.allKeysSorted = allKeys
   236  
   237  	return it.allKeysSorted
   238  }
   239  
   240  func (it *MapStringStringOnce) AllValuesSorted() []string {
   241  	if it.allValuesSorted != nil {
   242  		return it.allValuesSorted
   243  	}
   244  
   245  	listMap := it.ItemsMap()
   246  
   247  	if len(listMap) == 0 {
   248  		return []string{}
   249  	}
   250  
   251  	allValues := make([]string, len(listMap))
   252  	index := 0
   253  	for _, value := range listMap {
   254  		allValues[index] = value
   255  		index++
   256  	}
   257  
   258  	sort.Strings(allValues)
   259  	it.allValuesSorted = allValues
   260  
   261  	return it.allValuesSorted
   262  }
   263  
   264  func (it MapStringStringOnce) Serialize() ([]byte, error) {
   265  	values := it.Value()
   266  
   267  	return json.Marshal(values)
   268  }
   269  
   270  func (it *MapStringStringOnce) IsEqual(rightMap map[string]string) bool {
   271  	if it == nil && rightMap == nil {
   272  		return true
   273  	}
   274  
   275  	currentItems := it.Value()
   276  	if currentItems == nil && rightMap == nil {
   277  		return true
   278  	}
   279  
   280  	if currentItems == nil || rightMap == nil {
   281  		return false
   282  	}
   283  
   284  	if len(currentItems) != len(rightMap) {
   285  		return false
   286  	}
   287  
   288  	for leftKey, leftVal := range currentItems {
   289  		rightVal, hasRight := rightMap[leftKey]
   290  
   291  		if !hasRight {
   292  			return false
   293  		}
   294  
   295  		if leftVal != rightVal {
   296  			return false
   297  		}
   298  	}
   299  
   300  	return true
   301  }
   302  
   303  func (it MapStringStringOnce) JsonStringMust() string {
   304  	marshalledJsonBytes, err := it.MarshalJSON()
   305  
   306  	if err != nil {
   307  		errcore.MarshallingFailedType.
   308  			HandleUsingPanic(
   309  				"MapStringStringOnce failed to marshall."+err.Error(), it.innerData)
   310  
   311  	}
   312  
   313  	return string(marshalledJsonBytes)
   314  }
   315  
   316  func (it MapStringStringOnce) String() string {
   317  	return strings.Join(it.Strings(), constants.CommaUnixNewLine)
   318  }