what-changed operational

the code is working, the tests are passing. There is a lot more to check, but it's a good time to bring the feature into main.
This commit is contained in:
Dave Shanley
2022-11-18 10:47:39 -05:00
parent b510be3563
commit 9d8d6e36ea
8 changed files with 133 additions and 69 deletions

View File

@@ -4,8 +4,11 @@
package model
import (
"fmt"
"github.com/pb33f/libopenapi/datamodel/low/base"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/utils"
"sort"
)
// ExampleChanges represent changes to an Example object, part of an OpenAPI specification.
@@ -60,16 +63,56 @@ func CompareExamples(l, r *base.Example) *ExampleChanges {
})
// Value
props = append(props, &PropertyCheck{
LeftNode: l.Value.ValueNode,
RightNode: r.Value.ValueNode,
Label: v3.ValueLabel,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
if utils.IsNodeMap(l.Value.ValueNode) && utils.IsNodeMap(r.Value.ValueNode) {
lKeys := make([]string, len(l.Value.ValueNode.Content)/2)
rKeys := make([]string, len(r.Value.ValueNode.Content)/2)
z := 0
for k := range l.Value.ValueNode.Content {
if k%2 == 0 {
lKeys[z] = fmt.Sprintf("%v-%v", l.Value.ValueNode.Content[k].Value, l.Value.ValueNode.Content[k+1].Value)
z++
} else {
continue
}
}
z = 0
for k := range r.Value.ValueNode.Content {
if k%2 == 0 {
rKeys[z] = fmt.Sprintf("%v-%v", r.Value.ValueNode.Content[k].Value, r.Value.ValueNode.Content[k+1].Value)
z++
} else {
continue
}
}
sort.Strings(lKeys)
sort.Strings(rKeys)
if (len(lKeys) > len(rKeys)) || (len(rKeys) > len(lKeys)) {
CreateChange(&changes, Modified, v3.ValueLabel,
l.Value.GetValueNode(), r.Value.GetValueNode(), false, l.Value.GetValue(), r.Value.GetValue())
}
for k := range lKeys {
if lKeys[k] != rKeys[k] {
CreateChange(&changes, Modified, v3.ValueLabel,
l.Value.GetValueNode(), r.Value.GetValueNode(), false, l.Value.GetValue(), r.Value.GetValue())
}
}
for k := range rKeys {
if k >= len(lKeys) {
CreateChange(&changes, Modified, v3.ValueLabel,
nil, r.Value.GetValueNode(), false, nil, r.Value.GetValue())
}
}
} else {
props = append(props, &PropertyCheck{
LeftNode: l.Value.ValueNode,
RightNode: r.Value.ValueNode,
Label: v3.ValueLabel,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
}
// ExternalValue
props = append(props, &PropertyCheck{
LeftNode: l.ExternalValue.ValueNode,