Skip to content
Snippets Groups Projects
Commit b9f793bb authored by Johan de Klerk's avatar Johan de Klerk
Browse files

Docs: Read comments from functions and structs

parent 7bc39420
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,14 @@ package api_documentation ...@@ -2,7 +2,14 @@ package api_documentation
import ( import (
"fmt" "fmt"
"gitlab.com/uafrica/go-utils/logs"
"go/doc"
"go/parser"
"go/token"
"os"
"path/filepath"
"reflect" "reflect"
"runtime"
"strings" "strings"
"gitlab.com/uafrica/go-utils/errors" "gitlab.com/uafrica/go-utils/errors"
...@@ -56,7 +63,7 @@ type DocSchemaResponse struct { ...@@ -56,7 +63,7 @@ type DocSchemaResponse struct {
Items DocSchema `json:"items"` Items DocSchema `json:"items"`
} }
func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { func GetDocs(endpointHandlers map[string]map[string]interface{}, corePath string) (Docs, error) {
docs := Docs{ docs := Docs{
Paths: map[string]DocPath{}, Paths: map[string]DocPath{},
Components: DocSchemas{Schemas: map[string]interface{}{}}, Components: DocSchemas{Schemas: map[string]interface{}{}},
...@@ -67,6 +74,8 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { ...@@ -67,6 +74,8 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) {
return Docs{}, validationError return Docs{}, validationError
} }
functionDocs := GetStructDocs(corePath)
for path, methods := range endpointHandlers { for path, methods := range endpointHandlers {
docPath := DocPath{} docPath := DocPath{}
for method, methodHandler := range methods { for method, methodHandler := range methods {
...@@ -75,7 +84,8 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { ...@@ -75,7 +84,8 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) {
docMethod.Description = "Not available" docMethod.Description = "Not available"
} else { } else {
// purpose // purpose
docMethod.Description = "Not available - see request and response structs" functionName := GetFunctionName(handler.FuncValue.Interface())
docMethod.Description = functionDocs[functionName]
docMethod.Tags = []string{path} docMethod.Tags = []string{path}
// describe parameters // describe parameters
...@@ -110,7 +120,7 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { ...@@ -110,7 +120,7 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) {
bodyTypeString := getType(body) bodyTypeString := getType(body)
docMethod.RequestBody = &DocRequestBody{ docMethod.RequestBody = &DocRequestBody{
Description: "Some description", Description: functionDocs[bodyTypeString],
Required: true, Required: true,
Content: map[string]interface{}{ Content: map[string]interface{}{
"application/json": map[string]interface{}{ "application/json": map[string]interface{}{
...@@ -135,7 +145,7 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { ...@@ -135,7 +145,7 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) {
responseBodyTypeString := getType(responseBody) responseBodyTypeString := getType(responseBody)
response := DocResponseValue{ response := DocResponseValue{
Description: "Some description", Description: "",
} }
responses["200"] = response responses["200"] = response
...@@ -270,3 +280,60 @@ func StructSchema(t reflect.Type) (interface{}, error) { ...@@ -270,3 +280,60 @@ func StructSchema(t reflect.Type) (interface{}, error) {
return schema, nil return schema, nil
} }
func GetStructDocs(corePath string) map[string]string {
docs := map[string]string{}
walkFunction := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
fset := token.NewFileSet()
d, err := parser.ParseDir(fset, path, nil, parser.ParseComments)
if err != nil {
fmt.Println(err)
return nil
}
for k, f := range d {
fmt.Println("package", k)
p := doc.New(f, "./", 2)
for _, objectTypes := range p.Types {
doc := strings.ReplaceAll(objectTypes.Doc, objectTypes.Name, "")
docs[objectTypes.Name] = doc
}
//
// for _, v := range p.Vars {
// fmt.Println("type", v.Names)
// fmt.Println("docs:", v.Doc)
// }
for _, function := range p.Funcs {
doc := strings.ReplaceAll(function.Doc, function.Name, "")
docs[function.Name] = doc
}
for _, n := range p.Notes {
fmt.Println("body", n[0].Body)
}
}
return nil
}
err := filepath.Walk(corePath, walkFunction)
if err != nil {
logs.ErrorWithMsg("Failed to upload files to s3", err)
}
return docs
}
func GetFunctionName(temp interface{}) string {
strs := strings.Split((runtime.FuncForPC(reflect.ValueOf(temp).Pointer()).Name()), ".")
return strs[len(strs)-1]
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment