| 1 |
package formatter |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"sort" |
| 8 |
|
| 9 |
diff "github.com/yudai/gojsondiff" |
| 10 |
) |
| 11 |
|
| 12 |
func NewAsciiFormatter(left interface{}, config AsciiFormatterConfig) *AsciiFormatter { |
| 13 |
return &AsciiFormatter{ |
| 14 |
left: left, |
| 15 |
config: config, |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
type AsciiFormatter struct { |
| 20 |
left interface{} |
| 21 |
config AsciiFormatterConfig |
| 22 |
buffer *bytes.Buffer |
| 23 |
path []string |
| 24 |
size []int |
| 25 |
inArray []bool |
| 26 |
line *AsciiLine |
| 27 |
} |
| 28 |
|
| 29 |
type AsciiFormatterConfig struct { |
| 30 |
ShowArrayIndex bool |
| 31 |
Coloring bool |
| 32 |
} |
| 33 |
|
| 34 |
var AsciiFormatterDefaultConfig = AsciiFormatterConfig{} |
| 35 |
|
| 36 |
type AsciiLine struct { |
| 37 |
marker string |
| 38 |
indent int |
| 39 |
buffer *bytes.Buffer |
| 40 |
} |
| 41 |
|
| 42 |
func (f *AsciiFormatter) Format(diff diff.Diff) (result string, err error) { |
| 43 |
f.buffer = bytes.NewBuffer([]byte{}) |
| 44 |
f.path = []string{} |
| 45 |
f.size = []int{} |
| 46 |
f.inArray = []bool{} |
| 47 |
|
| 48 |
if v, ok := f.left.(map[string]interface{}); ok { |
| 49 |
f.formatObject(v, diff) |
| 50 |
} else if v, ok := f.left.([]interface{}); ok { |
| 51 |
f.formatArray(v, diff) |
| 52 |
} else { |
| 53 |
return "", fmt.Errorf("expected map[string]interface{} or []interface{}, got %T", |
| 54 |
f.left) |
| 55 |
} |
| 56 |
|
| 57 |
return f.buffer.String(), nil |
| 58 |
} |
| 59 |
|
| 60 |
func (f *AsciiFormatter) formatObject(left map[string]interface{}, df diff.Diff) { |
| 61 |
f.addLineWith(AsciiSame, "{") |
| 62 |
f.push("ROOT", len(left), false) |
| 63 |
f.processObject(left, df.Deltas()) |
| 64 |
f.pop() |
| 65 |
f.addLineWith(AsciiSame, "}") |
| 66 |
} |
| 67 |
|
| 68 |
func (f *AsciiFormatter) formatArray(left []interface{}, df diff.Diff) { |
| 69 |
f.addLineWith(AsciiSame, "[") |
| 70 |
f.push("ROOT", len(left), true) |
| 71 |
f.processArray(left, df.Deltas()) |
| 72 |
f.pop() |
| 73 |
f.addLineWith(AsciiSame, "]") |
| 74 |
} |
| 75 |
|
| 76 |
func (f *AsciiFormatter) processArray(array []interface{}, deltas []diff.Delta) error { |
| 77 |
patchedIndex := 0 |
| 78 |
for index, value := range array { |
| 79 |
f.processItem(value, deltas, diff.Index(index)) |
| 80 |
patchedIndex++ |
| 81 |
} |
| 82 |
|
| 83 |
// additional Added |
| 84 |
for _, delta := range deltas { |
| 85 |
switch delta.(type) { |
| 86 |
case *diff.Added: |
| 87 |
d := delta.(*diff.Added) |
| 88 |
// skip items already processed |
| 89 |
if int(d.Position.(diff.Index)) < len(array) { |
| 90 |
continue |
| 91 |
} |
| 92 |
f.printRecursive(d.Position.String(), d.Value, AsciiAdded) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
func (f *AsciiFormatter) processObject(object map[string]interface{}, deltas []diff.Delta) error { |
| 100 |
names := sortedKeys(object) |
| 101 |
for _, name := range names { |
| 102 |
value := object[name] |
| 103 |
f.processItem(value, deltas, diff.Name(name)) |
| 104 |
} |
| 105 |
|
| 106 |
// Added |
| 107 |
for _, delta := range deltas { |
| 108 |
switch delta.(type) { |
| 109 |
case *diff.Added: |
| 110 |
d := delta.(*diff.Added) |
| 111 |
f.printRecursive(d.Position.String(), d.Value, AsciiAdded) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return nil |
| 116 |
} |
| 117 |
|
| 118 |
func (f *AsciiFormatter) processItem(value interface{}, deltas []diff.Delta, position diff.Position) error { |
| 119 |
matchedDeltas := f.searchDeltas(deltas, position) |
| 120 |
positionStr := position.String() |
| 121 |
if len(matchedDeltas) > 0 { |
| 122 |
for _, matchedDelta := range matchedDeltas { |
| 123 |
|
| 124 |
switch matchedDelta.(type) { |
| 125 |
case *diff.Object: |
| 126 |
d := matchedDelta.(*diff.Object) |
| 127 |
switch value.(type) { |
| 128 |
case map[string]interface{}: |
| 129 |
//ok |
| 130 |
default: |
| 131 |
return errors.New("Type mismatch") |
| 132 |
} |
| 133 |
o := value.(map[string]interface{}) |
| 134 |
|
| 135 |
f.newLine(AsciiSame) |
| 136 |
f.printKey(positionStr) |
| 137 |
f.print("{") |
| 138 |
f.closeLine() |
| 139 |
f.push(positionStr, len(o), false) |
| 140 |
f.processObject(o, d.Deltas) |
| 141 |
f.pop() |
| 142 |
f.newLine(AsciiSame) |
| 143 |
f.print("}") |
| 144 |
f.printComma() |
| 145 |
f.closeLine() |
| 146 |
|
| 147 |
case *diff.Array: |
| 148 |
d := matchedDelta.(*diff.Array) |
| 149 |
switch value.(type) { |
| 150 |
case []interface{}: |
| 151 |
//ok |
| 152 |
default: |
| 153 |
return errors.New("Type mismatch") |
| 154 |
} |
| 155 |
a := value.([]interface{}) |
| 156 |
|
| 157 |
f.newLine(AsciiSame) |
| 158 |
f.printKey(positionStr) |
| 159 |
f.print("[") |
| 160 |
f.closeLine() |
| 161 |
f.push(positionStr, len(a), true) |
| 162 |
f.processArray(a, d.Deltas) |
| 163 |
f.pop() |
| 164 |
f.newLine(AsciiSame) |
| 165 |
f.print("]") |
| 166 |
f.printComma() |
| 167 |
f.closeLine() |
| 168 |
|
| 169 |
case *diff.Added: |
| 170 |
d := matchedDelta.(*diff.Added) |
| 171 |
f.printRecursive(positionStr, d.Value, AsciiAdded) |
| 172 |
f.size[len(f.size)-1]++ |
| 173 |
|
| 174 |
case *diff.Modified: |
| 175 |
d := matchedDelta.(*diff.Modified) |
| 176 |
savedSize := f.size[len(f.size)-1] |
| 177 |
f.printRecursive(positionStr, d.OldValue, AsciiDeleted) |
| 178 |
f.size[len(f.size)-1] = savedSize |
| 179 |
f.printRecursive(positionStr, d.NewValue, AsciiAdded) |
| 180 |
|
| 181 |
case *diff.TextDiff: |
| 182 |
savedSize := f.size[len(f.size)-1] |
| 183 |
d := matchedDelta.(*diff.TextDiff) |
| 184 |
f.printRecursive(positionStr, d.OldValue, AsciiDeleted) |
| 185 |
f.size[len(f.size)-1] = savedSize |
| 186 |
f.printRecursive(positionStr, d.NewValue, AsciiAdded) |
| 187 |
|
| 188 |
case *diff.Deleted: |
| 189 |
d := matchedDelta.(*diff.Deleted) |
| 190 |
f.printRecursive(positionStr, d.Value, AsciiDeleted) |
| 191 |
|
| 192 |
default: |
| 193 |
return errors.New("Unknown Delta type detected") |
| 194 |
} |
| 195 |
|
| 196 |
} |
| 197 |
} else { |
| 198 |
f.printRecursive(positionStr, value, AsciiSame) |
| 199 |
} |
| 200 |
|
| 201 |
return nil |
| 202 |
} |
| 203 |
|
| 204 |
func (f *AsciiFormatter) searchDeltas(deltas []diff.Delta, position diff.Position) (results []diff.Delta) { |
| 205 |
results = make([]diff.Delta, 0) |
| 206 |
for _, delta := range deltas { |
| 207 |
switch delta.(type) { |
| 208 |
case diff.PostDelta: |
| 209 |
if delta.(diff.PostDelta).PostPosition() == position { |
| 210 |
results = append(results, delta) |
| 211 |
} |
| 212 |
case diff.PreDelta: |
| 213 |
if delta.(diff.PreDelta).PrePosition() == position { |
| 214 |
results = append(results, delta) |
| 215 |
} |
| 216 |
default: |
| 217 |
panic("heh") |
| 218 |
} |
| 219 |
} |
| 220 |
return |
| 221 |
} |
| 222 |
|
| 223 |
const ( |
| 224 |
AsciiSame = " " |
| 225 |
AsciiAdded = "+" |
| 226 |
AsciiDeleted = "-" |
| 227 |
) |
| 228 |
|
| 229 |
var AsciiStyles = map[string]string{ |
| 230 |
AsciiAdded: "30;42", |
| 231 |
AsciiDeleted: "30;41", |
| 232 |
} |
| 233 |
|
| 234 |
func (f *AsciiFormatter) push(name string, size int, array bool) { |
| 235 |
f.path = append(f.path, name) |
| 236 |
f.size = append(f.size, size) |
| 237 |
f.inArray = append(f.inArray, array) |
| 238 |
} |
| 239 |
|
| 240 |
func (f *AsciiFormatter) pop() { |
| 241 |
f.path = f.path[0 : len(f.path)-1] |
| 242 |
f.size = f.size[0 : len(f.size)-1] |
| 243 |
f.inArray = f.inArray[0 : len(f.inArray)-1] |
| 244 |
} |
| 245 |
|
| 246 |
func (f *AsciiFormatter) addLineWith(marker string, value string) { |
| 247 |
f.line = &AsciiLine{ |
| 248 |
marker: marker, |
| 249 |
indent: len(f.path), |
| 250 |
buffer: bytes.NewBufferString(value), |
| 251 |
} |
| 252 |
f.closeLine() |
| 253 |
} |
| 254 |
|
| 255 |
func (f *AsciiFormatter) newLine(marker string) { |
| 256 |
f.line = &AsciiLine{ |
| 257 |
marker: marker, |
| 258 |
indent: len(f.path), |
| 259 |
buffer: bytes.NewBuffer([]byte{}), |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
func (f *AsciiFormatter) closeLine() { |
| 264 |
style, ok := AsciiStyles[f.line.marker] |
| 265 |
if f.config.Coloring && ok { |
| 266 |
f.buffer.WriteString("\x1b[" + style + "m") |
| 267 |
} |
| 268 |
|
| 269 |
f.buffer.WriteString(f.line.marker) |
| 270 |
for n := 0; n < f.line.indent; n++ { |
| 271 |
f.buffer.WriteString(" ") |
| 272 |
} |
| 273 |
f.buffer.Write(f.line.buffer.Bytes()) |
| 274 |
|
| 275 |
if f.config.Coloring && ok { |
| 276 |
f.buffer.WriteString("\x1b[0m") |
| 277 |
} |
| 278 |
|
| 279 |
f.buffer.WriteRune('\n') |
| 280 |
} |
| 281 |
|
| 282 |
func (f *AsciiFormatter) printKey(name string) { |
| 283 |
if !f.inArray[len(f.inArray)-1] { |
| 284 |
fmt.Fprintf(f.line.buffer, `"%s": `, name) |
| 285 |
} else if f.config.ShowArrayIndex { |
| 286 |
fmt.Fprintf(f.line.buffer, `%s: `, name) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func (f *AsciiFormatter) printComma() { |
| 291 |
f.size[len(f.size)-1]-- |
| 292 |
if f.size[len(f.size)-1] > 0 { |
| 293 |
f.line.buffer.WriteRune(',') |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
func (f *AsciiFormatter) printValue(value interface{}) { |
| 298 |
switch value.(type) { |
| 299 |
case string: |
| 300 |
fmt.Fprintf(f.line.buffer, `"%s"`, value) |
| 301 |
case nil: |
| 302 |
f.line.buffer.WriteString("null") |
| 303 |
default: |
| 304 |
fmt.Fprintf(f.line.buffer, `%#v`, value) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
func (f *AsciiFormatter) print(a string) { |
| 309 |
f.line.buffer.WriteString(a) |
| 310 |
} |
| 311 |
|
| 312 |
func (f *AsciiFormatter) printRecursive(name string, value interface{}, marker string) { |
| 313 |
switch value.(type) { |
| 314 |
case map[string]interface{}: |
| 315 |
f.newLine(marker) |
| 316 |
f.printKey(name) |
| 317 |
f.print("{") |
| 318 |
f.closeLine() |
| 319 |
|
| 320 |
m := value.(map[string]interface{}) |
| 321 |
size := len(m) |
| 322 |
f.push(name, size, false) |
| 323 |
|
| 324 |
keys := sortedKeys(m) |
| 325 |
for _, key := range keys { |
| 326 |
f.printRecursive(key, m[key], marker) |
| 327 |
} |
| 328 |
f.pop() |
| 329 |
|
| 330 |
f.newLine(marker) |
| 331 |
f.print("}") |
| 332 |
f.printComma() |
| 333 |
f.closeLine() |
| 334 |
|
| 335 |
case []interface{}: |
| 336 |
f.newLine(marker) |
| 337 |
f.printKey(name) |
| 338 |
f.print("[") |
| 339 |
f.closeLine() |
| 340 |
|
| 341 |
s := value.([]interface{}) |
| 342 |
size := len(s) |
| 343 |
f.push("", size, true) |
| 344 |
for _, item := range s { |
| 345 |
f.printRecursive("", item, marker) |
| 346 |
} |
| 347 |
f.pop() |
| 348 |
|
| 349 |
f.newLine(marker) |
| 350 |
f.print("]") |
| 351 |
f.printComma() |
| 352 |
f.closeLine() |
| 353 |
|
| 354 |
default: |
| 355 |
f.newLine(marker) |
| 356 |
f.printKey(name) |
| 357 |
f.printValue(value) |
| 358 |
f.printComma() |
| 359 |
f.closeLine() |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
func sortedKeys(m map[string]interface{}) (keys []string) { |
| 364 |
keys = make([]string, 0, len(m)) |
| 365 |
for key := range m { |
| 366 |
keys = append(keys, key) |
| 367 |
} |
| 368 |
sort.Strings(keys) |
| 369 |
return |
| 370 |
} |