Show embeds in type documentation

This commit is contained in:
2019-11-18 14:55:12 +09:00
parent cafb43aa17
commit ab81556651
9 changed files with 36 additions and 24 deletions

66
pages/api/apitype/type.go Normal file

@ -0,0 +1,66 @@
package apitype
import (
"reflect"
"strings"
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
"github.com/animenotifier/notify.moe/utils/routetests"
)
// ByType renders the api docs page for the given type.
func ByType(typeName string) func(aero.Context) error {
return func(ctx aero.Context) error {
t := arn.API.Type(typeName)
fields := []*utils.APIField{}
embedded := []string{}
if t.Kind() == reflect.Struct {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Anonymous {
embedded = append(embedded, field.Name)
continue
}
if len(field.PkgPath) > 0 {
continue
}
typeName := ""
switch field.Type.Kind() {
case reflect.Ptr:
typeName = field.Type.Elem().Name()
case reflect.Slice:
sliceElementType := field.Type.Elem()
if sliceElementType.Kind() == reflect.Ptr {
sliceElementType = sliceElementType.Elem()
}
typeName = sliceElementType.Name() + "[]"
default:
typeName = field.Type.Name()
}
fields = append(fields, &utils.APIField{
Name: field.Name,
JSON: field.Tag.Get("json"),
Type: typeName,
})
}
}
route := "/api/" + strings.ToLower(typeName) + "/:id"
examples := routetests.All()[route]
return ctx.HTML(components.APIType(t, examples, fields, embedded))
}
}

@ -0,0 +1,39 @@
component APIType(t reflect.Type, examples []string, fields []*utils.APIField, embedded []string)
.api-docs
h1.mountable= t.Name()
h2.mountable Examples
if len(examples) == 0
p.no-data.mountable No examples available yet.
else
.buttons.mountable
each example in examples
a.button(href=example, target="_blank")
Icon("external-link")
span= strings.TrimPrefix(example, "/api")
h2.mountable Fields
table
thead
tr.mountable
th Field
th Type
tbody
each field in fields
tr.api-field.mountable
td.api-field-json(title=field.Name + " (Go)")= field.JSON
td.api-field-type= field.Type
if len(embedded) > 0
h2.mountable Embeds
ul
each embed in embedded
li.mountable= embed
.corner-buttons
a.button(href="/api")
Icon("code")
span Overview

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