Skip to content

Commit db8ecef

Browse files
authored
Merge pull request #17 from fredbi/feat/new-mangling
Feat/new mangling
2 parents 1094d62 + 1826d54 commit db8ecef

105 files changed

Lines changed: 236923 additions & 3012 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

doc.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
// Package codegen exposes utilities to build and test
2-
// code generators that build go apps.
1+
// Package codegen exposes utilities to build and test code generators that build go apps.
32
package codegen

formatting/format_lite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package language
4+
package formatting
55

66
import (
77
"bytes"

formatting/format_lite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package language
4+
package formatting
55

66
import (
77
"strings"

formatting/golang.go

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package language
4+
package formatting
55

66
import (
77
"encoding/json"
@@ -24,17 +24,8 @@ var moduleRe = regexp.MustCompile(`module[ \t]+([^\s]+)`)
2424
// GolangOpts returns [Options] for rendering items as golang code.
2525
func GolangOpts(extraInitialisms ...string) *Options {
2626
opts := new(Options)
27-
opts.ReservedWords = []string{
28-
"break", "default", "func", "interface", "select",
29-
"case", "defer", "go", "map", "struct",
30-
"chan", "else", "goto", "package", "switch",
31-
"const", "fallthrough", "if", "range", "type",
32-
"continue", "for", "import", "return", "var",
33-
}
3427
opts.ExtraInitialisms = extraInitialisms
3528
opts.formatFunc = defaultGoFormatFunc() // this default may be overridden by [GenOpts]
36-
opts.fileNameFunc = opts.defaultGoFilenameFunc(goOtherReservedSuffixes())
37-
opts.dirNameFunc = defaultGoDirnameFunc()
3829
opts.ImportsFunc = defaultGoImportsFunc()
3930
opts.ArrayInitializerFunc = defaultGoArrayInitializerFunc()
4031
opts.BaseImportFunc = defaultGoBaseImportFunc()
@@ -52,26 +43,6 @@ func defaultGoFormatFunc() FormatterFunc {
5243
}
5344
}
5445

55-
func (o *Options) defaultGoFilenameFunc(reservedSuffixes map[string]bool) MangleFunc {
56-
return func(name string) string {
57-
parts := strings.Split(o.Mangler.ToFileName(name), "_")
58-
if reservedSuffixes[parts[len(parts)-1]] {
59-
parts = append(parts, "swagger")
60-
}
61-
return strings.Join(parts, "_")
62-
}
63-
}
64-
65-
func defaultGoDirnameFunc() MangleFunc {
66-
return func(name string) string {
67-
switch name {
68-
case "vendor", "internal":
69-
return strings.Join([]string{name, "swagger"}, "_")
70-
}
71-
return name
72-
}
73-
}
74-
7546
func defaultGoImportsFunc() func(map[string]string) string {
7647
return func(imports map[string]string) string {
7748
if len(imports) == 0 {
@@ -277,55 +248,3 @@ func CheckPrefixAndFetchRelativePath(childpath string, parentpath string) (bool,
277248

278249
return false, ""
279250
}
280-
281-
func goOtherReservedSuffixes() map[string]bool {
282-
return map[string]bool{
283-
// goos
284-
"aix": true,
285-
"android": true,
286-
"darwin": true,
287-
"dragonfly": true,
288-
"freebsd": true,
289-
"hurd": true,
290-
"illumos": true,
291-
"ios": true,
292-
"js": true,
293-
"linux": true,
294-
"nacl": true,
295-
"netbsd": true,
296-
"openbsd": true,
297-
"plan9": true,
298-
"solaris": true,
299-
"windows": true,
300-
"zos": true,
301-
302-
// arch
303-
"386": true,
304-
"amd64": true,
305-
"amd64p32": true,
306-
"arm": true,
307-
"armbe": true,
308-
"arm64": true,
309-
"arm64be": true,
310-
"loong64": true,
311-
"mips": true,
312-
"mipsle": true,
313-
"mips64": true,
314-
"mips64le": true,
315-
"mips64p32": true,
316-
"mips64p32le": true,
317-
"ppc": true,
318-
"ppc64": true,
319-
"ppc64le": true,
320-
"riscv": true,
321-
"riscv64": true,
322-
"s390": true,
323-
"s390x": true,
324-
"sparc": true,
325-
"sparc64": true,
326-
"wasm": true,
327-
328-
// other reserved suffixes
329-
"test": true,
330-
}
331-
}

formatting/golang_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package language
4+
package formatting
55

66
import (
77
"strings"
@@ -15,7 +15,8 @@ func TestGolang_MangleFileName(t *testing.T) {
1515
o := &Options{}
1616
o.Init()
1717
res := o.MangleFileName("aFileEndingInOsNameWindows")
18-
assert.TrueT(t, strings.HasSuffix(res, "_windows"))
18+
assert.FalseT(t, strings.HasSuffix(res, "_windows"))
19+
assert.TrueT(t, strings.HasSuffix(res, "_windows_swagger"))
1920

2021
o = GolangOpts()
2122
res = o.MangleFileName("aFileEndingInOsNameWindows")
@@ -36,20 +37,20 @@ func TestGolang_ManglePackage(t *testing.T) {
3637
expectedName string
3738
}{
3839
{tested: "", expectedPath: defaultPackage, expectedName: defaultPackage},
39-
{tested: "select", expectedPath: "select_default", expectedName: "select_default"},
40+
{tested: "select", expectedPath: "selectpkg", expectedName: "selectpkg"}, // a package path may use a go keyword?
4041
{tested: "x", expectedPath: "x", expectedName: "x"},
4142
{tested: "a/b/c-d/e_f/g", expectedPath: "a/b/c-d/e_f/g", expectedName: "g"},
42-
{tested: "a/b/c-d/e_f/g-h", expectedPath: "a/b/c-d/e_f/g_h", expectedName: "g_h"},
43-
{tested: "a/b/c-d/e_f/2A", expectedPath: "a/b/c-d/e_f/nr2_a", expectedName: "nr2_a"},
44-
{tested: "a/b/c-d/e_f/#", expectedPath: "a/b/c-d/e_f/hash_tag", expectedName: "hash_tag"},
45-
{tested: "#help", expectedPath: "hash_tag_help", expectedName: "hash_tag_help"},
46-
{tested: "vendor", expectedPath: "vendor_swagger", expectedName: "vendor_swagger"},
47-
{tested: "internal", expectedPath: "internal_swagger", expectedName: "internal_swagger"},
43+
{tested: "a/b/c-d/e_f/g-h", expectedPath: "a/b/c-d/e_f/g-h", expectedName: "h"},
44+
{tested: "a/b/c-d/e_f/2A", expectedPath: "a/b/c-d/e_f/2-a", expectedName: "a"},
45+
{tested: "a/b/c-d/e_f/#", expectedPath: "a/b/c-d/e_f/hash", expectedName: "hash"},
46+
{tested: "#help", expectedPath: "hash-help", expectedName: "help"},
47+
{tested: "vendor", expectedPath: "vendorpkg", expectedName: "vendorpkg"},
48+
{tested: "internal", expectedPath: "internalpkg", expectedName: "internalpkg"},
4849
} {
4950
res := o.ManglePackagePath(v.tested, defaultPackage)
50-
assert.EqualT(t, v.expectedPath, res)
51+
assert.EqualTf(t, v.expectedPath, res, "expected ManglePackagePath(%q) to yield %q but go %q", v.tested, v.expectedPath, res)
5152
res = o.ManglePackageName(v.tested, defaultPackage)
52-
assert.EqualT(t, v.expectedName, res)
53+
assert.EqualTf(t, v.expectedName, res, "expected ManglePackageName(%q) to yield %q but go %q", v.tested, v.expectedName, res)
5354
}
5455
}
5556

formatting/options.go

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
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

99
import (
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.
7875
type 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.
119106
func (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.
128115
func (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.
138120
func (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-
}

formatting/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package language
4+
package formatting
55

66
import (
77
"testing"

0 commit comments

Comments
 (0)