gitlab.com/evatix-go/core@v1.3.55/coredata/coreonce/StringsOnce.go (about) 1 package coreonce 2 3 import ( 4 "encoding/json" 5 "sort" 6 "sync" 7 8 "gitlab.com/evatix-go/core/errcore" 9 "gitlab.com/evatix-go/core/internal/csvinternal" 10 ) 11 12 type StringsOnce struct { 13 innerData []string 14 mapOnce map[string]bool 15 initializerFunc func() []string 16 isInitialized bool 17 sortedValues []string 18 sync.Mutex 19 } 20 21 func NewStringsOnce(initializerFunc func() []string) StringsOnce { 22 return StringsOnce{ 23 initializerFunc: initializerFunc, 24 } 25 } 26 27 func NewStringsOncePtr(initializerFunc func() []string) *StringsOnce { 28 return &StringsOnce{ 29 initializerFunc: initializerFunc, 30 } 31 } 32 33 func (it *StringsOnce) MarshalJSON() ([]byte, error) { 34 return json.Marshal(it.Value()) 35 } 36 37 func (it *StringsOnce) UnmarshalJSON(data []byte) error { 38 it.isInitialized = true 39 40 return json.Unmarshal(data, &it.innerData) 41 } 42 43 func (it *StringsOnce) Strings() []string { 44 return it.Value() 45 } 46 47 func (it *StringsOnce) SafeStrings() []string { 48 if it.IsEmpty() { 49 return []string{} 50 } 51 52 return it.Value() 53 } 54 55 func (it *StringsOnce) List() []string { 56 return it.Value() 57 } 58 59 func (it *StringsOnce) Values() []string { 60 return it.Value() 61 } 62 63 func (it *StringsOnce) ValuesPtr() *[]string { 64 values := it.Value() 65 66 return &values 67 } 68 69 func (it *StringsOnce) Value() []string { 70 if it.isInitialized == true { 71 return it.innerData 72 } 73 74 it.innerData = it.initializerFunc() 75 it.isInitialized = true 76 77 return it.innerData 78 } 79 80 func (it *StringsOnce) Execute() []string { 81 return it.Value() 82 } 83 84 func (it *StringsOnce) Length() int { 85 values := it.Value() 86 87 if values == nil { 88 return 0 89 } 90 91 return len(values) 92 } 93 94 func (it *StringsOnce) HasAnyItem() bool { 95 return !it.IsEmpty() 96 } 97 98 // IsEmpty returns true if zero 99 func (it *StringsOnce) IsEmpty() bool { 100 if it == nil || it.initializerFunc == nil { 101 return true 102 } 103 104 values := it.Value() 105 106 return values == nil || len(values) == 0 107 } 108 109 func (it *StringsOnce) HasAll(searchTerms ...string) bool { 110 for _, term := range searchTerms { 111 if !it.IsContains(term) { 112 return false 113 } 114 } 115 116 return true 117 } 118 119 func (it *StringsOnce) UniqueMapLock() map[string]bool { 120 it.Lock() 121 defer it.Unlock() 122 123 return it.UniqueMap() 124 } 125 126 func (it *StringsOnce) UniqueMap() map[string]bool { 127 if it.mapOnce != nil { 128 return it.mapOnce 129 } 130 131 values := it.Values() 132 133 if values == nil { 134 return map[string]bool{} 135 } 136 137 hashset := make(map[string]bool, len(values)) 138 139 for _, item := range values { 140 hashset[item] = true 141 } 142 143 it.mapOnce = hashset 144 145 return it.mapOnce 146 } 147 148 func (it *StringsOnce) Has(search string) bool { 149 return it.IsContains(search) 150 } 151 152 func (it *StringsOnce) IsContains(search string) bool { 153 values := it.Values() 154 155 for _, s := range values { 156 if s == search { 157 return true 158 } 159 } 160 161 return false 162 } 163 164 func (it *StringsOnce) CsvLines() []string { 165 return csvinternal.StringsToCsvStringsDefault( 166 it.List()...) 167 } 168 169 func (it *StringsOnce) CsvOptions() string { 170 return csvinternal.StringsToStringDefault(it.Value()...) 171 } 172 173 func (it *StringsOnce) Csv() string { 174 return it.CsvOptions() 175 } 176 177 // Sorted 178 // 179 // Warning : Current values will be mutated, 180 // so better to make a clone of it. 181 func (it *StringsOnce) Sorted() []string { 182 if it.sortedValues != nil { 183 return it.sortedValues 184 } 185 186 it.sortedValues = it.Value() 187 sort.Strings(it.sortedValues) 188 189 return it.sortedValues 190 } 191 192 func (it *StringsOnce) RangesMap() map[string]int { 193 values := it.Value() 194 195 if len(values) == 0 { 196 return map[string]int{} 197 } 198 199 newMap := make(map[string]int, len(values)) 200 201 for i, value := range values { 202 newMap[value] = i 203 } 204 205 return newMap 206 } 207 208 func (it StringsOnce) Serialize() ([]byte, error) { 209 values := it.Value() 210 211 return json.Marshal(values) 212 } 213 214 func (it *StringsOnce) IsEqual(comparingItems ...string) bool { 215 if it == nil && comparingItems == nil { 216 return true 217 } 218 219 currentItems := it.Value() 220 if currentItems == nil && comparingItems == nil { 221 return true 222 } 223 224 if currentItems == nil || comparingItems == nil { 225 return false 226 } 227 228 if len(currentItems) != len(comparingItems) { 229 return false 230 } 231 232 for i, item := range currentItems { 233 if item != comparingItems[i] { 234 return false 235 } 236 } 237 238 return true 239 } 240 241 func (it StringsOnce) JsonStringMust() string { 242 marshalledJsonBytes, err := it.MarshalJSON() 243 244 if err != nil { 245 errcore.MarshallingFailedType. 246 HandleUsingPanic( 247 "StringsOnce failed to marshall."+err.Error(), it.innerData) 248 249 } 250 251 return string(marshalledJsonBytes) 252 } 253 254 func (it StringsOnce) String() string { 255 return it.Csv() 256 }