mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 04:20:24 +00:00
Refactor v2 Paths to parse YAML using TranslatePipeline.
This commit is contained in:
@@ -4,14 +4,18 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"github.com/pb33f/libopenapi/utils"
|
||||
"gopkg.in/yaml.v3"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Paths represents a low-level Swagger / OpenAPI Paths object.
|
||||
@@ -55,65 +59,104 @@ func (p *Paths) Build(_, root *yaml.Node, idx *index.SpecIndex) error {
|
||||
root = utils.NodeAlias(root)
|
||||
utils.CheckForMergeNodes(root)
|
||||
p.Extensions = low.ExtractExtensions(root)
|
||||
skip := false
|
||||
var currentNode *yaml.Node
|
||||
// skip := false
|
||||
// var currentNode *yaml.Node
|
||||
|
||||
pathsMap := make(map[low.KeyReference[string]]low.ValueReference[*PathItem])
|
||||
|
||||
// build each new path, in a new thread.
|
||||
// Translate YAML nodes to pathsMap using `TranslatePipeline`.
|
||||
type pathBuildResult struct {
|
||||
k low.KeyReference[string]
|
||||
v low.ValueReference[*PathItem]
|
||||
key low.KeyReference[string]
|
||||
value low.ValueReference[*PathItem]
|
||||
}
|
||||
type nodeItem struct {
|
||||
currentNode *yaml.Node
|
||||
pathNode *yaml.Node
|
||||
}
|
||||
pathsMap := make(map[low.KeyReference[string]]low.ValueReference[*PathItem])
|
||||
in := make(chan nodeItem)
|
||||
out := make(chan pathBuildResult)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2) // input and output goroutines.
|
||||
|
||||
bChan := make(chan pathBuildResult)
|
||||
eChan := make(chan error)
|
||||
var buildPathItem = func(cNode, pNode *yaml.Node, b chan<- pathBuildResult, e chan<- error) {
|
||||
// TranslatePipeline input.
|
||||
go func() {
|
||||
defer func() {
|
||||
close(in)
|
||||
wg.Done()
|
||||
}()
|
||||
skip := false
|
||||
var currentNode *yaml.Node
|
||||
for i, pathNode := range root.Content {
|
||||
if strings.HasPrefix(strings.ToLower(pathNode.Value), "x-") {
|
||||
skip = true
|
||||
continue
|
||||
}
|
||||
if skip {
|
||||
skip = false
|
||||
continue
|
||||
}
|
||||
if i%2 == 0 {
|
||||
currentNode = pathNode
|
||||
continue
|
||||
}
|
||||
|
||||
select {
|
||||
case in <- nodeItem{
|
||||
currentNode: currentNode,
|
||||
pathNode: pathNode,
|
||||
}:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// TranslatePipeline output.
|
||||
go func() {
|
||||
defer func() {
|
||||
cancel()
|
||||
wg.Done()
|
||||
}()
|
||||
for {
|
||||
select {
|
||||
case result, ok := <-out:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
pathsMap[result.key] = result.value
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
translateFunc := func(value nodeItem) (retval pathBuildResult, _ error) {
|
||||
pNode := value.pathNode
|
||||
cNode := value.currentNode
|
||||
path := new(PathItem)
|
||||
_ = low.BuildModel(pNode, path)
|
||||
err := path.Build(cNode, pNode, idx)
|
||||
if err != nil {
|
||||
e <- err
|
||||
return
|
||||
return retval, err
|
||||
}
|
||||
b <- pathBuildResult{
|
||||
k: low.KeyReference[string]{
|
||||
return pathBuildResult{
|
||||
key: low.KeyReference[string]{
|
||||
Value: cNode.Value,
|
||||
KeyNode: cNode,
|
||||
},
|
||||
v: low.ValueReference[*PathItem]{
|
||||
value: low.ValueReference[*PathItem]{
|
||||
Value: path,
|
||||
ValueNode: pNode,
|
||||
},
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
pathCount := 0
|
||||
for i, pathNode := range root.Content {
|
||||
if strings.HasPrefix(strings.ToLower(pathNode.Value), "x-") {
|
||||
skip = true
|
||||
continue
|
||||
}
|
||||
if skip {
|
||||
skip = false
|
||||
continue
|
||||
}
|
||||
if i%2 == 0 {
|
||||
currentNode = pathNode
|
||||
continue
|
||||
}
|
||||
pathCount++
|
||||
go buildPathItem(currentNode, pathNode, bChan, eChan)
|
||||
}
|
||||
completedItems := 0
|
||||
for completedItems < pathCount {
|
||||
select {
|
||||
case err := <-eChan:
|
||||
return err
|
||||
case res := <-bChan:
|
||||
completedItems++
|
||||
pathsMap[res.k] = res.v
|
||||
}
|
||||
err := datamodel.TranslatePipeline[nodeItem, pathBuildResult](in, out, translateFunc)
|
||||
wg.Wait()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.PathItems = pathsMap
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user