Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
b9f793bb
Commit
b9f793bb
authored
3 years ago
by
Johan de Klerk
Browse files
Options
Downloads
Patches
Plain Diff
Docs: Read comments from functions and structs
parent
7bc39420
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
api_documentation/api_documentation.go
+71
-4
71 additions, 4 deletions
api_documentation/api_documentation.go
with
71 additions
and
4 deletions
api_documentation/api_documentation.go
+
71
−
4
View file @
b9f793bb
...
@@ -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
]
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment