-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathref_locations.go
More file actions
49 lines (40 loc) · 1.43 KB
/
Copy pathref_locations.go
File metadata and controls
49 lines (40 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package validate
import (
"github.com/go-openapi/analysis"
)
// refLocations tells where a $ref value is declared in a document.
//
// The analyzer indexes references the other way around, by the location each
// was found at, so the index is inverted here. Only declarations are indexed:
// a "$ref" member sitting in an example or in a default value is data, and the
// analyzer never walks into it.
type refLocations map[string]pathSegments
// newRefLocations inverts the analyzer's reference index.
//
// A reference declared in several places keeps the smallest declaration, so
// that the answer does not depend on map iteration order.
func newRefLocations(analyzer *analysis.Spec) refLocations {
declarations := make(map[string]string)
for location, ref := range analyzer.AllRefsByLocation() {
value := ref.String()
if value == "" {
continue
}
if known, isKnown := declarations[value]; isKnown && known <= location {
continue
}
declarations[value] = location
}
locations := make(refLocations, len(declarations))
for value, location := range declarations {
locations[value] = localRefPath(location)
}
return locations
}
// at returns where a reference is declared, or the document root when the
// reference is not one the analyzer indexed.
func (l refLocations) at(ref string) pathSegments {
return l[ref]
}