Skip to content

Commit 252fecd

Browse files
skast96skast
andauthored
Use the structs name without the @name comment (#2043)
* Fixed struct naming when using dependency parsing * Fixed naming * Fixed debug message always triggering --------- Co-authored-by: skast <[email protected]>
1 parent b0c5cc9 commit 252fecd

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

cmd/swag/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
outputTypesFlag = "outputTypes"
2525
parseVendorFlag = "parseVendor"
2626
parseDependencyFlag = "parseDependency"
27+
useStructNameFlag = "useStructName"
2728
parseDependencyLevelFlag = "parseDependencyLevel"
2829
markdownFilesFlag = "markdownFiles"
2930
codeExampleFilesFlag = "codeExampleFiles"
@@ -99,6 +100,11 @@ var initFlags = []cli.Flag{
99100
Aliases: []string{"pd"},
100101
Usage: "Parse go files inside dependency folder, disabled by default",
101102
},
103+
&cli.BoolFlag{
104+
Name: useStructNameFlag,
105+
Aliases: []string{"st"},
106+
Usage: "Dont use those ugly full-path names when using dependency flag",
107+
},
102108
&cli.StringFlag{
103109
Name: markdownFilesFlag,
104110
Aliases: []string{"md"},
@@ -251,6 +257,7 @@ func initAction(ctx *cli.Context) error {
251257
ParseDependency: pdv,
252258
MarkdownFilesDir: ctx.String(markdownFilesFlag),
253259
ParseInternal: ctx.Bool(parseInternalFlag),
260+
UseStructNames: ctx.Bool(useStructNameFlag),
254261
GeneratedTime: ctx.Bool(generatedTimeFlag),
255262
RequiredByDefault: ctx.Bool(requiredByDefaultFlag),
256263
CodeExampleFilesDir: ctx.String(codeExampleFilesFlag),

gen/gen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ type Config struct {
108108
// ParseDependencies whether swag should be parse outside dependency folder: 0 none, 1 models, 2 operations, 3 all
109109
ParseDependency int
110110

111+
// UseStructNames stick to the struct name instead of those ugly full-path names
112+
UseStructNames bool
113+
111114
// ParseInternal whether swag should parse internal packages
112115
ParseInternal bool
113116

@@ -198,6 +201,7 @@ func (g *Gen) Build(config *Config) error {
198201

199202
p := swag.New(
200203
swag.SetParseDependency(config.ParseDependency),
204+
swag.SetUseStructName(config.UseStructNames),
201205
swag.SetMarkdownFileDirectory(config.MarkdownFilesDir),
202206
swag.SetDebugger(config.Debugger),
203207
swag.SetExcludedDirsAndFiles(config.Excludes),

parser.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ type Parser struct {
182182

183183
// ParseFuncBody whether swag should parse api info inside of funcs
184184
ParseFuncBody bool
185+
186+
// UseStructName Dont use those ugly full-path names when using dependency flag
187+
UseStructName bool
185188
}
186189

187190
// FieldParserFactory create FieldParser.
@@ -261,6 +264,13 @@ func SetParseDependency(parseDependency int) func(*Parser) {
261264
}
262265
}
263266

267+
// SetUseStructName sets whether to strip the full-path definition name.
268+
func SetUseStructName(useStructName bool) func(*Parser) {
269+
return func(p *Parser) {
270+
p.UseStructName = useStructName
271+
}
272+
}
273+
264274
// SetMarkdownFileDirectory sets the directory to search for markdown files.
265275
func SetMarkdownFileDirectory(directoryPath string) func(*Parser) {
266276
return func(p *Parser) {
@@ -1330,6 +1340,16 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef) (*Schema, error)
13301340
ErrRecursiveParseStruct
13311341
}
13321342

1343+
if parser.UseStructName {
1344+
schemaName := strings.Split(typeSpecDef.SchemaName, ".")
1345+
if len(schemaName) > 1 {
1346+
typeSpecDef.SchemaName = schemaName[len(schemaName)-1]
1347+
typeName = typeSpecDef.SchemaName
1348+
} else {
1349+
parser.debug.Printf("Could not strip type name of %s", typeName)
1350+
}
1351+
}
1352+
13331353
parser.structStack = append(parser.structStack, typeSpecDef)
13341354

13351355
parser.debug.Printf("Generating %s", typeName)

0 commit comments

Comments
 (0)