Cleaned up tags logic for what-changed feature.

Using new generic functions
This commit is contained in:
Dave Shanley
2022-10-01 12:40:30 -04:00
parent ba37ca4e29
commit 3d0fe1cc2e
5 changed files with 71 additions and 53 deletions

View File

@@ -31,6 +31,7 @@ func (ex *ExternalDoc) Build(root *yaml.Node, idx *index.SpecIndex) error {
return nil return nil
} }
// GetExtensions returns all ExternalDoc extensions and satisfies the low.HasExtensions interface.
func (ex *ExternalDoc) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] { func (ex *ExternalDoc) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
if ex == nil { if ex == nil {
return nil return nil

View File

@@ -37,6 +37,14 @@ func (t *Tag) Build(root *yaml.Node, idx *index.SpecIndex) error {
return err return err
} }
// GetExtensions returns all Tag extensions and satisfies the low.HasExtensions interface.
func (t *Tag) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
if t == nil {
return nil
}
return t.Extensions
}
// TODO: future mutation API experiment code is here. this snippet is to re-marshal the object. // TODO: future mutation API experiment code is here. this snippet is to re-marshal the object.
//func (t *Tag) MarshalYAML() (interface{}, error) { //func (t *Tag) MarshalYAML() (interface{}, error) {
// m := make(map[string]interface{}) // m := make(map[string]interface{})

View File

@@ -25,7 +25,7 @@ func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
var changes []*Change[*lowbase.ExternalDoc] var changes []*Change[*lowbase.ExternalDoc]
var props []*PropertyCheck[*lowbase.ExternalDoc] var props []*PropertyCheck[*lowbase.ExternalDoc]
// check URL // URL
props = append(props, &PropertyCheck[*lowbase.ExternalDoc]{ props = append(props, &PropertyCheck[*lowbase.ExternalDoc]{
LeftNode: l.URL.ValueNode, LeftNode: l.URL.ValueNode,
RightNode: r.URL.ValueNode, RightNode: r.URL.ValueNode,
@@ -36,6 +36,7 @@ func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
New: r, New: r,
}) })
// description.
props = append(props, &PropertyCheck[*lowbase.ExternalDoc]{ props = append(props, &PropertyCheck[*lowbase.ExternalDoc]{
LeftNode: l.Description.ValueNode, LeftNode: l.Description.ValueNode,
RightNode: r.Description.ValueNode, RightNode: r.Description.ValueNode,

View File

@@ -46,57 +46,41 @@ func CompareTags(l, r []low.ValueReference[*lowbase.Tag]) *TagChanges {
// check for removals, modifications and moves // check for removals, modifications and moves
for i := range seenLeft { for i := range seenLeft {
if seenRight[i] == nil {
// deleted CheckForAdditionOrRemoval[*lowbase.Tag](seenLeft, seenRight, i, &changes, false, true)
CreateChange[*lowbase.Tag](&changes, ObjectRemoved, i, seenLeft[i].ValueNode, nil,
false, seenLeft[i].Value, nil)
continue
}
// if the existing tag exists, let's check it. // if the existing tag exists, let's check it.
if seenRight[i] != nil { if seenRight[i] != nil {
// check if name has moved var props []*PropertyCheck[*lowbase.Tag]
ctx := CreateContext(seenLeft[i].Value.Name.ValueNode, seenRight[i].Value.Name.ValueNode)
if ctx.HasChanged() {
CreateChange[*lowbase.Tag](&changes, Moved, lowv3.NameLabel,
seenLeft[i].Value.Name.ValueNode, seenRight[i].Value.Name.ValueNode,
false, seenLeft[i].Value, seenRight[i].Value)
} // Name
props = append(props, &PropertyCheck[*lowbase.Tag]{
LeftNode: seenLeft[i].Value.Name.ValueNode,
RightNode: seenRight[i].Value.Name.ValueNode,
Label: lowv3.NameLabel,
Changes: &changes,
Breaking: true,
Original: seenLeft[i].Value,
New: seenRight[i].Value,
})
// check if description has been modified // Description
if seenLeft[i].Value.Description.Value != seenRight[i].Value.Description.Value { props = append(props, &PropertyCheck[*lowbase.Tag]{
var changeType int LeftNode: seenLeft[i].Value.Description.ValueNode,
changeType = Modified RightNode: seenRight[i].Value.Description.ValueNode,
ctx = CreateContext(seenLeft[i].Value.Description.ValueNode, seenRight[i].Value.Description.ValueNode) Label: lowv3.DescriptionLabel,
if ctx.HasChanged() { Changes: &changes,
changeType = ModifiedAndMoved Breaking: true,
} Original: seenLeft[i].Value,
CreateChange[*lowbase.Tag](&changes, changeType, lowv3.DescriptionLabel, New: seenRight[i].Value,
seenLeft[i].Value.Description.ValueNode, seenRight[i].Value.Description.ValueNode, })
false, seenLeft[i].Value, seenRight[i].Value)
}
// check if description has moved // check properties
if seenLeft[i].Value.Description.Value == seenRight[i].Value.Description.Value { CheckProperties(props)
ctx = CreateContext(seenLeft[i].Value.Description.ValueNode, seenRight[i].Value.Description.ValueNode)
if ctx.HasChanged() {
CreateChange[*lowbase.Tag](&changes, Moved, lowv3.DescriptionLabel,
seenLeft[i].Value.Description.ValueNode, seenRight[i].Value.Description.ValueNode,
false, seenLeft[i].Value, seenRight[i].Value)
}
}
// compare extensions // check extensions
var lExt, rExt map[low.KeyReference[string]]low.ValueReference[any] tc.ExtensionChanges = CheckExtensions(seenLeft[i].GetValue(), seenRight[i].GetValue())
if l != nil && len(seenLeft[i].Value.Extensions) > 0 {
lExt = seenLeft[i].Value.Extensions
}
if r != nil && len(seenRight[i].Value.Extensions) > 0 {
rExt = seenRight[i].Value.Extensions
}
tc.ExtensionChanges = CompareExtensions(lExt, rExt)
// compare external docs // compare external docs
tc.ExternalDocs = CompareExternalDocs(seenLeft[i].Value.ExternalDocs.Value, tc.ExternalDocs = CompareExternalDocs(seenLeft[i].Value.ExternalDocs.Value,
@@ -104,15 +88,6 @@ func CompareTags(l, r []low.ValueReference[*lowbase.Tag]) *TagChanges {
} }
} }
// check for additions
for i := range seenRight {
if seenLeft[i] == nil {
// added
CreateChange[*lowbase.Tag](&changes, ObjectAdded, i,
nil, seenRight[i].ValueNode,
false, nil, seenRight[i].Value)
}
}
if len(changes) <= 0 { if len(changes) <= 0 {
return nil return nil
} }

View File

@@ -108,6 +108,39 @@ type PropertyCheck[T any] struct {
Changes *[]*Change[T] Changes *[]*Change[T]
} }
func CheckForAdditionOrRemoval[T any](l, r map[string]*low.ValueReference[T], label string, changes *[]*Change[T],
breakingAdd, breakingRemove bool) {
var left, right T
if CheckObjectRemoved(l, r) {
left = l[label].GetValue()
CreateChange[T](changes, ObjectRemoved, label, l[label].GetValueNode(), nil,
breakingRemove, left, right)
}
if added, key := CheckObjectAdded(l, r); added {
right = r[key].GetValue()
CreateChange[T](changes, ObjectAdded, label, nil, r[key].GetValueNode(),
breakingAdd, left, right)
}
}
func CheckObjectRemoved[T any](l, r map[string]*T) bool {
for i := range l {
if r[i] == nil {
return true
}
}
return false
}
func CheckObjectAdded[T any](l, r map[string]*T) (bool, string) {
for i := range r {
if l[i] == nil {
return true, i
}
}
return false, ""
}
func CheckProperties[T any](properties []*PropertyCheck[T]) { func CheckProperties[T any](properties []*PropertyCheck[T]) {
for _, n := range properties { for _, n := range properties {
CheckPropertyAdditionOrRemoval(n.LeftNode, n.RightNode, n.Label, n.Changes, n.Breaking, n.Original, n.New) CheckPropertyAdditionOrRemoval(n.LeftNode, n.RightNode, n.Label, n.Changes, n.Breaking, n.Original, n.New)