Implement entry compacting

This commit is contained in:
Andrey Pokhilko
2022-02-21 21:16:55 +03:00
parent a3357fd589
commit 204ec490ec
2 changed files with 58 additions and 2 deletions

View File

@@ -188,7 +188,7 @@ func (g *SpecGen) handlePathObj(entryWithSource *EntryWithSource) (string, error
}
if entry.Request.Method == "OPTIONS" {
logger.Log.Debugf("Dropped traffic entry due to its method: %s", urlParsed.Path)
logger.Log.Debugf("Dropped traffic entry due to its method: %s %s", entry.Request.Method, urlParsed.Path)
return "", nil
}

View File

@@ -214,7 +214,48 @@ func (n *Node) searchInConstants(pathChunk string) *Node {
}
func (n *Node) compact() {
// TODO
// TODO: introduce and leverage "dirty" flag?
var param *Node
// find the param
for _, subnode := range n.children {
if subnode.constant != nil {
continue
}
param = subnode
}
if param != nil {
// take its regex
pRegex := param.pathParam.Schema.Pattern
if pRegex == nil {
return
}
newChildren := make([]*Node, 0)
// compact the constants via regex
for _, subnode := range n.children {
if subnode.constant != nil {
if pRegex.Match([]byte(*subnode.constant)) {
param.merge(subnode)
continue
}
}
newChildren = append(newChildren, subnode)
}
if len(n.children) != len(newChildren) {
logger.Log.Debugf("Shrinking children from %d to %d", len(n.children), len(newChildren))
n.children = newChildren
n.compact()
}
}
// recurse into next tree level
for _, subnode := range n.children {
subnode.compact()
}
}
func (n *Node) listPaths() *openapi.Paths {
@@ -303,3 +344,18 @@ func (n *Node) countParentParams() int {
}
return res
}
func (n *Node) merge(other *Node) {
outer:
for _, oChild := range other.children {
for _, nChild := range n.children {
matchedConst := oChild.constant != nil && oChild.constant == nChild.constant
matchedParam := oChild.constant == nil && nChild.constant == nil
if matchedConst || matchedParam {
nChild.merge(oChild)
continue outer
}
}
n.children = append(n.children, oChild)
}
}