mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-08 04:20:17 +00:00
fix: continued moving everything to orderedmaps plus cleaned up most the tests
This commit is contained in:
@@ -7,12 +7,11 @@ import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"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"
|
||||
)
|
||||
@@ -23,15 +22,15 @@ import (
|
||||
type Example struct {
|
||||
Summary low.NodeReference[string]
|
||||
Description low.NodeReference[string]
|
||||
Value low.NodeReference[any]
|
||||
Value low.NodeReference[*yaml.Node]
|
||||
ExternalValue low.NodeReference[string]
|
||||
Extensions map[low.KeyReference[string]]low.ValueReference[any]
|
||||
Extensions *orderedmap.Map[low.KeyReference[string], low.ValueReference[*yaml.Node]]
|
||||
*low.Reference
|
||||
}
|
||||
|
||||
// FindExtension returns a ValueReference containing the extension value, if found.
|
||||
func (ex *Example) FindExtension(ext string) *low.ValueReference[any] {
|
||||
return low.FindItemInMap[any](ext, ex.Extensions)
|
||||
func (ex *Example) FindExtension(ext string) *low.ValueReference[*yaml.Node] {
|
||||
return low.FindItemInOrderedMap[*yaml.Node](ext, ex.Extensions)
|
||||
}
|
||||
|
||||
// Hash will return a consistent SHA256 Hash of the Discriminator object
|
||||
@@ -43,21 +42,15 @@ func (ex *Example) Hash() [32]byte {
|
||||
if ex.Description.Value != "" {
|
||||
f = append(f, ex.Description.Value)
|
||||
}
|
||||
if ex.Value.Value != "" {
|
||||
if ex.Value.Value != nil && !ex.Value.Value.IsZero() {
|
||||
// this could be anything!
|
||||
f = append(f, fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint(ex.Value.Value)))))
|
||||
b, _ := yaml.Marshal(ex.Value.Value)
|
||||
f = append(f, fmt.Sprintf("%x", sha256.Sum256(b)))
|
||||
}
|
||||
if ex.ExternalValue.Value != "" {
|
||||
f = append(f, ex.ExternalValue.Value)
|
||||
}
|
||||
keys := make([]string, len(ex.Extensions))
|
||||
z := 0
|
||||
for k := range ex.Extensions {
|
||||
keys[z] = fmt.Sprintf("%s-%x", k.Value, sha256.Sum256([]byte(fmt.Sprint(ex.Extensions[k].Value))))
|
||||
z++
|
||||
}
|
||||
sort.Strings(keys)
|
||||
f = append(f, keys...)
|
||||
f = append(f, low.HashExtensions(ex.Extensions)...)
|
||||
return sha256.Sum256([]byte(strings.Join(f, "|")))
|
||||
}
|
||||
|
||||
@@ -70,32 +63,8 @@ func (ex *Example) Build(_ context.Context, _, root *yaml.Node, _ *index.SpecInd
|
||||
_, ln, vn := utils.FindKeyNodeFull(ValueLabel, 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)
|
||||
ex.Value = low.NodeReference[any]{
|
||||
Value: j,
|
||||
KeyNode: ln,
|
||||
ValueNode: vn,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
ex.Value = low.NodeReference[any]{
|
||||
Value: k,
|
||||
KeyNode: ln,
|
||||
ValueNode: vn,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
ex.Value = low.NodeReference[any]{
|
||||
Value: n,
|
||||
ex.Value = low.NodeReference[*yaml.Node]{
|
||||
Value: vn,
|
||||
KeyNode: ln,
|
||||
ValueNode: vn,
|
||||
}
|
||||
@@ -105,33 +74,6 @@ func (ex *Example) Build(_ context.Context, _, root *yaml.Node, _ *index.SpecInd
|
||||
}
|
||||
|
||||
// GetExtensions will return Example extensions to satisfy the HasExtensions interface.
|
||||
func (ex *Example) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
|
||||
func (ex *Example) GetExtensions() *orderedmap.Map[low.KeyReference[string], low.ValueReference[*yaml.Node]] {
|
||||
return ex.Extensions
|
||||
}
|
||||
|
||||
// ExtractExampleValue will extract a primitive example value (if possible), or just the raw Value property if not.
|
||||
func ExtractExampleValue(exp *yaml.Node) any {
|
||||
if utils.IsNodeBoolValue(exp) {
|
||||
v, _ := strconv.ParseBool(exp.Value)
|
||||
return v
|
||||
}
|
||||
if utils.IsNodeIntValue(exp) {
|
||||
v, _ := strconv.ParseInt(exp.Value, 10, 64)
|
||||
return v
|
||||
}
|
||||
if utils.IsNodeFloatValue(exp) {
|
||||
v, _ := strconv.ParseFloat(exp.Value, 64)
|
||||
return v
|
||||
}
|
||||
if utils.IsNodeMap(exp) {
|
||||
var m map[string]interface{}
|
||||
_ = exp.Decode(&m)
|
||||
return m
|
||||
}
|
||||
if utils.IsNodeArray(exp) {
|
||||
var m []interface{}
|
||||
_ = exp.Decode(&m)
|
||||
return m
|
||||
}
|
||||
return exp.Value
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user