@@ -9,12 +9,12 @@ import (
99
1010 "github.com/pb33f/doctor/changerator"
1111 drModel "github.com/pb33f/doctor/model"
12+ v3 "github.com/pb33f/doctor/model/high/v3"
1213 whatChangedModel "github.com/pb33f/libopenapi/what-changed/model"
1314 "github.com/pb33f/openapi-changes/internal/breakingrules"
1415 "github.com/pb33f/openapi-changes/model"
1516)
1617
17-
1818// changeratorResult owns the doctor-side resources created for a single comparison.
1919//
2020// Callers must call Release() exactly once when they are done reading Changerator,
@@ -82,6 +82,7 @@ func runChangerator(commit *model.Commit, breakingConfig *whatChangedModel.Break
8282 leftDrDoc .Release ()
8383 return nil , nil
8484 }
85+ rewriteOutputLocations (ctr , docChanges , commit .DocumentRewriters )
8586
8687 return & changeratorResult {
8788 Changerator : ctr ,
@@ -91,6 +92,94 @@ func runChangerator(commit *model.Commit, breakingConfig *whatChangedModel.Break
9192 }, nil
9293}
9394
95+ func rewriteOutputLocations (ctr * changerator.Changerator , docChanges * whatChangedModel.DocumentChanges , rewriters []model.DocumentPathRewriter ) {
96+ if len (rewriters ) == 0 {
97+ return
98+ }
99+ rewriteDocumentChangeLocations (docChanges , rewriters )
100+ if ctr != nil {
101+ rewriteChangedNodeLocations (ctr .ChangedNodes , rewriters )
102+ }
103+ }
104+
105+ func rewriteDocumentChangeLocations (docChanges * whatChangedModel.DocumentChanges , rewriters []model.DocumentPathRewriter ) {
106+ if docChanges == nil {
107+ return
108+ }
109+ for _ , change := range docChanges .GetAllChanges () {
110+ if change == nil || change .Context == nil || change .Context .DocumentLocation == "" {
111+ continue
112+ }
113+ change .Context .DocumentLocation = rewriteDocumentLocation (change .Context .DocumentLocation , rewriters )
114+ }
115+ }
116+
117+ func rewriteChangedNodeLocations (nodes []* v3.Node , rewriters []model.DocumentPathRewriter ) {
118+ if len (nodes ) == 0 {
119+ return
120+ }
121+ seen := make (map [* v3.Node ]struct {}, len (nodes ))
122+ var walk func (* v3.Node )
123+ walk = func (node * v3.Node ) {
124+ if node == nil {
125+ return
126+ }
127+ if _ , ok := seen [node ]; ok {
128+ return
129+ }
130+ seen [node ] = struct {}{}
131+
132+ if node .Origin != nil {
133+ node .Origin .AbsoluteLocation = rewriteDocumentLocation (node .Origin .AbsoluteLocation , rewriters )
134+ node .Origin .AbsoluteLocationValue = rewriteDocumentLocation (node .Origin .AbsoluteLocationValue , rewriters )
135+ }
136+ node .OriginLocation = rewriteDocumentLocation (node .OriginLocation , rewriters )
137+ for _ , changed := range node .GetChanges () {
138+ for _ , change := range changed .GetAllChanges () {
139+ if change == nil || change .Context == nil {
140+ continue
141+ }
142+ change .Context .DocumentLocation = rewriteDocumentLocation (change .Context .DocumentLocation , rewriters )
143+ }
144+ }
145+ for _ , change := range node .RenderedChanges {
146+ if change == nil || change .Context == nil {
147+ continue
148+ }
149+ change .Context .DocumentLocation = rewriteDocumentLocation (change .Context .DocumentLocation , rewriters )
150+ }
151+ for _ , change := range node .CleanedChanged {
152+ if change == nil || change .Context == nil {
153+ continue
154+ }
155+ change .Context .DocumentLocation = rewriteDocumentLocation (change .Context .DocumentLocation , rewriters )
156+ }
157+
158+ for _ , child := range node .Children {
159+ walk (child )
160+ }
161+ }
162+ for _ , node := range nodes {
163+ walk (node )
164+ }
165+ }
166+
167+ func rewriteDocumentLocation (raw string , rewriters []model.DocumentPathRewriter ) string {
168+ if raw == "" {
169+ return raw
170+ }
171+ for _ , rewrite := range rewriters {
172+ if rewrite == nil {
173+ continue
174+ }
175+ rewritten := rewrite (raw )
176+ if rewritten != raw {
177+ return rewritten
178+ }
179+ }
180+ return raw
181+ }
182+
94183func emitCommitWarning (commitHash string , err error ) {
95184 fmt .Fprintf (os .Stderr , "warning: commit %s: %s\n " , commitHash , err )
96185}
0 commit comments