Skip to content

Commit

Permalink
simplify instance name fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
h44z committed Oct 11, 2021
1 parent c10e08d commit 31380a2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ type Config struct {

// Build builds swagger json file for given searchDir and mainAPIFile. Returns json
func (g *Gen) Build(config *Config) error {
instanceName := "swag.Name"
if config.InstanceName != "" {
instanceName = fmt.Sprintf("%q", config.InstanceName) // use a quoted string
if config.InstanceName == "" {
config.InstanceName = swag.Name
}
config.InstanceName = instanceName

searchDirs := strings.Split(config.SearchDir, ",")
for _, searchDir := range searchDirs {
if _, err := os.Stat(searchDir); os.IsNotExist(err) {
Expand Down Expand Up @@ -334,6 +333,6 @@ func (s *s) ReadDoc() string {
}
func init() {
swag.Register({{ .InstanceName }}, &s{})
swag.Register({{ printf "%q" .InstanceName }}, &s{})
}
`
2 changes: 1 addition & 1 deletion gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestGen_BuildInstanceName(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(expectedCode), "swag.Register(swag.Name, &s{})") {
if !strings.Contains(string(expectedCode), "swag.Register(\"swagger\", &s{})") {
t.Fatal(errors.New("generated go code does not contain the correct default registration sequence"))
}

Expand Down

0 comments on commit 31380a2

Please sign in to comment.