11// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
22// SPDX-License-Identifier: Apache-2.0
33
4- // Package language provides the language-specific options used by the
4+ // Package formatting provides the language-specific options used by the
55// go-swagger code generator. The primary type is [Options], which describes
66// formatting, naming, and import resolution rules for a target language.
7- package language
7+ package formatting
88
99import (
10- "path"
1110 "path/filepath"
12- "strings"
1311
1412 "golang.org/x/tools/imports"
1513
16- golangfuncs "github.com/go-openapi/codegen/funcmaps/golang"
1714 "github.com/go-openapi/codegen/mangling"
1815)
1916
@@ -76,19 +73,15 @@ func FormatOptsWithDefault(opts []FormatOption) FormatOpts {
7673
7774// Options describes a target language to the code generator.
7875type Options struct {
79- ReservedWords []string
8076 BaseImportFunc MangleFunc `json:"-"`
8177 ImportsFunc func (map [string ]string ) string `json:"-"`
8278 ArrayInitializerFunc func (any ) (string , error ) `json:"-"`
8379 FormatOnly bool
8480 ExtraInitialisms []string
85- Mangler mangling.NameMangler
81+ Mangler mangling.GoMangler
8682
87- reservedWordsSet map [string ]struct {}
88- initialized bool
89- formatFunc FormatterFunc
90- fileNameFunc MangleFunc // language specific source file naming rules
91- dirNameFunc MangleFunc // language specific directory naming rules
83+ initialized bool
84+ formatFunc FormatterFunc
9285}
9386
9487// SetFormatFunc sets the formatting function for this language.
@@ -102,45 +95,30 @@ func (l *Options) Init() {
10295 return
10396 }
10497
105- l .Mangler = mangling .NewNameMangler (
106- mangling .WithGoNamePrefixFunc (golangfuncs .PrefixForName ),
107- mangling .WithAdditionalInitialisms (l .ExtraInitialisms ... ),
98+ l .Mangler = mangling .MakeGoMangler (
99+ mangling .WithGoInitialisms (l .ExtraInitialisms ... ),
108100 )
109101
110102 l .initialized = true
111- l .reservedWordsSet = make (map [string ]struct {})
112-
113- for _ , rw := range l .ReservedWords {
114- l .reservedWordsSet [rw ] = struct {}{}
115- }
116103}
117104
118- // MangleName makes sure a reserved word gets a safe name .
105+ // MangleName makes sure a string becomes a safe go identifier .
119106func (l * Options ) MangleName (name , suffix string ) string {
120- if _ , ok := l . reservedWordsSet [ l . Mangler . ToFileName ( name )]; ! ok {
121- return name
107+ if name == "" {
108+ return suffix
122109 }
123110
124- return strings . Join ([] string { name , suffix }, "_" )
111+ return l . Mangler . IdentExported ( name )
125112}
126113
127114// MangleVarName makes sure a reserved word gets a safe name.
128115func (l * Options ) MangleVarName (name string ) string {
129- nm := l .Mangler .ToVarName (name )
130- if _ , ok := l .reservedWordsSet [nm ]; ! ok {
131- return nm
132- }
133-
134- return nm + "Var"
116+ return l .Mangler .IdentUnexported (name )
135117}
136118
137119// MangleFileName makes sure a file name gets a safe name.
138120func (l * Options ) MangleFileName (name string ) string {
139- if l .fileNameFunc != nil {
140- return l .fileNameFunc (name )
141- }
142-
143- return l .Mangler .ToFileName (name )
121+ return l .Mangler .File (name )
144122}
145123
146124// ManglePackageName makes sure a package gets a safe name.
@@ -149,12 +127,11 @@ func (l *Options) ManglePackageName(name, suffix string) string {
149127 if name == "" {
150128 return suffix
151129 }
152- if l .dirNameFunc != nil {
153- name = l .dirNameFunc (name )
154- }
155- pth := filepath .ToSlash (filepath .Clean (name )) // preserve path
156- pkg := importAlias (pth ) // drop path
157- return l .MangleName (l .Mangler .ToFileName (golangfuncs .PrefixForName (pkg )+ pkg ), suffix )
130+
131+ target := filepath .ToSlash (filepath .Clean (name )) // preserve path
132+ short , _ := l .Mangler .Package (target )
133+
134+ return short
158135}
159136
160137// ManglePackagePath makes sure a full package path gets a safe name.
@@ -163,11 +140,11 @@ func (l *Options) ManglePackagePath(name string, suffix string) string {
163140 if name == "" {
164141 return suffix
165142 }
143+
166144 target := filepath .ToSlash (filepath .Clean (name )) // preserve path
167- parts := strings .Split (target , "/" )
168- parts [len (parts )- 1 ] = l .ManglePackageName (parts [len (parts )- 1 ], suffix )
145+ _ , fqn := l .Mangler .Package (target )
169146
170- return strings . Join ( parts , "/" )
147+ return fqn
171148}
172149
173150// FormatContent formats a file with a language specific formatter.
@@ -206,9 +183,3 @@ func (l *Options) BaseImport(tgt string) string {
206183
207184 return ""
208185}
209-
210- // importAlias extracts the last path component from a package import path.
211- func importAlias (pkg string ) string {
212- _ , k := path .Split (pkg )
213- return k
214- }
0 commit comments