2018-03-27 22:26:06 +00:00
|
|
|
package apidocs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2018-03-27 23:32:49 +00:00
|
|
|
"strings"
|
2018-03-27 22:26:06 +00:00
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
"github.com/animenotifier/notify.moe/components"
|
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2018-03-27 23:32:49 +00:00
|
|
|
"github.com/animenotifier/notify.moe/utils/routetests"
|
2018-03-27 22:26:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ByType renders the api docs page for the given type.
|
|
|
|
func ByType(typeName string) func(*aero.Context) string {
|
|
|
|
return func(ctx *aero.Context) string {
|
|
|
|
t := arn.API.Type(typeName)
|
|
|
|
fields := []*utils.APIField{}
|
|
|
|
|
2018-03-27 23:32:49 +00:00
|
|
|
if t.Kind() == reflect.Struct {
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
field := t.Field(i)
|
2018-03-27 22:26:06 +00:00
|
|
|
|
2018-03-27 23:32:49 +00:00
|
|
|
if field.Anonymous || !unicode.IsUpper(rune(field.Name[0])) {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-27 22:26:06 +00:00
|
|
|
|
2018-03-27 23:32:49 +00:00
|
|
|
typeName := ""
|
2018-03-27 22:26:06 +00:00
|
|
|
|
2018-03-27 23:32:49 +00:00
|
|
|
switch field.Type.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
typeName = field.Type.Elem().Name()
|
2018-03-27 22:26:06 +00:00
|
|
|
|
2018-03-27 23:32:49 +00:00
|
|
|
case reflect.Slice:
|
|
|
|
sliceElementType := field.Type.Elem()
|
2018-03-27 22:26:06 +00:00
|
|
|
|
2018-03-27 23:32:49 +00:00
|
|
|
if sliceElementType.Kind() == reflect.Ptr {
|
|
|
|
sliceElementType = sliceElementType.Elem()
|
|
|
|
}
|
2018-03-27 22:26:06 +00:00
|
|
|
|
2018-03-27 23:32:49 +00:00
|
|
|
typeName = sliceElementType.Name() + "[]"
|
2018-03-27 22:26:06 +00:00
|
|
|
|
2018-03-27 23:32:49 +00:00
|
|
|
default:
|
|
|
|
typeName = field.Type.Name()
|
|
|
|
}
|
2018-03-27 22:26:06 +00:00
|
|
|
|
2018-03-27 23:32:49 +00:00
|
|
|
fields = append(fields, &utils.APIField{
|
|
|
|
Name: field.Name,
|
|
|
|
JSON: field.Tag.Get("json"),
|
|
|
|
Type: typeName,
|
|
|
|
})
|
|
|
|
}
|
2018-03-27 22:26:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 23:32:49 +00:00
|
|
|
route := "/api/" + strings.ToLower(typeName) + "/:id"
|
|
|
|
examples := routetests.All()[route]
|
|
|
|
|
|
|
|
return ctx.HTML(components.APIDocs(t, examples, fields))
|
2018-03-27 22:26:06 +00:00
|
|
|
}
|
|
|
|
}
|