Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental: Support API Blueprint output #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions _example/doc/apib/validate.apib
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Example API (with validation)

# POST /v1/user
Create a new user
+ Name - User Name
+ Email - User email address
+ Attribute.Birthday - User birthday YYYY-MM-DD format


+ Parameters
+ pretty - Pretty print response message
+ token - Request token

+ Request (application/json)

+ Headers
X-Version: 2


+ Body
{
"name": "tcnksm",
"email": "[email protected]",
"attribute": {
"birthday": "1988-11-24"
}
}

+ Response 200

+ Headers
Content-Type: application/json


+ Body
{
"id": 11241988,
"name": "tcnksm"
}


6 changes: 6 additions & 0 deletions _example/handler_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func TestUserHandlerWithValidate(t *testing.T) {
if err := document.Generate("doc/validate.md"); err != nil {
t.Fatalf("err: %s", err)
}

// This is still experimental.
document.Template = httpdoc.ExperimentalTmplAPIBlueprint
if err := document.Generate("doc/apib/validate.apib"); err != nil {
t.Fatalf("err: %s", err)
}
}()

mux := http.NewServeMux()
Expand Down
4 changes: 2 additions & 2 deletions httpdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type Document struct {
// This is exported just for templating.
Entries []Entry

// tmpl is template file to use. Currently this is only static/tmpl/doc.md.tmpl
tmpl string
// Template is template file to use. Default is TmplMarkdown.
Template builtinTmpl

logger *log.Logger
}
Expand Down
43 changes: 33 additions & 10 deletions static/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions static/tmpl/api-blueprint.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# {{ .Name }}

{{ range .Entries -}}

# {{ .Method }} {{ .Path }}
{{ .Description }}
{{ if .RequestFields -}}
{{ range .RequestFields -}}
+ {{ .Name }} - {{ .Description }}
{{ end }}
{{ end }}
{{ if .RequestParams -}}
+ Parameters
{{ range .RequestParams }} + {{ .Name }} - {{ .Description }}
{{ end }}{{ end }}
+ Request (application/json)
{{ if .RequestHeaders }}
+ Headers
{{ range .RequestHeaders }} {{ .Name }}: {{ .Value }}
{{ end }}
{{ end }}
+ Body
{{ .RequestExample }}
+ Response {{ .ResponseStatusCode }}
{{ if .ResponseHeaders }}
+ Headers
{{ range .ResponseHeaders }} {{ .Name }}: {{ .Value }}
{{ end }}
{{ end }}
+ Body
{{ .ResponseExample }}
{{ end }}
File renamed without changes.
20 changes: 16 additions & 4 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@ import (
"github.com/mercari/go-httpdoc/static"
)

const (
// TmplMarkdown is markdown tempalte.
TmplMarkdown builtinTmpl = "tmpl/markdown.tmpl"

// ExperimentalTmplAPIBlueprint is experimental support for API blueprint template.
// This maybe be modified or deleted.
ExperimentalTmplAPIBlueprint builtinTmpl = "tmpl/api-blueprint.tmpl"
)

// defaultTmpl is default template file to use.
var defaultTmpl = "tmpl/doc.md.tmpl"
var defaultTmpl = TmplMarkdown

// builtinTmpl is builtin template.
type builtinTmpl string

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

func (d *Document) generate(w io.Writer) error {
if d.tmpl == "" {
d.tmpl = defaultTmpl
if d.Template == "" {
d.Template = defaultTmpl
}

buf, err := static.Asset(d.tmpl)
buf, err := static.Asset(string(d.Template))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestTemplateGenerate_InvalidPath(t *testing.T) {

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