func diffMaps(dq *diffQueue, w io.Writer, p types.Path, v1, v2 types.Map) { wroteHeader := false added, removed, modified := v2.Diff(v1) for _, k := range added { wroteHeader = writeHeader(w, wroteHeader, p) line(w, addPrefix, k, v2.Get(k)) } for _, k := range removed { wroteHeader = writeHeader(w, wroteHeader, p) line(w, subPrefix, k, v1.Get(k)) } for _, k := range modified { c1, c2 := v1.Get(k), v2.Get(k) if canCompare(c1, c2) { buf := bytes.NewBuffer(nil) d.Exp.NoError(types.WriteEncodedValueWithTags(buf, k)) p1 := p.AddField(buf.String()) dq.PushBack(diffInfo{path: p1, key: k, v1: c1, v2: c2}) } else { wroteHeader = writeHeader(w, wroteHeader, p) line(w, subPrefix, k, v1.Get(k)) line(w, addPrefix, k, v2.Get(k)) } } writeFooter(w, wroteHeader) }
func diffStructs(w io.Writer, p types.Path, v1, v2 types.Struct) { changeChan := make(chan types.ValueChanged) stopChan := make(chan struct{}, 1) // buffer size of 1, so this won't block if diff already finished defer stop(stopChan) go func() { v2.Diff(v1, changeChan, stopChan) close(changeChan) }() wroteHeader := false for change := range changeChan { fn := string(change.V.(types.String)) switch change.ChangeType { case types.DiffChangeAdded: wroteHeader = writeHeader(w, wroteHeader, p) field(w, ADD, change.V, v2.Get(fn)) case types.DiffChangeRemoved: wroteHeader = writeHeader(w, wroteHeader, p) field(w, DEL, change.V, v1.Get(fn)) case types.DiffChangeModified: f1 := v1.Get(fn) f2 := v2.Get(fn) if shouldDescend(f1, f2) { diff(w, p.AddField(fn), types.String(fn), f1, f2) } else { wroteHeader = writeHeader(w, wroteHeader, p) field(w, DEL, change.V, f1) field(w, ADD, change.V, f2) } } } writeFooter(w, wroteHeader) }
func diffStructs(dq *diffQueue, w io.Writer, p types.Path, v1, v2 types.Struct) { changed := types.StructDiff(v1, v2) wroteHeader := false for _, field := range changed { f1 := v1.Get(field) f2 := v2.Get(field) if canCompare(f1, f2) { p1 := p.AddField(field) dq.PushBack(diffInfo{path: p1, key: types.String(field), v1: f1, v2: f2}) } else { wroteHeader = writeHeader(w, wroteHeader, p) line(w, subPrefix, types.String(field), f1) line(w, addPrefix, types.String(field), f2) } } }
func diffSets(dq *diffQueue, w io.Writer, p types.Path, v1, v2 types.Set) { wroteHeader := false added, removed := v2.Diff(v1) if len(added) == 1 && len(removed) == 1 && canCompare(added[0], removed[0]) { p1 := p.AddField(added[0].Hash().String()) dq.PushBack(diffInfo{path: p1, key: types.String(""), v1: removed[0], v2: added[0]}) } else { for _, value := range removed { wroteHeader = writeHeader(w, wroteHeader, p) line(w, subPrefix, nil, value) } for _, value := range added { wroteHeader = writeHeader(w, wroteHeader, p) line(w, addPrefix, nil, value) } } writeFooter(w, wroteHeader) return }