Skip to content

Commit fe54fb3

Browse files
committed
apidiff: represent a Report as a list of Changes
Modify the Report representation to be a list of Change values, instead of two string slices. This will enable adding more information to each change, like source location. Change-Id: Ia7389d7bc552479ea5e06efd7fdefe004058e66f Reviewed-on: https://go-review.googlesource.com/c/tools/+/172777 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent c6b416e commit fe54fb3

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

internal/apidiff/apidiff.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ import (
2424
func Changes(old, new *types.Package) Report {
2525
d := newDiffer(old, new)
2626
d.checkPackage()
27-
return Report{
28-
Incompatible: d.incompatibles.collect(),
29-
Compatible: d.compatibles.collect(),
27+
r := Report{}
28+
for _, m := range d.incompatibles.collect() {
29+
r.Changes = append(r.Changes, Change{Message: m, Compatible: false})
3030
}
31+
for _, m := range d.compatibles.collect() {
32+
r.Changes = append(r.Changes, Change{Message: m, Compatible: true})
33+
}
34+
return r
3135
}
3236

3337
type differ struct {

internal/apidiff/apidiff_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ func TestChanges(t *testing.T) {
3737

3838
report := Changes(oldpkg.Types, newpkg.Types)
3939

40-
if !reflect.DeepEqual(report.Incompatible, wanti) {
41-
t.Errorf("incompatibles: got %v\nwant %v\n", report.Incompatible, wanti)
40+
got := report.messages(false)
41+
if !reflect.DeepEqual(got, wanti) {
42+
t.Errorf("incompatibles: got %v\nwant %v\n", got, wanti)
4243
}
43-
if !reflect.DeepEqual(report.Compatible, wantc) {
44-
t.Errorf("compatibles: got %v\nwant %v\n", report.Compatible, wantc)
44+
got = report.messages(true)
45+
if !reflect.DeepEqual(got, wantc) {
46+
t.Errorf("compatibles: got %v\nwant %v\n", got, wantc)
4547
}
4648
}
4749

internal/apidiff/report.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,23 @@ import (
88

99
// Report describes the changes detected by Changes.
1010
type Report struct {
11-
Incompatible, Compatible []string
11+
Changes []Change
12+
}
13+
14+
// A Change describes a single API change.
15+
type Change struct {
16+
Message string
17+
Compatible bool
18+
}
19+
20+
func (r Report) messages(compatible bool) []string {
21+
var msgs []string
22+
for _, c := range r.Changes {
23+
if c.Compatible == compatible {
24+
msgs = append(msgs, c.Message)
25+
}
26+
}
27+
return msgs
1228
}
1329

1430
func (r Report) String() string {
@@ -28,13 +44,13 @@ func (r Report) Text(w io.Writer) error {
2844

2945
func (r Report) TextIncompatible(w io.Writer, withHeader bool) error {
3046
if withHeader {
31-
return r.writeMessages(w, "Incompatible changes:", r.Incompatible)
47+
return r.writeMessages(w, "Incompatible changes:", r.messages(false))
3248
}
33-
return r.writeMessages(w, "", r.Incompatible)
49+
return r.writeMessages(w, "", r.messages(false))
3450
}
3551

3652
func (r Report) TextCompatible(w io.Writer) error {
37-
return r.writeMessages(w, "Compatible changes:", r.Compatible)
53+
return r.writeMessages(w, "Compatible changes:", r.messages(true))
3854
}
3955

4056
func (r Report) writeMessages(w io.Writer, header string, msgs []string) error {

0 commit comments

Comments
 (0)