Show embeds in type documentation

This commit is contained in:
Eduard Urbach 2019-11-18 14:55:12 +09:00
parent cafb43aa17
commit ab81556651
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
9 changed files with 36 additions and 24 deletions

View File

@ -16,7 +16,7 @@ type Type struct {
// Endpoint returns the REST endpoint for that type. // Endpoint returns the REST endpoint for that type.
func (typ *Type) Endpoint() string { func (typ *Type) Endpoint() string {
return "/api/" + strings.ToLower(typ.Name) + "/" return "/api/" + strings.ToLower(typ.Name)
} }
// GitHubLink returns link to display the type in GitHub. // GitHubLink returns link to display the type in GitHub.

View File

@ -1,4 +1,4 @@
package apiview package api
import ( import (
"path" "path"

View File

@ -1,5 +1,5 @@
component API(types []*autodocs.Type) component API(types []*autodocs.Type)
.api-docs-page .api-docs
h1.mountable API h1.mountable API
h2.mountable Endpoints h2.mountable Endpoints

View File

@ -1,4 +1,4 @@
.api-docs-page .api-docs
h1, h1,
h2, h2,
p p
@ -10,11 +10,5 @@
.buttons .buttons
justify-content flex-start justify-content flex-start
.api-field .api-types
// // ...
.api-field-json
//
.api-field-type
opacity 0.5

View File

@ -1,9 +1,8 @@
package apidocs package apitype
import ( import (
"reflect" "reflect"
"strings" "strings"
"unicode"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/notify.moe/arn"
@ -17,12 +16,18 @@ func ByType(typeName string) func(aero.Context) error {
return func(ctx aero.Context) error { return func(ctx aero.Context) error {
t := arn.API.Type(typeName) t := arn.API.Type(typeName)
fields := []*utils.APIField{} fields := []*utils.APIField{}
embedded := []string{}
if t.Kind() == reflect.Struct { if t.Kind() == reflect.Struct {
for i := 0; i < t.NumField(); i++ { for i := 0; i < t.NumField(); i++ {
field := t.Field(i) field := t.Field(i)
if field.Anonymous || !unicode.IsUpper(rune(field.Name[0])) { if field.Anonymous {
embedded = append(embedded, field.Name)
continue
}
if len(field.PkgPath) > 0 {
continue continue
} }
@ -56,6 +61,6 @@ func ByType(typeName string) func(aero.Context) error {
route := "/api/" + strings.ToLower(typeName) + "/:id" route := "/api/" + strings.ToLower(typeName) + "/:id"
examples := routetests.All()[route] examples := routetests.All()[route]
return ctx.HTML(components.APIDocs(t, examples, fields)) return ctx.HTML(components.APIType(t, examples, fields, embedded))
} }
} }

View File

@ -1,5 +1,5 @@
component APIDocs(t reflect.Type, examples []string, fields []*utils.APIField) component APIType(t reflect.Type, examples []string, fields []*utils.APIField, embedded []string)
.api-docs-page .api-docs
h1.mountable= t.Name() h1.mountable= t.Name()
h2.mountable Examples h2.mountable Examples
@ -25,6 +25,13 @@ component APIDocs(t reflect.Type, examples []string, fields []*utils.APIField)
tr.api-field.mountable tr.api-field.mountable
td.api-field-json(title=field.Name + " (Go)")= field.JSON td.api-field-json(title=field.Name + " (Go)")= field.JSON
td.api-field-type= field.Type td.api-field-type= field.Type
if len(embedded) > 0
h2.mountable Embeds
ul
each embed in embedded
li.mountable= embed
.corner-buttons .corner-buttons
a.button(href="/api") a.button(href="/api")

View File

@ -0,0 +1,8 @@
.api-field
//
.api-field-json
//
.api-field-type
opacity 0.5

View File

@ -1,2 +0,0 @@
.api-types
// ...

View File

@ -7,8 +7,8 @@ import (
"github.com/animenotifier/notify.moe/arn" "github.com/animenotifier/notify.moe/arn"
"github.com/animenotifier/notify.moe/pages/animeimport" "github.com/animenotifier/notify.moe/pages/animeimport"
"github.com/animenotifier/notify.moe/pages/animelist" "github.com/animenotifier/notify.moe/pages/animelist"
"github.com/animenotifier/notify.moe/pages/apiview" "github.com/animenotifier/notify.moe/pages/api"
"github.com/animenotifier/notify.moe/pages/apiview/apidocs" "github.com/animenotifier/notify.moe/pages/api/apitype"
"github.com/animenotifier/notify.moe/pages/character" "github.com/animenotifier/notify.moe/pages/character"
"github.com/animenotifier/notify.moe/pages/database" "github.com/animenotifier/notify.moe/pages/database"
"github.com/animenotifier/notify.moe/pages/editor/jobs" "github.com/animenotifier/notify.moe/pages/editor/jobs"
@ -26,10 +26,10 @@ import (
// Register registers the page routes. // Register registers the page routes.
func Register(app *aero.Application) { func Register(app *aero.Application) {
// API pages // API pages
page.Get(app, "/api", apiview.Get) page.Get(app, "/api", api.Get)
for name := range arn.DB.Types() { for name := range arn.DB.Types() {
page.Get(app, "/api/"+strings.ToLower(name), apidocs.ByType(name)) page.Get(app, "/api/"+strings.ToLower(name), apitype.ByType(name))
} }
// API // API