Select Git revision
api_documentation.go
api_documentation.go 4.19 KiB
package api_documentation
import (
"fmt"
"reflect"
"strings"
"gitlab.com/uafrica/go-utils/handler_utils"
"gitlab.com/uafrica/go-utils/errors"
)
type NoParams struct{}
type Docs struct {
Paths map[string]DocPath `json:"paths"`
}
type DocPath struct {
Methods map[string]DocMethod `json:"methods"`
}
type DocMethod struct {
Description string `json:"description"`
Parameters map[string]DocParam `json:"parameters,omitempty"`
Request interface{} `json:"request,omitempty"`
Response interface{} `json:"response,omitempty"`
}
type DocParam struct {
Name string
Type string
Description string
}
func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) {
docs := Docs{
Paths: map[string]DocPath{},
}
for path, methods := range endpointHandlers {
docPath := DocPath{
Methods: map[string]DocMethod{},
}
for method, methodHandler := range methods {
docMethod := DocMethod{}
if handler, ok := methodHandler.(handler_utils.Handler); !ok {
docMethod.Description = "Not available"
} else {
//purpose
docMethod.Description = "Not available - see request and response structs"
//describe parameters
docMethod.Parameters = map[string]DocParam{}
for i := 0; i < handler.RequestParamsType.NumField(); i++ {
f := handler.RequestParamsType.Field(i)
name := f.Tag.Get("json")
if name == "" {
name = f.Name
}
docMethod.Parameters[f.Name] = DocParam{
Name: name,
Type: fmt.Sprintf("%v", f.Type),
Description: f.Tag.Get("doc"),
}
}
//describe request schema
var err error