diff --git a/api_documentation/api_documentation.go b/api_documentation/api_documentation.go index e50a69c434f99a39e771b9856622e0bd0ec0eef6..c155a8c5911133672aa833e534ef7b95b074feb8 100644 --- a/api_documentation/api_documentation.go +++ b/api_documentation/api_documentation.go @@ -2,7 +2,14 @@ package api_documentation import ( "fmt" + "gitlab.com/uafrica/go-utils/logs" + "go/doc" + "go/parser" + "go/token" + "os" + "path/filepath" "reflect" + "runtime" "strings" "gitlab.com/uafrica/go-utils/errors" @@ -56,7 +63,7 @@ type DocSchemaResponse struct { 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{ Paths: map[string]DocPath{}, Components: DocSchemas{Schemas: map[string]interface{}{}}, @@ -67,6 +74,8 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { return Docs{}, validationError } + functionDocs := GetStructDocs(corePath) + for path, methods := range endpointHandlers { docPath := DocPath{} for method, methodHandler := range methods { @@ -75,7 +84,8 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { docMethod.Description = "Not available" } else { // purpose - docMethod.Description = "Not available - see request and response structs" + functionName := GetFunctionName(handler.FuncValue.Interface()) + docMethod.Description = functionDocs[functionName] docMethod.Tags = []string{path} // describe parameters @@ -110,7 +120,7 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { bodyTypeString := getType(body) docMethod.RequestBody = &DocRequestBody{ - Description: "Some description", + Description: functionDocs[bodyTypeString], Required: true, Content: map[string]interface{}{ "application/json": map[string]interface{}{ @@ -135,7 +145,7 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { responseBodyTypeString := getType(responseBody) response := DocResponseValue{ - Description: "Some description", + Description: "", } responses["200"] = response @@ -270,3 +280,60 @@ func StructSchema(t reflect.Type) (interface{}, error) { 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] +}