gitlab.com/evatix-go/core@v1.3.55/internal/mapdiffinternal/MapStringAnyDiff.go (about) 1 package mapdiffinternal 2 3 import ( 4 "fmt" 5 "reflect" 6 "sort" 7 "strings" 8 9 "gitlab.com/evatix-go/core/constants" 10 ) 11 12 type MapStringAnyDiff map[string]interface{} 13 14 func (it *MapStringAnyDiff) Length() int { 15 if it == nil { 16 return 0 17 } 18 19 return len(*it) 20 } 21 22 func (it MapStringAnyDiff) IsEmpty() bool { 23 return it.Length() == 0 24 } 25 26 func (it MapStringAnyDiff) HasAnyItem() bool { 27 return it.Length() > 0 28 } 29 30 func (it MapStringAnyDiff) LastIndex() int { 31 return it.Length() - 1 32 } 33 34 func (it MapStringAnyDiff) AllKeysSorted() []string { 35 if it.IsEmpty() { 36 return []string{} 37 } 38 39 allKeys := make( 40 []string, 41 it.Length()) 42 43 index := 0 44 for key := range it { 45 allKeys[index] = key 46 index++ 47 } 48 49 sort.Strings(allKeys) 50 51 return allKeys 52 } 53 54 func (it MapStringAnyDiff) Raw() map[string]interface{} { 55 if it == nil { 56 return map[string]interface{}{} 57 } 58 59 return it 60 } 61 62 func (it *MapStringAnyDiff) HasAnyChanges( 63 isRegardlessType bool, 64 rightMap map[string]interface{}, 65 ) bool { 66 return !it.IsRawEqual( 67 isRegardlessType, 68 rightMap) 69 } 70 71 func (it *MapStringAnyDiff) IsRawEqual( 72 isRegardlessType bool, 73 rightMap map[string]interface{}, 74 ) bool { 75 if it == nil && rightMap == nil { 76 return true 77 } 78 79 if it == nil || rightMap == nil { 80 return false 81 } 82 83 if it.Length() != len(rightMap) { 84 return false 85 } 86 87 for key, leftValInf := range *it { 88 rightValInf, has := rightMap[key] 89 90 if !has { 91 return false 92 } 93 94 if it.isNotEqual( 95 isRegardlessType, 96 leftValInf, 97 rightValInf) { 98 return false 99 } 100 } 101 102 return true 103 } 104 105 func (it *MapStringAnyDiff) HashmapDiffUsingRaw( 106 isRegardlessType bool, 107 rightMap map[string]interface{}, 108 ) MapStringAnyDiff { 109 diffMap := it.DiffRaw( 110 isRegardlessType, 111 rightMap) 112 113 if len(diffMap) == 0 { 114 return map[string]interface{}{} 115 } 116 117 return diffMap 118 } 119 120 func (it *MapStringAnyDiff) DiffRaw( 121 isRegardlessType bool, 122 rightMap map[string]interface{}, 123 ) map[string]interface{} { 124 if it == nil && rightMap == nil { 125 return map[string]interface{}{} 126 } 127 128 if it == nil && rightMap != nil { 129 return rightMap 130 } 131 132 if it != nil && rightMap == nil { 133 return *it 134 } 135 136 length := it.Length() / 3 137 diffMap := make( 138 map[string]interface{}, 139 length) 140 141 for key, leftValInf := range *it { 142 rightValInf, has := rightMap[key] 143 144 if !has { 145 diffMap[key] = leftValInf 146 147 continue 148 } 149 150 if it.isNotEqual( 151 isRegardlessType, 152 leftValInf, 153 rightValInf) { 154 diffMap[key] = leftValInf 155 } 156 } 157 158 if len(diffMap) == 0 && it.Length() == len(rightMap) { 159 return diffMap 160 } 161 162 leftMap := *it 163 for rightKey, rightAnyVal := range rightMap { 164 _, hasDiff := diffMap[rightKey] 165 166 if hasDiff { 167 // already added 168 169 continue 170 } 171 172 leftVal, has := leftMap[rightKey] 173 174 if !has { 175 diffMap[rightKey] = rightAnyVal 176 177 continue 178 } 179 180 if it.isNotEqual( 181 isRegardlessType, 182 rightAnyVal, 183 leftVal) { 184 diffMap[rightKey] = rightAnyVal 185 } 186 } 187 188 return diffMap 189 } 190 191 func (it *MapStringAnyDiff) DiffJsonMessage( 192 isRegardlessType bool, 193 rightMap map[string]interface{}, 194 ) string { 195 diffMap := it.HashmapDiffUsingRaw( 196 isRegardlessType, rightMap) 197 198 if diffMap.Length() == 0 { 199 return "" 200 } 201 202 slice := it.ToStringsSliceOfDiffMap(diffMap) 203 compiledString := strings.Join( 204 slice, 205 constants.CommaUnixNewLine) 206 207 return fmt.Sprintf( 208 curlyWrapFormat, 209 compiledString) 210 } 211 212 func (it *MapStringAnyDiff) ToStringsSliceOfDiffMap( 213 diffMap map[string]interface{}, 214 ) (diffSlice []string) { 215 allKeys := MapStringAnyDiff(diffMap).AllKeysSorted() 216 slice := make([]string, len(diffMap)) 217 218 for index, key := range allKeys { 219 val := diffMap[key] 220 if isStringType(val) { 221 slice[index] = fmt.Sprintf( 222 constants.KeyValQuotationWrapJsonFormat, 223 key, 224 val) 225 226 continue 227 } 228 229 // not string 230 slice[index] = fmt.Sprintf( 231 constants.KeyStringValAnyWrapJsonFormat, 232 key, 233 val) 234 } 235 236 return slice 237 } 238 239 func (it *MapStringAnyDiff) ShouldDiffMessage( 240 isRegardlessType bool, 241 title string, 242 rightMap map[string]interface{}, 243 ) string { 244 diffMessage := it.DiffJsonMessage( 245 isRegardlessType, 246 rightMap) 247 248 if diffMessage == "" { 249 return "" 250 } 251 252 return fmt.Sprintf( 253 diffBetweenMapShouldBeMessageFormat, 254 title, 255 diffMessage) 256 } 257 258 func (it *MapStringAnyDiff) LogShouldDiffMessage( 259 isRegardlessType bool, 260 title string, 261 rightMap map[string]interface{}, 262 ) (diffMessage string) { 263 diffMessage = it.ShouldDiffMessage( 264 isRegardlessType, 265 title, 266 rightMap) 267 268 if diffMessage == "" { 269 return 270 } 271 272 fmt.Println(diffMessage) 273 274 return diffMessage 275 } 276 277 func (it *MapStringAnyDiff) isNotEqual( 278 isRegardlessType bool, 279 left, 280 right interface{}, 281 ) bool { 282 if isRegardlessType { 283 leftString := fmt.Sprintf( 284 constants.SprintPropertyNameValueFormat, 285 left) 286 rightString := fmt.Sprintf( 287 constants.SprintPropertyNameValueFormat, 288 right) 289 290 return leftString != rightString 291 } 292 293 return !reflect.DeepEqual(left, right) 294 }