Skip to content

Commit

Permalink
Add generated time flag (swaggo#501)
Browse files Browse the repository at this point in the history
* feat: add generated time flag
* test(generatetime): add test

Co-authored-by: Bogdan U <[email protected]>
  • Loading branch information
xizhibei and ubogdan committed Dec 27, 2019
1 parent 73e3f31 commit bef48a6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
6 changes: 6 additions & 0 deletions cmd/swag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
parseVendorFlag = "parseVendor"
parseDependencyFlag = "parseDependency"
markdownFilesFlag = "markdownFiles"
generatedTimeFlag = "generatedTime"
)

var initFlags = []cli.Flag{
Expand Down Expand Up @@ -54,6 +55,10 @@ var initFlags = []cli.Flag{
Value: "",
Usage: "Parse folder containing markdown files to use as description, disabled by default",
},
cli.BoolTFlag{
Name: "generatedTime",
Usage: "Generate timestamp at the top of docs.go, true by default",
},
}

func initAction(c *cli.Context) error {
Expand All @@ -73,6 +78,7 @@ func initAction(c *cli.Context) error {
ParseVendor: c.Bool(parseVendorFlag),
ParseDependency: c.Bool(parseDependencyFlag),
MarkdownFilesDir: c.String(markdownFilesFlag),
GeneratedTime: c.BoolT(generatedTimeFlag),
})
}

Expand Down
45 changes: 25 additions & 20 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type Config struct {

// MarkdownFilesDir used to find markdownfiles, which can be used for tag descriptions
MarkdownFilesDir string

// GeneratedTime whether swag should generate the timestamp at the top of docs.go
GeneratedTime bool
}

// Build builds swagger json file for given searchDir and mainAPIFile. Returns json
Expand Down Expand Up @@ -111,7 +114,7 @@ func (g *Gen) Build(config *Config) error {
}

// Write doc
err = g.writeGoDoc(docs, swagger)
err = g.writeGoDoc(docs, swagger, config)
if err != nil {
return err
}
Expand Down Expand Up @@ -142,7 +145,7 @@ func (g *Gen) formatSource(src []byte) []byte {
return code
}

func (g *Gen) writeGoDoc(output io.Writer, swagger *spec.Swagger) error {
func (g *Gen) writeGoDoc(output io.Writer, swagger *spec.Swagger, config *Config) error {

generator, err := template.New("swagger_info").Funcs(template.FuncMap{
"printDoc": func(v string) string {
Expand Down Expand Up @@ -195,23 +198,25 @@ func (g *Gen) writeGoDoc(output io.Writer, swagger *spec.Swagger) error {

buffer := &bytes.Buffer{}
err = generator.Execute(buffer, struct {
Timestamp time.Time
Doc string
Host string
BasePath string
Schemes []string
Title string
Description string
Version string
Timestamp time.Time
GeneratedTime bool
Doc string
Host string
BasePath string
Schemes []string
Title string
Description string
Version string
}{
Timestamp: time.Now(),
Doc: string(buf),
Host: swagger.Host,
BasePath: swagger.BasePath,
Schemes: swagger.Schemes,
Title: swagger.Info.Title,
Description: swagger.Info.Description,
Version: swagger.Info.Version,
Timestamp: time.Now(),
GeneratedTime: config.GeneratedTime,
Doc: string(buf),
Host: swagger.Host,
BasePath: swagger.BasePath,
Schemes: swagger.Schemes,
Title: swagger.Info.Title,
Description: swagger.Info.Description,
Version: swagger.Info.Version,
})
if err != nil {
return err
Expand All @@ -225,8 +230,8 @@ func (g *Gen) writeGoDoc(output io.Writer, swagger *spec.Swagger) error {
}

var packageTemplate = `// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// {{ .Timestamp }}
// This file was generated by swaggo/swag{{ if .GeneratedTime }} at
// {{ .Timestamp }}{{ end }}
package docs
Expand Down
25 changes: 22 additions & 3 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,14 @@ fmt.Print("Helo world")
assert.NotEqual(t, []byte(src2), res, "Should return fmt code")
}

type mocWriter struct{}
type mocWriter struct {
hook func([]byte)
}

func (w *mocWriter) Write(data []byte) (int, error) {
if w.hook != nil {
w.hook(data)
}
return len(data), nil
}

Expand All @@ -274,7 +279,7 @@ func TestGen_writeGoDoc(t *testing.T) {
swapTemplate := packageTemplate

packageTemplate = `{{{`
err := gen.writeGoDoc(nil, nil)
err := gen.writeGoDoc(nil, nil, &Config{})
assert.Error(t, err)

packageTemplate = `{{.Data}}`
Expand All @@ -284,9 +289,23 @@ func TestGen_writeGoDoc(t *testing.T) {
Info: &spec.Info{},
},
}
err = gen.writeGoDoc(&mocWriter{}, swagger)
err = gen.writeGoDoc(&mocWriter{}, swagger, &Config{})
assert.Error(t, err)

packageTemplate = `{{ if .GeneratedTime }}Fake Time{{ end }}`
err = gen.writeGoDoc(&mocWriter{
hook: func(data []byte) {
assert.Equal(t, "Fake Time", string(data))
},
}, swagger, &Config{GeneratedTime: true})
assert.NoError(t, err)
err = gen.writeGoDoc(&mocWriter{
hook: func(data []byte) {
assert.Equal(t, "", string(data))
},
}, swagger, &Config{GeneratedTime: false})
assert.NoError(t, err)

packageTemplate = swapTemplate

}
Expand Down

0 comments on commit bef48a6

Please sign in to comment.