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

37
pages/api/api.go Normal file
View File

@ -0,0 +1,37 @@
package api
import (
"path"
"sort"
"github.com/aerogo/aero"
"github.com/akyoto/color"
"github.com/animenotifier/notify.moe/arn"
"github.com/animenotifier/notify.moe/arn/autodocs"
"github.com/animenotifier/notify.moe/components"
)
// Get api page.
func Get(ctx aero.Context) error {
types := []*autodocs.Type{}
for typeName := range arn.DB.Types() {
if typeName == "Session" {
continue
}
typ, err := autodocs.GetTypeDocumentation(typeName, path.Join(arn.Root, "arn", typeName+".go"))
types = append(types, typ)
if err != nil {
color.Red(err.Error())
continue
}
}
sort.Slice(types, func(i, j int) bool {
return types[i].Name < types[j].Name
})
return ctx.HTML(components.API(types))
}

24
pages/api/api.pixy Normal file
View File

@ -0,0 +1,24 @@
component API(types []*autodocs.Type)
.api-docs
h1.mountable API
h2.mountable Endpoints
ul
li.mountable
strong REST:
span https://notify.moe/api
li.mountable
strong GraphQL:
span https://notify.moe/graphql
h2.mountable Types
table.api-types
tbody
each typ in types
tr.mountable
td
a(href=typ.Endpoint())= typ.Name
td= typ.Comment
td
a(href=typ.GitHubLink())
RawIcon("github")

14
pages/api/api.scarlet Normal file
View File

@ -0,0 +1,14 @@
.api-docs
h1,
h2,
p
text-align left
table
margin 0
.buttons
justify-content flex-start
.api-types
// ...

66
pages/api/apitype/type.go Normal file
View 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))
}
}

View File

@ -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

View File

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