Monster refactor of map to orderedmap.Map data type.

This commit is contained in:
Shawn Poulson
2023-08-30 13:12:03 -04:00
parent 652e818456
commit f389fedadd
91 changed files with 1264 additions and 1192 deletions

View File

@@ -6,24 +6,26 @@ package v2
import (
"crypto/sha256"
"fmt"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
"sort"
"strings"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
)
// Examples represents a low-level Swagger / OpenAPI 2 Example object.
// Allows sharing examples for operation responses
// - https://swagger.io/specification/v2/#exampleObject
type Examples struct {
Values map[low.KeyReference[string]]low.ValueReference[any]
Values orderedmap.Map[low.KeyReference[string], low.ValueReference[any]]
}
// FindExample attempts to locate an example value, using a key label.
func (e *Examples) FindExample(name string) *low.ValueReference[any] {
return low.FindItemInMap[any](name, e.Values)
return low.FindItemInOrderedMap[any](name, e.Values)
}
// Build will extract all examples and will attempt to unmarshal content into a map or slice based on type.
@@ -32,7 +34,7 @@ func (e *Examples) Build(_, root *yaml.Node, _ *index.SpecIndex) error {
utils.CheckForMergeNodes(root)
var keyNode, currNode *yaml.Node
var err error
e.Values = make(map[low.KeyReference[string]]low.ValueReference[any])
e.Values = orderedmap.New[low.KeyReference[string], low.ValueReference[any]]()
for i := range root.Content {
if i%2 == 0 {
keyNode = root.Content[i]
@@ -48,32 +50,40 @@ func (e *Examples) Build(_, root *yaml.Node, _ *index.SpecIndex) error {
// lets just default to interface
var j interface{}
_ = currNode.Decode(&j)
e.Values[low.KeyReference[string]{
Value: keyNode.Value,
KeyNode: keyNode,
}] = low.ValueReference[any]{
Value: j,
ValueNode: currNode,
}
e.Values.Set(
low.KeyReference[string]{
Value: keyNode.Value,
KeyNode: keyNode,
},
low.ValueReference[any]{
Value: j,
ValueNode: currNode,
},
)
continue
}
e.Values[low.KeyReference[string]{
Value: keyNode.Value,
KeyNode: keyNode,
}] = low.ValueReference[any]{
Value: k,
ValueNode: currNode,
}
e.Values.Set(
low.KeyReference[string]{
Value: keyNode.Value,
KeyNode: keyNode,
},
low.ValueReference[any]{
Value: k,
ValueNode: currNode,
},
)
continue
}
e.Values[low.KeyReference[string]{
Value: keyNode.Value,
KeyNode: keyNode,
}] = low.ValueReference[any]{
Value: n,
ValueNode: currNode,
}
e.Values.Set(
low.KeyReference[string]{
Value: keyNode.Value,
KeyNode: keyNode,
},
low.ValueReference[any]{
Value: n,
ValueNode: currNode,
},
)
}
return nil
}
@@ -81,10 +91,10 @@ func (e *Examples) Build(_, root *yaml.Node, _ *index.SpecIndex) error {
// Hash will return a consistent SHA256 Hash of the Examples object
func (e *Examples) Hash() [32]byte {
var f []string
keys := make([]string, len(e.Values))
keys := make([]string, orderedmap.Len(e.Values))
z := 0
for k := range e.Values {
keys[z] = k.Value
for pair := orderedmap.First(e.Values); pair != nil; pair = pair.Next() {
keys[z] = pair.Key().Value
z++
}
sort.Strings(keys)