Skip to content

Commit 29ebe32

Browse files
authored
chore: move install mapping to global state (#853)
Move importMapping into globalState along side other global variables so its all in one place as per comment on that struct.
1 parent 47bc5ec commit 29ebe32

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

pkg/codegen/codegen.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ var templates embed.FS
3737
// globalState stores all global state. Please don't put global state anywhere
3838
// else so that we can easily track it.
3939
var globalState struct {
40-
options Configuration
41-
spec *openapi3.T
40+
options Configuration
41+
spec *openapi3.T
42+
importMapping importMap
4243
}
4344

4445
// goImport represents a go package to be imported in the generated code
@@ -67,8 +68,6 @@ func (im importMap) GoImports() []string {
6768
return goImports
6869
}
6970

70-
var importMapping importMap
71-
7271
func constructImportMapping(importMapping map[string]string) importMap {
7372
var (
7473
pathToName = map[string]string{}
@@ -101,8 +100,7 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) {
101100
// This is global state
102101
globalState.options = opts
103102
globalState.spec = spec
104-
105-
importMapping = constructImportMapping(opts.ImportMapping)
103+
globalState.importMapping = constructImportMapping(opts.ImportMapping)
106104

107105
filterOperationsByTag(spec, opts)
108106
if !opts.OutputOptions.SkipPrune {
@@ -237,7 +235,7 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) {
237235

238236
var inlinedSpec string
239237
if opts.Generate.EmbeddedSpec {
240-
inlinedSpec, err = GenerateInlinedSpec(t, importMapping, spec)
238+
inlinedSpec, err = GenerateInlinedSpec(t, globalState.importMapping, spec)
241239
if err != nil {
242240
return "", fmt.Errorf("error generating Go handlers for Paths: %w", err)
243241
}
@@ -246,7 +244,7 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) {
246244
var buf bytes.Buffer
247245
w := bufio.NewWriter(&buf)
248246

249-
externalImports := append(importMapping.GoImports(), importMap(xGoTypeImports).GoImports()...)
247+
externalImports := append(globalState.importMapping.GoImports(), importMap(xGoTypeImports).GoImports()...)
250248
importsOut, err := GenerateImports(t, externalImports, opts.PackageName)
251249
if err != nil {
252250
return "", fmt.Errorf("error generating imports: %w", err)

pkg/codegen/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func refPathToGoType(refPath string, local bool) (string, error) {
369369
return "", fmt.Errorf("unsupported reference: %s", refPath)
370370
}
371371
remoteComponent, flatComponent := pathParts[0], pathParts[1]
372-
if goImport, ok := importMapping[remoteComponent]; !ok {
372+
if goImport, ok := globalState.importMapping[remoteComponent]; !ok {
373373
return "", fmt.Errorf("unrecognized external reference '%s'; please provide the known import for this reference using option --import-mapping", remoteComponent)
374374
} else {
375375
goType, err := refPathToGoType("#"+flatComponent, false)

pkg/codegen/utils_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ func TestSortedRequestBodyKeys(t *testing.T) {
134134
}
135135

136136
func TestRefPathToGoType(t *testing.T) {
137-
old := importMapping
138-
importMapping = constructImportMapping(map[string]string{
137+
old := globalState.importMapping
138+
globalState.importMapping = constructImportMapping(map[string]string{
139139
"doc.json": "externalref0",
140140
"http://deepmap.com/doc.json": "externalref1",
141141
})
142-
defer func() { importMapping = old }()
142+
defer func() { globalState.importMapping = old }()
143143

144144
tests := []struct {
145145
name string

0 commit comments

Comments
 (0)