Skip to content

Commit

Permalink
🎨 Logical reordering of entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaynetik committed Jan 29, 2021
1 parent c0cb541 commit 7573f60
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 243 deletions.
94 changes: 93 additions & 1 deletion annotations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package docs

import "fmt"
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)

const (
goFileExt = ".go"
)

// MapAnnotationsInPath scanIn is relevant from initiator calling it.
//
Expand All @@ -20,3 +31,84 @@ func (o *OAS) MapAnnotationsInPath(scanIn string) error {

return nil
}

// scanForChangesInPath scans for annotations changes on handlers in passed path,
// which is relative to the caller's point of view.
func scanForChangesInPath(handlersPath string) (files []string, err error) {
currentPath, err := os.Getwd()
if err != nil {
return files, fmt.Errorf("failed getting current working directory: %w", err)
}

files, err = walkFilepath(filepath.Join(currentPath, handlersPath))
if err != nil {
return files, fmt.Errorf("failed walking tree of the given path: %w", err)
}

return files, nil
}

func walkFilepath(pathToTraverse string) ([]string, error) {
var files []string

err := filepath.Walk(pathToTraverse, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}

if filepath.Ext(path) != goFileExt {
return nil
}

files = append(files, path)
return nil
})
if err != nil {
return files, err //nolint:wrapcheck //it will be wrapped by consumer.
}

return files, nil
}

func (o *OAS) mapDocAnnotations(path string) error {
if o == nil {
return errors.New("pointer to OASHandlers can not be nil") // fixme: migrate to validator!
}

f, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open file in path %s :%w", path, err)
}
defer f.Close() // FIXME: Consume this error.

scanner := bufio.NewScanner(f)

line := 1

for scanner.Scan() {
mapIfLineContainsOASTag(scanner.Text(), o)
line++
}

err = scanner.Err()
if err != nil {
return fmt.Errorf("scanner failure :%w", err)
}

return nil
}

func mapIfLineContainsOASTag(lineText string, o *OAS) {
if strings.Contains(lineText, OASAnnotationInit) {
// TODO: Can this be more performance cautious?
fields := strings.Fields(lineText)

// TODO: Implement getters for these fields?
var newRoute Path
newRoute.handlerFuncName = fields[2]
newRoute.Route = fields[3]
newRoute.HTTPMethod = fields[4]

o.Paths = append(o.Paths, newRoute)
}
}
12 changes: 6 additions & 6 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (o *OAS) transformToMap() map[string]interface{} {
reqBodyMap["description"] = path.RequestBody.Description
reqBodyMap["content"] = makeContentSchemaMap(path.RequestBody.Content)

responsesMap := make(map[uint]interface{})
responsesMap := make(map[uint]interface{}, len(path.Responses))

for _, resp := range path.Responses {
codeBodyMap := make(map[string]interface{})
Expand Down Expand Up @@ -106,10 +106,10 @@ func (o *OAS) transformToMap() map[string]interface{} {

oasPrep["paths"] = allPaths

componentsMap := make(map[string]interface{})
componentsMap := make(map[string]interface{}, len(o.Components))

for _, cm := range o.Components {
schemesMap := make(map[string]interface{})
schemesMap := make(map[string]interface{}, len(cm.Schemas))

for _, s := range cm.Schemas {
scheme := make(map[string]interface{})
Expand All @@ -124,7 +124,7 @@ func (o *OAS) transformToMap() map[string]interface{} {
schemesMap[s.Name] = scheme
}

secSchemesMap := make(map[string]interface{})
secSchemesMap := make(map[string]interface{}, len(cm.SecuritySchemes))

for _, ss := range cm.SecuritySchemes {
scheme := make(map[string]interface{})
Expand All @@ -151,10 +151,10 @@ func makeContentSchemaMap(content ContentTypes) map[string]interface{} {
contentSchemaMap := make(map[string]interface{})

for _, ct := range content {
refMap := make(map[string]interface{})
refMap := make(map[string]string)
refMap["$ref"] = ct.Schema

schemaMap := make(map[string]interface{})
schemaMap := make(map[string]map[string]string)
schemaMap["schema"] = refMap

contentSchemaMap[ct.Name] = schemaMap
Expand Down
140 changes: 0 additions & 140 deletions impl.go

This file was deleted.

87 changes: 0 additions & 87 deletions internal/dist/openapi.yaml

This file was deleted.

Loading

0 comments on commit 7573f60

Please sign in to comment.