golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/apidiff/report.go (about) 1 package apidiff 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 ) 8 9 // Report describes the changes detected by Changes. 10 type Report struct { 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 28 } 29 30 func (r Report) String() string { 31 var buf bytes.Buffer 32 if err := r.Text(&buf); err != nil { 33 return fmt.Sprintf("!!%v", err) 34 } 35 return buf.String() 36 } 37 38 func (r Report) Text(w io.Writer) error { 39 if err := r.TextIncompatible(w, true); err != nil { 40 return err 41 } 42 return r.TextCompatible(w) 43 } 44 45 func (r Report) TextIncompatible(w io.Writer, withHeader bool) error { 46 if withHeader { 47 return r.writeMessages(w, "Incompatible changes:", r.messages(false)) 48 } 49 return r.writeMessages(w, "", r.messages(false)) 50 } 51 52 func (r Report) TextCompatible(w io.Writer) error { 53 return r.writeMessages(w, "Compatible changes:", r.messages(true)) 54 } 55 56 func (r Report) writeMessages(w io.Writer, header string, msgs []string) error { 57 if len(msgs) == 0 { 58 return nil 59 } 60 if header != "" { 61 if _, err := fmt.Fprintf(w, "%s\n", header); err != nil { 62 return err 63 } 64 } 65 for _, m := range msgs { 66 if _, err := fmt.Fprintf(w, "- %s\n", m); err != nil { 67 return err 68 } 69 } 70 return nil 71 }