Repository navigation
Expand file tree
/
Copy pathexternalref.go
More file actions
172 lines (155 loc) · 6.55 KB
/
Copy pathexternalref.go
File metadata and controls
172 lines (155 loc) · 6.55 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package codegen
import (
"fmt"
"go/ast"
"slices"
"strings"
)
// ensureExternalRefsInRequestBodyDefinitions ensures that when an externalRef (`$ref` that points to a file that isn't the current spec) is encountered, we make sure we update our underlying `RefType` to make sure that we point to that type.
// This only happens if we have a non-empty `ref` passed in, and that `ref` isn't pointing to something in our file
// NOTE that the pointer here allows us to pass in a reference and edit in-place
func ensureExternalRefsInRequestBodyDefinitions(defs *[]RequestBodyDefinition, ref string) {
if ref == "" {
return
}
for i, rbd := range *defs {
ensureExternalRefsInSchema(&rbd.Schema, ref)
// make sure we then update it in-place
(*defs)[i] = rbd
}
}
// ensureExternalRefsInResponseDefinitions ensures that when an externalRef (`$ref` that points to a file that isn't the current spec) is encountered, we make sure we update our underlying `RefType` to make sure that we point to that type.
// This only happens if we have a non-empty `ref` passed in, and that `ref` isn't pointing to something in our file
// NOTE that the pointer here allows us to pass in a reference and edit in-place
func ensureExternalRefsInResponseDefinitions(defs *[]ResponseDefinition, ref string) {
if ref == "" {
return
}
for i, rd := range *defs {
for j, rcd := range rd.Contents {
ensureExternalRefsInSchema(&rcd.Schema, ref)
// make sure we then update it in-place
rd.Contents[j] = rcd
}
for j, rhd := range rd.Headers {
// A header that is itself a $ref into another document was
// already qualified with that document's package.
if rhd.Schema.IsExternalRef() {
continue
}
ensureExternalRefsInSchema(&rhd.Schema, ref)
rd.Headers[j] = rhd
}
// make sure we then update it in-place
(*defs)[i] = rd
}
}
// ensureExternalRefsInParameterDefinitions ensures that when an externalRef (`$ref` that points to a file that isn't the current spec) is encountered, we make sure we update our underlying `RefType` to make sure that we point to that type.
// This only happens if we have a non-empty `ref` passed in, and that `ref` isn't pointing to something in our file
// NOTE that the pointer here allows us to pass in a reference and edit in-place
func ensureExternalRefsInParameterDefinitions(defs *[]ParameterDefinition, ref string) {
if ref == "" {
return
}
for i, pd := range *defs {
ensureExternalRefsInSchema(&pd.Schema, ref)
// make sure we then update it in-place
(*defs)[i] = pd
}
}
// externalPackageFor returns the imported Go package name for a `$ref` that
// targets a file outside the current spec. Returns an empty string when the
// ref is empty, points within the current spec, or has no import-mapping.
func externalPackageFor(ref string) string {
if ref == "" {
return ""
}
parts := strings.SplitN(ref, "#", 2)
if pack, ok := globalState.importMapping[parts[0]]; ok {
return pack.Name
}
return ""
}
// ensureExternalRefsInSchema ensures that when an externalRef (`$ref` that points to a file that isn't the current spec) is encountered, we make sure we update our underlying `RefType` to make sure that we point to that type.
//
// This only happens if we have a non-empty `ref` passed in, and that `ref` isn't pointing to something in our file
//
// The schema's Go type was generated in the context of the external document,
// so the type names in it are declared by the imported package. Only those
// names are qualified: builtins, names already qualified with a package, and
// the syntax around them (`[]`, `*`, `map[...]`, struct literals) are kept.
//
// NOTE that the pointer here allows us to pass in a reference and edit in-place
func ensureExternalRefsInSchema(schema *Schema, ref string) {
pkg := externalPackageFor(ref)
if pkg == "" {
return
}
switch {
case isExternalTypeName(schema.GoType):
// A named type of the imported package is referred to as a $ref
// to it would be. This wins over a RefType the schema already
// carries, as it always has, so generated code keeps its types.
schema.RefType = fmt.Sprintf("%s.%s", pkg, schema.GoType)
case strings.HasPrefix(schema.GoType, "struct {"):
// A struct literal is spelled out in place, so its fields must
// name the imported package's types. One that the schema declares
// as a named type is referred to by that name, unchanged.
if schema.RefType == "" {
schema.GoType = qualifyExternalTypeNames(schema.GoType, pkg)
}
case schema.RefType != "":
schema.RefType = qualifyExternalTypeNames(schema.RefType, pkg)
default:
schema.GoType = qualifyExternalTypeNames(schema.GoType, pkg)
}
// Qualify union branch types. Each UnionElement was resolved as a
// local reference by the union generator (e.g. "Bionicle"); when the
// enclosing schema came from an externally-ref'd file the branches
// actually live in the imported package, so the As/From/Merge
// methods generated by union.tmpl must use "<pkg>.Bionicle" instead.
for i, elem := range schema.UnionElements {
s := string(elem)
if !strings.Contains(s, ".") {
schema.UnionElements[i] = UnionElement(fmt.Sprintf("%s.%s", pkg, s))
}
}
}
// isExternalTypeName reports whether goType is a single unqualified type name
// that is not a Go builtin, such as the name of a generated model. A name in
// parentheses is not one: the caller spells the qualified name from goType as
// written.
func isExternalTypeName(goType string) bool {
expr, _, ok := parseGoType(goType)
if !ok {
return false
}
ident, isIdent := expr.(*ast.Ident)
return isIdent && !isPredeclaredType(ident.Name)
}
// qualifyExternalTypeNames qualifies with pkg every unqualified type name in
// the Go type expression typeExpr that is not a Go builtin, for example
// "[]Foo" becomes "[]pkg.Foo" and "struct { F *Foo `json:...` }" becomes
// "struct { F *pkg.Foo `json:...` }". Builtins ("any", "*string"), names that
// already carry a package ("time.Time") and everything else in typeExpr,
// including field names, tags and comments, are left as they are. An
// expression that does not parse is returned unchanged.
func qualifyExternalTypeNames(typeExpr, pkg string) string {
expr, fset, ok := parseGoType(typeExpr)
if !ok {
return typeExpr
}
// Rewritten from the end, so an insertion doesn't move the ones before.
var offsets []int
typeIdents(expr, func(id *ast.Ident) bool {
if !isPredeclaredType(id.Name) {
offsets = append(offsets, fset.Position(id.Pos()).Offset)
}
return true
})
qualified := typeExpr
for _, offset := range slices.Backward(offsets) {
qualified = qualified[:offset] + pkg + "." + qualified[offset:]
}
return qualified
}