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