fix: continued moving everything to orderedmaps plus cleaned up most the tests

This commit is contained in:
Tristan Cartledge
2023-12-01 17:37:07 +00:00
parent 0f3d0cb28f
commit a4ad09aab3
169 changed files with 3435 additions and 3764 deletions

View File

@@ -7,12 +7,14 @@ import (
"context"
"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"
)
// Header Represents a low-level Swagger / OpenAPI 2 Header object.
@@ -25,7 +27,7 @@ type Header struct {
Description low.NodeReference[string]
Items low.NodeReference[*Items]
CollectionFormat low.NodeReference[string]
Default low.NodeReference[any]
Default low.NodeReference[*yaml.Node]
Maximum low.NodeReference[int]
ExclusiveMaximum low.NodeReference[bool]
Minimum low.NodeReference[int]
@@ -36,18 +38,18 @@ type Header struct {
MaxItems low.NodeReference[int]
MinItems low.NodeReference[int]
UniqueItems low.NodeReference[bool]
Enum low.NodeReference[[]low.ValueReference[any]]
Enum low.NodeReference[[]low.ValueReference[*yaml.Node]]
MultipleOf low.NodeReference[int]
Extensions map[low.KeyReference[string]]low.ValueReference[any]
Extensions *orderedmap.Map[low.KeyReference[string], low.ValueReference[*yaml.Node]]
}
// FindExtension will attempt to locate an extension value using a name lookup.
func (h *Header) FindExtension(ext string) *low.ValueReference[any] {
return low.FindItemInMap[any](ext, h.Extensions)
func (h *Header) FindExtension(ext string) *low.ValueReference[*yaml.Node] {
return low.FindItemInOrderedMap(ext, h.Extensions)
}
// GetExtensions returns all Header extensions and satisfies the low.HasExtensions interface.
func (h *Header) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
func (h *Header) GetExtensions() *orderedmap.Map[low.KeyReference[string], low.ValueReference[*yaml.Node]] {
return h.Extensions
}
@@ -64,37 +66,14 @@ func (h *Header) Build(ctx context.Context, _, root *yaml.Node, idx *index.SpecI
_, ln, vn := utils.FindKeyNodeFull(DefaultLabel, root.Content)
if vn != nil {
var n map[string]interface{}
err = vn.Decode(&n)
if err != nil {
// if not a map, then try an array
var k []interface{}
err = vn.Decode(&k)
if err != nil {
// lets just default to interface
var j interface{}
_ = vn.Decode(&j)
h.Default = low.NodeReference[any]{
Value: j,
KeyNode: ln,
ValueNode: vn,
}
return nil
}
h.Default = low.NodeReference[any]{
Value: k,
KeyNode: ln,
ValueNode: vn,
}
return nil
}
h.Default = low.NodeReference[any]{
Value: n,
h.Default = low.NodeReference[*yaml.Node]{
Value: vn,
KeyNode: ln,
ValueNode: vn,
}
return nil
}
return nil
}
@@ -113,8 +92,8 @@ func (h *Header) Hash() [32]byte {
if h.CollectionFormat.Value != "" {
f = append(f, h.CollectionFormat.Value)
}
if h.Default.Value != "" {
f = append(f, fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint(h.Default.Value)))))
if h.Default.Value != nil && !h.Default.Value.IsZero() {
f = append(f, low.GenerateHashString(h.Default.Value))
}
f = append(f, fmt.Sprint(h.Maximum.Value))
f = append(f, fmt.Sprint(h.Minimum.Value))
@@ -129,24 +108,17 @@ func (h *Header) Hash() [32]byte {
if h.Pattern.Value != "" {
f = append(f, fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint(h.Pattern.Value)))))
}
f = append(f, low.HashExtensions(h.Extensions)...)
keys := make([]string, len(h.Extensions))
keys := make([]string, len(h.Enum.Value))
z := 0
for k := range h.Extensions {
keys[z] = fmt.Sprintf("%s-%x", k.Value, sha256.Sum256([]byte(fmt.Sprint(h.Extensions[k].Value))))
for k := range h.Enum.Value {
keys[z] = low.ValueToString(h.Enum.Value[k].Value)
z++
}
sort.Strings(keys)
f = append(f, keys...)
keys = make([]string, len(h.Enum.Value))
z = 0
for k := range h.Enum.Value {
keys[z] = fmt.Sprint(h.Enum.Value[k].Value)
z++
}
sort.Strings(keys)
f = append(f, keys...)
if h.Items.Value != nil {
f = append(f, low.GenerateHashString(h.Items.Value))
}
@@ -158,12 +130,15 @@ func (h *Header) Hash() [32]byte {
func (h *Header) GetType() *low.NodeReference[string] {
return &h.Type
}
func (h *Header) GetDescription() *low.NodeReference[string] {
return &h.Description
}
func (h *Header) GetFormat() *low.NodeReference[string] {
return &h.Format
}
func (h *Header) GetItems() *low.NodeReference[any] {
i := low.NodeReference[any]{
KeyNode: h.Items.KeyNode,
@@ -172,45 +147,59 @@ func (h *Header) GetItems() *low.NodeReference[any] {
}
return &i
}
func (h *Header) GetCollectionFormat() *low.NodeReference[string] {
return &h.CollectionFormat
}
func (h *Header) GetDefault() *low.NodeReference[any] {
func (h *Header) GetDefault() *low.NodeReference[*yaml.Node] {
return &h.Default
}
func (h *Header) GetMaximum() *low.NodeReference[int] {
return &h.Maximum
}
func (h *Header) GetExclusiveMaximum() *low.NodeReference[bool] {
return &h.ExclusiveMaximum
}
func (h *Header) GetMinimum() *low.NodeReference[int] {
return &h.Minimum
}
func (h *Header) GetExclusiveMinimum() *low.NodeReference[bool] {
return &h.ExclusiveMinimum
}
func (h *Header) GetMaxLength() *low.NodeReference[int] {
return &h.MaxLength
}
func (h *Header) GetMinLength() *low.NodeReference[int] {
return &h.MinLength
}
func (h *Header) GetPattern() *low.NodeReference[string] {
return &h.Pattern
}
func (h *Header) GetMaxItems() *low.NodeReference[int] {
return &h.MaxItems
}
func (h *Header) GetMinItems() *low.NodeReference[int] {
return &h.MinItems
}
func (h *Header) GetUniqueItems() *low.NodeReference[bool] {
return &h.UniqueItems
}
func (h *Header) GetEnum() *low.NodeReference[[]low.ValueReference[any]] {
func (h *Header) GetEnum() *low.NodeReference[[]low.ValueReference[*yaml.Node]] {
return &h.Enum
}
func (h *Header) GetMultipleOf() *low.NodeReference[int] {
return &h.MultipleOf
}