github.com/TykTechnologies/tyk/internal/oasutil
No package summary is available.
Package
Files: 1. Third party imports: 0. Imports from organisation: 1. Tests: 0. Benchmarks: 0.
Vars
var pathParamRegex = regexp.MustCompile(`\{[^}]+\}`)
Types
PathItem
PathItem holds the path to a particular OAS path item.
| Field name | Field type | Comment |
|---|---|---|
|
PathItem represents an openapi3.Paths value. |
|
| Path |
|
Path is an openapi3.Paths key, the endpoint URL. |
type PathItem struct {
// PathItem represents an openapi3.Paths value.
*openapi3.PathItem
// Path is an openapi3.Paths key, the endpoint URL.
Path string
}
Functions
func ExtractPaths
ExtractPaths will extract paths with the given order.
func ExtractPaths(in openapi3.Paths, order []string) []PathItem {
// collect url and pathItem
result := []PathItem{}
for _, v := range order {
value := PathItem{
PathItem: in[v],
Path: v,
}
result = append(result, value)
}
return result
}
Cognitive complexity: 5, Cyclomatic complexity: 2
func SortByPathLength
SortByPathLength decomposes an openapi3.Paths to a sorted []PathItem. The sorting takes the length of the paths into account, as well as path parameters, sorting them by length descending, and ordering path parameters after the statically defined paths.
Check the test function for sorting expectations.
func SortByPathLength(in openapi3.Paths) []PathItem {
// get urls
paths := []string{}
for k := range in {
paths = append(paths, k)
}
// sort by length and lexicographically
sort.Slice(paths, func(i, j int) bool {
pathI := pathParamRegex.ReplaceAllString(paths[i], "")
pathJ := pathParamRegex.ReplaceAllString(paths[j], "")
// handle /sub and /sub{id} order with raw path.
if pathI == pathJ {
// we're reversing indexes here so path with
// parameter is sorted after the literal.
pathI, pathJ = paths[j], paths[i]
}
// sort by number of path fragments
k, v := strings.Count(pathI, "/"), strings.Count(pathJ, "/")
if k != v {
return k > v
}
il, jl := len(pathI), len(pathJ)
if il == jl {
return pathI < pathJ
}
return il > jl
})
return ExtractPaths(in, paths)
}
Cognitive complexity: 11, Cyclomatic complexity: 5
Tests
Files: 1. Third party imports: 1. Imports from organisation: 1. Tests: 2. Benchmarks: 0.