Skip to content

Commit

Permalink
feat: openapi3: add SpecMore.OperationByPathMethod(), `SpecMore.Spe…
Browse files Browse the repository at this point in the history
…cTagStats()`, `SpecTagStats{}`
  • Loading branch information
grokify committed Oct 19, 2022
1 parent 8247951 commit afabe3d
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions openapi3/spec_more.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package openapi3

import (
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/grokify/gocharts/v2/data/table"
"github.com/grokify/gocharts/v2/data/table/tabulator"
"github.com/grokify/mogo/encoding/jsonutil"
"github.com/grokify/mogo/net/httputilmore"
"github.com/grokify/mogo/net/urlutil"
"github.com/grokify/mogo/type/stringsutil"
)
Expand Down Expand Up @@ -247,6 +249,26 @@ func (sm *SpecMore) OperationByID(wantOperationID string) (path, method string,
return path, method, op, err
}

var (
ErrPathNotFound = errors.New("path not found")
ErrOperationNotFound = errors.New("operation not found")
)

func (sm *SpecMore) OperationByPathMethod(path, method string) (*oas3.Operation, error) {
method = strings.ToUpper(strings.TrimSpace(method))
_, err := httputilmore.ParseHTTPMethod(method)
if err != nil {
return nil, err
}

pathItem, ok := sm.Spec.Paths[path]
if !ok {
return nil, nil
}

return pathItem.GetOperation(method), nil
}

func (sm *SpecMore) SetOperation(path, method string, op *oas3.Operation) {
path = strings.TrimSpace(path)
if strings.Index(path, "/") != 0 {
Expand Down Expand Up @@ -394,6 +416,28 @@ func (sm *SpecMore) ServerURLBasePath(index uint) (string, error) {
return serverURLParsed.Path, nil
}

func (sm *SpecMore) SpecTagStats() SpecTagStats {
stats := SpecTagStats{
TagStats: SpecTagCounts{},
TagsAll: sm.Tags(true, true),
TagsMeta: sm.Tags(true, false),
TagsOps: sm.Tags(false, true),
TagCountsAll: sm.TagsMap(true, true),
TagCountsMeta: sm.TagsMap(true, false),
TagCountsOps: sm.TagsMap(false, true),
}
VisitOperations(sm.Spec, func(skipPath, skipMethod string, op *oas3.Operation) {
op.Tags = stringsutil.SliceCondenseSpace(op.Tags, true, true)
stats.TagStats.OpsTotal++
if len(op.Tags) > 0 {
stats.TagStats.OpsWithTags++
} else {
stats.TagStats.OpsWithoutTags++
}
})
return stats
}

func (sm *SpecMore) Tags(inclTop, inclOps bool) []string {
tags := []string{}
tagsMap := sm.TagsMap(inclTop, inclOps)
Expand All @@ -403,17 +447,16 @@ func (sm *SpecMore) Tags(inclTop, inclOps bool) []string {
return stringsutil.SliceCondenseSpace(tags, true, true)
}

// TagsMap returns a set of tags present in the current spec.
// TagsMap returns a set of operations with tags present in the current spec.
func (sm *SpecMore) TagsMap(inclTop, inclOps bool) map[string]int {
tagsMap := map[string]int{}
if inclTop {
for _, tag := range sm.Spec.Tags {
tagName := strings.TrimSpace(tag.Name)
if len(tagName) > 0 {
if _, ok := tagsMap[tagName]; !ok {
tagsMap[tagName] = 0
tagsMap[tagName] = 0 // don't increment unless present in operations
}
tagsMap[tagName]++
}
}
}
Expand All @@ -438,6 +481,22 @@ type SpecStats struct {
SchemasCount int
}

type SpecTagStats struct {
TagStats SpecTagCounts
TagsAll []string
TagsMeta []string
TagsOps []string
TagCountsAll map[string]int
TagCountsMeta map[string]int
TagCountsOps map[string]int
}

type SpecTagCounts struct {
OpsWithTags int
OpsWithoutTags int
OpsTotal int
}

func (sm *SpecMore) Stats() SpecStats {
return SpecStats{
OperationsCount: sm.OperationsCount(),
Expand Down

0 comments on commit afabe3d

Please sign in to comment.