Skip to content

Commit 9e7d391

Browse files
committed
Experimental: Support API Blueprint output
1 parent df39848 commit 9e7d391

8 files changed

Lines changed: 128 additions & 17 deletions

File tree

_example/doc/apib/validate.apib

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Example API (with validation)
2+
3+
# POST /v1/user
4+
Create a new user
5+
+ Name - User Name
6+
+ Email - User email address
7+
+ Attribute.Birthday - User birthday YYYY-MM-DD format
8+
9+
10+
+ Parameters
11+
+ pretty - Pretty print response message
12+
+ token - Request token
13+
14+
+ Request (application/json)
15+
16+
+ Headers
17+
X-Version: 2
18+
19+
20+
+ Body
21+
{
22+
"name": "tcnksm",
23+
"email": "tcnksm@mercari.com",
24+
"attribute": {
25+
"birthday": "1988-11-24"
26+
}
27+
}
28+
29+
+ Response 200
30+
31+
+ Headers
32+
Content-Type: application/json
33+
34+
35+
+ Body
36+
{
37+
"id": 11241988,
38+
"name": "tcnksm"
39+
}
40+
41+

_example/handler_validate_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ func TestUserHandlerWithValidate(t *testing.T) {
1919
if err := document.Generate("doc/validate.md"); err != nil {
2020
t.Fatalf("err: %s", err)
2121
}
22+
23+
// This is still experimental.
24+
document.Template = httpdoc.ExperimentalTmplAPIBlueprint
25+
if err := document.Generate("doc/apib/validate.apib"); err != nil {
26+
t.Fatalf("err: %s", err)
27+
}
2228
}()
2329

2430
mux := http.NewServeMux()

httpdoc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ type Document struct {
3333
// This is exported just for templating.
3434
Entries []Entry
3535

36-
// tmpl is template file to use. Currently this is only static/tmpl/doc.md.tmpl
37-
tmpl string
36+
// Template is template file to use. Default is TmplMarkdown.
37+
Template string
3838

3939
logger *log.Logger
4040
}

static/bindata.go

Lines changed: 33 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/tmpl/api-blueprint.tmpl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# {{ .Name }}
2+
3+
{{ range .Entries -}}
4+
5+
# {{ .Method }} {{ .Path }}
6+
{{ .Description }}
7+
{{ if .RequestFields -}}
8+
{{ range .RequestFields -}}
9+
+ {{ .Name }} - {{ .Description }}
10+
{{ end }}
11+
{{ end }}
12+
{{ if .RequestParams -}}
13+
+ Parameters
14+
{{ range .RequestParams }} + {{ .Name }} - {{ .Description }}
15+
{{ end }}{{ end }}
16+
+ Request (application/json)
17+
{{ if .RequestHeaders }}
18+
+ Headers
19+
{{ range .RequestHeaders }} {{ .Name }}: {{ .Value }}
20+
{{ end }}
21+
{{ end }}
22+
+ Body
23+
{{ .RequestExample }}
24+
+ Response {{ .ResponseStatusCode }}
25+
{{ if .ResponseHeaders }}
26+
+ Headers
27+
{{ range .ResponseHeaders }} {{ .Name }}: {{ .Value }}
28+
{{ end }}
29+
{{ end }}
30+
+ Body
31+
{{ .ResponseExample }}
32+
{{ end }}

template.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@ import (
1010
"github.com/mercari/go-httpdoc/static"
1111
)
1212

13+
const (
14+
// TmplMarkdown is markdown tempalte.
15+
TmplMarkdown = "tmpl/markdown.tmpl"
16+
17+
// ExperimentalTmplAPIBlueprint is experimental support for API blueprint template.
18+
// This maybe be modified or deleted.
19+
ExperimentalTmplAPIBlueprint = "tmpl/api-blueprint.tmpl"
20+
)
21+
1322
// defaultTmpl is default template file to use.
14-
var defaultTmpl = "tmpl/doc.md.tmpl"
23+
var defaultTmpl = TmplMarkdown
1524

1625
// Generate writes documentation into the given file. Generation is skipped
1726
// if EnvHTTPDoc is empty. If directory does not exist or any, it returns error.
@@ -32,11 +41,11 @@ func (d *Document) Generate(path string) error {
3241
}
3342

3443
func (d *Document) generate(w io.Writer) error {
35-
if d.tmpl == "" {
36-
d.tmpl = defaultTmpl
44+
if d.Template == "" {
45+
d.Template = defaultTmpl
3746
}
3847

39-
buf, err := static.Asset(d.tmpl)
48+
buf, err := static.Asset(d.Template)
4049
if err != nil {
4150
return err
4251
}

template_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestTemplateGenerate_InvalidPath(t *testing.T) {
9292

9393
func TestTemplateGenerate_InvalidTmpl(t *testing.T) {
9494
doc := &Document{
95-
tmpl: "no-such-template",
95+
Template: "no-such-template",
9696
}
9797
if err := doc.generate(ioutil.Discard); err == nil {
9898
t.Fatalf("expect to be failed")

0 commit comments

Comments
 (0)