mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 20:47:49 +00:00
Tidy code.
This commit is contained in:
@@ -38,6 +38,11 @@ type Components struct {
|
|||||||
*low.Reference
|
*low.Reference
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type componentBuildResult[T any] struct {
|
||||||
|
key low.KeyReference[string]
|
||||||
|
value low.ValueReference[T]
|
||||||
|
}
|
||||||
|
|
||||||
// GetExtensions returns all Components extensions and satisfies the low.HasExtensions interface.
|
// GetExtensions returns all Components extensions and satisfies the low.HasExtensions interface.
|
||||||
func (co *Components) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
|
func (co *Components) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
|
||||||
return co.Extensions
|
return co.Extensions
|
||||||
@@ -210,22 +215,18 @@ func (co *Components) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
|||||||
return reterr
|
return reterr
|
||||||
}
|
}
|
||||||
|
|
||||||
type componentBuildResult[T any] struct {
|
|
||||||
k low.KeyReference[string]
|
|
||||||
v low.ValueReference[T]
|
|
||||||
}
|
|
||||||
|
|
||||||
// extractComponentValues converts all the YAML nodes of a component type to
|
// extractComponentValues converts all the YAML nodes of a component type to
|
||||||
// low level model.
|
// low level model.
|
||||||
// Process each node in parallel.
|
// Process each node in parallel.
|
||||||
func extractComponentValues[T low.Buildable[N], N any](label string, root *yaml.Node, idx *index.SpecIndex) (retval low.NodeReference[map[low.KeyReference[string]]low.ValueReference[T]], _ error) {
|
func extractComponentValues[T low.Buildable[N], N any](label string, root *yaml.Node, idx *index.SpecIndex) (low.NodeReference[map[low.KeyReference[string]]low.ValueReference[T]], error) {
|
||||||
|
var emptyResult low.NodeReference[map[low.KeyReference[string]]low.ValueReference[T]]
|
||||||
_, nodeLabel, nodeValue := utils.FindKeyNodeFullTop(label, root.Content)
|
_, nodeLabel, nodeValue := utils.FindKeyNodeFullTop(label, root.Content)
|
||||||
if nodeValue == nil {
|
if nodeValue == nil {
|
||||||
return retval, nil
|
return emptyResult, nil
|
||||||
}
|
}
|
||||||
componentValues := make(map[low.KeyReference[string]]low.ValueReference[T])
|
componentValues := make(map[low.KeyReference[string]]low.ValueReference[T])
|
||||||
if utils.IsNodeArray(nodeValue) {
|
if utils.IsNodeArray(nodeValue) {
|
||||||
return retval, fmt.Errorf("node is array, cannot be used in components: line %d, column %d", nodeValue.Line, nodeValue.Column)
|
return emptyResult, fmt.Errorf("node is array, cannot be used in components: line %d, column %d", nodeValue.Line, nodeValue.Column)
|
||||||
}
|
}
|
||||||
|
|
||||||
type inputValue struct {
|
type inputValue struct {
|
||||||
@@ -270,14 +271,14 @@ func extractComponentValues[T low.Buildable[N], N any](label string, root *yaml.
|
|||||||
// Collect output.
|
// Collect output.
|
||||||
go func() {
|
go func() {
|
||||||
for result := range out {
|
for result := range out {
|
||||||
componentValues[result.k] = result.v
|
componentValues[result.key] = result.value
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Translate.
|
// Translate.
|
||||||
translateFunc := func(value inputValue) (retval componentBuildResult[T], _ error) {
|
translateFunc := func(value inputValue) (componentBuildResult[T], error) {
|
||||||
var n T = new(N)
|
var n T = new(N)
|
||||||
currentLabel := value.currentLabel
|
currentLabel := value.currentLabel
|
||||||
node := value.node
|
node := value.node
|
||||||
@@ -290,21 +291,21 @@ func extractComponentValues[T low.Buildable[N], N any](label string, root *yaml.
|
|||||||
node, err = low.LocateRefNode(node, idx)
|
node, err = low.LocateRefNode(node, idx)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return retval, err
|
return componentBuildResult[T]{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// build.
|
// build.
|
||||||
_ = low.BuildModel(node, n)
|
_ = low.BuildModel(node, n)
|
||||||
err = n.Build(currentLabel, node, idx)
|
err = n.Build(currentLabel, node, idx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return retval, err
|
return componentBuildResult[T]{}, err
|
||||||
}
|
}
|
||||||
return componentBuildResult[T]{
|
return componentBuildResult[T]{
|
||||||
k: low.KeyReference[string]{
|
key: low.KeyReference[string]{
|
||||||
KeyNode: currentLabel,
|
KeyNode: currentLabel,
|
||||||
Value: currentLabel.Value,
|
Value: currentLabel.Value,
|
||||||
},
|
},
|
||||||
v: low.ValueReference[T]{
|
value: low.ValueReference[T]{
|
||||||
Value: n,
|
Value: n,
|
||||||
ValueNode: node,
|
ValueNode: node,
|
||||||
},
|
},
|
||||||
@@ -313,7 +314,7 @@ func extractComponentValues[T low.Buildable[N], N any](label string, root *yaml.
|
|||||||
err := datamodel.TranslatePipeline[inputValue, componentBuildResult[T]](in, out, translateFunc)
|
err := datamodel.TranslatePipeline[inputValue, componentBuildResult[T]](in, out, translateFunc)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return retval, err
|
return emptyResult, err
|
||||||
}
|
}
|
||||||
|
|
||||||
results := low.NodeReference[map[low.KeyReference[string]]low.ValueReference[T]]{
|
results := low.NodeReference[map[low.KeyReference[string]]low.ValueReference[T]]{
|
||||||
|
|||||||
@@ -68,17 +68,17 @@ func (p *Paths) Build(_, root *yaml.Node, idx *index.SpecIndex) error {
|
|||||||
p.Extensions = low.ExtractExtensions(root)
|
p.Extensions = low.ExtractExtensions(root)
|
||||||
|
|
||||||
// Translate YAML nodes to pathsMap using `TranslatePipeline`.
|
// Translate YAML nodes to pathsMap using `TranslatePipeline`.
|
||||||
type pathBuildResult struct {
|
type buildResult struct {
|
||||||
k low.KeyReference[string]
|
k low.KeyReference[string]
|
||||||
v low.ValueReference[*PathItem]
|
v low.ValueReference[*PathItem]
|
||||||
}
|
}
|
||||||
type nodeItem struct {
|
type inputValue struct {
|
||||||
currentNode *yaml.Node
|
currentNode *yaml.Node
|
||||||
pathNode *yaml.Node
|
pathNode *yaml.Node
|
||||||
}
|
}
|
||||||
pathsMap := make(map[low.KeyReference[string]]low.ValueReference[*PathItem])
|
pathsMap := make(map[low.KeyReference[string]]low.ValueReference[*PathItem])
|
||||||
in := make(chan nodeItem)
|
in := make(chan inputValue)
|
||||||
out := make(chan pathBuildResult)
|
out := make(chan buildResult)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
@@ -107,7 +107,7 @@ func (p *Paths) Build(_, root *yaml.Node, idx *index.SpecIndex) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case in <- nodeItem{
|
case in <- inputValue{
|
||||||
currentNode: currentNode,
|
currentNode: currentNode,
|
||||||
pathNode: pathNode,
|
pathNode: pathNode,
|
||||||
}:
|
}:
|
||||||
@@ -136,8 +136,8 @@ func (p *Paths) Build(_, root *yaml.Node, idx *index.SpecIndex) error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := datamodel.TranslatePipeline[nodeItem, pathBuildResult](in, out,
|
err := datamodel.TranslatePipeline[inputValue, buildResult](in, out,
|
||||||
func(value nodeItem) (pathBuildResult, error) {
|
func(value inputValue) (buildResult, error) {
|
||||||
pNode := value.pathNode
|
pNode := value.pathNode
|
||||||
cNode := value.currentNode
|
cNode := value.currentNode
|
||||||
|
|
||||||
@@ -153,11 +153,11 @@ func (p *Paths) Build(_, root *yaml.Node, idx *index.SpecIndex) error {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !idx.AllowCircularReferenceResolving() {
|
if !idx.AllowCircularReferenceResolving() {
|
||||||
return pathBuildResult{}, fmt.Errorf("path item build failed: %s", err.Error())
|
return buildResult{}, fmt.Errorf("path item build failed: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return pathBuildResult{}, fmt.Errorf("path item build failed: cannot find reference: %s at line %d, col %d",
|
return buildResult{}, fmt.Errorf("path item build failed: cannot find reference: %s at line %d, col %d",
|
||||||
pNode.Content[1].Value, pNode.Content[1].Line, pNode.Content[1].Column)
|
pNode.Content[1].Value, pNode.Content[1].Line, pNode.Content[1].Column)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -166,10 +166,10 @@ func (p *Paths) Build(_, root *yaml.Node, idx *index.SpecIndex) error {
|
|||||||
_ = low.BuildModel(pNode, path)
|
_ = low.BuildModel(pNode, path)
|
||||||
err := path.Build(cNode, pNode, idx)
|
err := path.Build(cNode, pNode, idx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return pathBuildResult{}, err
|
return buildResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return pathBuildResult{
|
return buildResult{
|
||||||
k: low.KeyReference[string]{
|
k: low.KeyReference[string]{
|
||||||
Value: cNode.Value,
|
Value: cNode.Value,
|
||||||
KeyNode: cNode,
|
KeyNode: cNode,
|
||||||
|
|||||||
Reference in New Issue
Block a user