Added CreateChange generic method

Works pretty well, cuts down on code.
This commit is contained in:
Dave Shanley
2022-09-29 10:09:47 -04:00
parent c47cc15cad
commit a9252c656d
6 changed files with 143 additions and 185 deletions

View File

@@ -41,4 +41,5 @@ const (
DescriptionLabel = "description" DescriptionLabel = "description"
URLLabel = "url" URLLabel = "url"
NameLabel = "name" NameLabel = "name"
EmailLabel = "email"
) )

79
what-changed/contact.go Normal file
View File

@@ -0,0 +1,79 @@
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package what_changed
import (
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"
)
type ContactChanges struct {
PropertyChanges[*lowbase.Contact]
}
func (c *ContactChanges) TotalChanges() int {
return len(c.Changes)
}
func CompareContact(l, r *lowbase.Contact) *ContactChanges {
var changes []*Change[*lowbase.Contact]
changeType := 0
// check if the url was added
if l != nil && r != nil && l.URL.Value == "" && r.URL.Value != "" {
changeType = PropertyAdded
CreateChange[*lowbase.Contact](&changes, changeType, lowv3.URLLabel,
nil, r.Name.ValueNode, false, l, r)
}
// check if the name was added
if l != nil && r != nil && l.Name.Value == "" && r.Name.Value != "" {
changeType = PropertyAdded
CreateChange[*lowbase.Contact](&changes, changeType, lowv3.NameLabel,
nil, r.Name.ValueNode, false, l, r)
}
// if both urls are set, but are different.
if l != nil && r != nil && l.URL.Value != r.URL.Value {
changeType = Modified
ctx := CreateContext(l.URL.ValueNode, r.URL.ValueNode)
if ctx.HasChanged() {
changeType = ModifiedAndMoved
}
CreateChange(&changes, changeType, lowv3.URLLabel,
l.URL.ValueNode, r.Name.ValueNode, false, l, r)
}
// if both names are set, but are different.
if l != nil && r != nil && l.Name.Value != r.Name.Value {
changeType = Modified
ctx := CreateContext(l.Name.ValueNode, r.Name.ValueNode)
if ctx.HasChanged() {
changeType = ModifiedAndMoved
}
CreateChange[*lowbase.Contact](&changes, changeType, lowv3.NameLabel,
l.Name.ValueNode, r.Name.ValueNode, false, l, r)
}
// if both email addresses are set, but are different.
if l != nil && r != nil && l.Email.Value != r.Email.Value {
changeType = Modified
ctx := CreateContext(l.Email.ValueNode, r.Email.ValueNode)
if ctx.HasChanged() {
changeType = ModifiedAndMoved
}
CreateChange[*lowbase.Contact](&changes, changeType, lowv3.EmailLabel,
l.Email.ValueNode, r.Email.ValueNode, false, l, r)
}
if changeType == 0 {
// no change, return nothing.
return nil
}
dc := new(ContactChanges)
dc.Changes = changes
return dc
}

View File

@@ -4,13 +4,12 @@
package what_changed package what_changed
import ( import (
"fmt"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
"strings" "strings"
) )
type ExtensionChanges struct { type ExtensionChanges struct {
PropertyChanges PropertyChanges[any]
} }
func CompareExtensions(l, r map[low.KeyReference[string]]low.ValueReference[any]) *ExtensionChanges { func CompareExtensions(l, r map[low.KeyReference[string]]low.ValueReference[any]) *ExtensionChanges {
@@ -27,24 +26,16 @@ func CompareExtensions(l, r map[low.KeyReference[string]]low.ValueReference[any]
seenRight[strings.ToLower(i.Value)] = &h seenRight[strings.ToLower(i.Value)] = &h
} }
var changes []*Change var changes []*Change[any]
var changeType int
for i := range seenLeft { for i := range seenLeft {
changeType = 0
if seenRight[i] == nil { if seenRight[i] == nil {
// deleted // deleted
changeType = PropertyRemoved CreateChange[any](&changes, PropertyRemoved, i, seenLeft[i].ValueNode, nil, false, l, nil)
ctx := CreateContext(seenLeft[i].ValueNode, nil)
changes = append(changes, &Change{
Context: ctx,
ChangeType: changeType,
Property: i,
Original: fmt.Sprintf("%v", seenLeft[i].Value),
})
} }
if seenRight[i] != nil { if seenRight[i] != nil {
// potentially modified and or moved // potentially modified and or moved
var changeType int
ctx := CreateContext(seenLeft[i].ValueNode, seenRight[i].ValueNode) ctx := CreateContext(seenLeft[i].ValueNode, seenRight[i].ValueNode)
if seenLeft[i].Value != seenRight[i].Value { if seenLeft[i].Value != seenRight[i].Value {
changeType = Modified changeType = Modified
@@ -57,26 +48,14 @@ func CompareExtensions(l, r map[low.KeyReference[string]]low.ValueReference[any]
} }
} }
if changeType != 0 { if changeType != 0 {
changes = append(changes, &Change{ CreateChange[any](&changes, changeType, i, seenLeft[i].ValueNode, seenRight[i].ValueNode, false, l, r)
Context: ctx,
ChangeType: changeType,
Property: i,
Original: fmt.Sprintf("%v", seenLeft[i].Value),
New: fmt.Sprintf("%v", seenRight[i].Value),
})
} }
} }
} }
for i := range seenRight { for i := range seenRight {
if seenLeft[i] == nil { if seenLeft[i] == nil {
// added // added
ctx := CreateContext(nil, seenRight[i].ValueNode) CreateChange[any](&changes, PropertyAdded, i, nil, seenRight[i].ValueNode, false, nil, r)
changes = append(changes, &Change{
Context: ctx,
ChangeType: PropertyAdded,
Property: i,
New: fmt.Sprintf("%v", seenRight[i].Value),
})
} }
} }

View File

@@ -10,7 +10,7 @@ import (
) )
type ExternalDocChanges struct { type ExternalDocChanges struct {
PropertyChanges PropertyChanges[*lowbase.ExternalDoc]
ExtensionChanges *ExtensionChanges ExtensionChanges *ExtensionChanges
} }
@@ -23,7 +23,7 @@ func (e *ExternalDocChanges) TotalChanges() int {
} }
func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges { func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
var changes []*Change var changes []*Change[*lowbase.ExternalDoc]
changeType := 0 changeType := 0
if l != nil && r != nil && l.URL.Value != r.URL.Value { if l != nil && r != nil && l.URL.Value != r.URL.Value {
changeType = Modified changeType = Modified
@@ -31,13 +31,8 @@ func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
if ctx.HasChanged() { if ctx.HasChanged() {
changeType = ModifiedAndMoved changeType = ModifiedAndMoved
} }
changes = append(changes, &Change{ CreateChange[*lowbase.ExternalDoc](&changes, changeType, lowv3.URLLabel, l.URL.ValueNode,
Context: ctx, r.URL.ValueNode, false, l, r)
ChangeType: changeType,
Property: lowv3.URLLabel,
Original: l.URL.Value,
New: r.URL.Value,
})
} }
if l != nil && r != nil && l.Description.Value != r.Description.Value { if l != nil && r != nil && l.Description.Value != r.Description.Value {
changeType = Modified changeType = Modified
@@ -45,13 +40,8 @@ func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
if ctx.HasChanged() { if ctx.HasChanged() {
changeType = ModifiedAndMoved changeType = ModifiedAndMoved
} }
changes = append(changes, &Change{ CreateChange[*lowbase.ExternalDoc](&changes, changeType, lowv3.DescriptionLabel, l.Description.ValueNode,
Context: ctx, r.Description.ValueNode, false, l, r)
ChangeType: changeType,
Property: lowv3.DescriptionLabel,
Original: l.Description.Value,
New: r.Description.Value,
})
} }
if changeType == 0 { if changeType == 0 {
// no change, return nothing. // no change, return nothing.

View File

@@ -4,7 +4,6 @@
package what_changed package what_changed
import ( import (
"fmt"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base" lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3" lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"
@@ -12,7 +11,7 @@ import (
) )
type TagChanges struct { type TagChanges struct {
PropertyChanges PropertyChanges[*lowbase.Tag]
ExternalDocs *ExternalDocChanges ExternalDocs *ExternalDocChanges
ExtensionChanges *ExtensionChanges ExtensionChanges *ExtensionChanges
} }
@@ -43,22 +42,14 @@ func CompareTags(l, r []low.ValueReference[*lowbase.Tag]) *TagChanges {
seenRight[strings.ToLower(r[i].Value.Name.Value)] = &h seenRight[strings.ToLower(r[i].Value.Name.Value)] = &h
} }
var changes []*Change var changes []*Change[*lowbase.Tag]
var changeType int
// check for removals, modifications and moves // check for removals, modifications and moves
for i := range seenLeft { for i := range seenLeft {
changeType = 0
if seenRight[i] == nil { if seenRight[i] == nil {
// deleted // deleted
changeType = ObjectRemoved CreateChange[*lowbase.Tag](&changes, ObjectRemoved, i, seenLeft[i].ValueNode, nil,
ctx := CreateContext(seenLeft[i].ValueNode, nil) false, seenLeft[i].Value, nil)
changes = append(changes, &Change{
Context: ctx,
ChangeType: changeType,
Property: i,
Original: fmt.Sprintf("%v", seenLeft[i].Value),
})
continue continue
} }
@@ -68,45 +59,32 @@ func CompareTags(l, r []low.ValueReference[*lowbase.Tag]) *TagChanges {
// check if name has moved // check if name has moved
ctx := CreateContext(seenLeft[i].Value.Name.ValueNode, seenRight[i].Value.Name.ValueNode) ctx := CreateContext(seenLeft[i].Value.Name.ValueNode, seenRight[i].Value.Name.ValueNode)
if ctx.HasChanged() { if ctx.HasChanged() {
changeType = Moved CreateChange[*lowbase.Tag](&changes, Moved, lowv3.NameLabel,
changes = append(changes, &Change{ seenLeft[i].Value.Name.ValueNode, seenRight[i].Value.Name.ValueNode,
Context: ctx, false, seenLeft[i].Value, seenRight[i].Value)
ChangeType: changeType,
Property: lowv3.NameLabel,
Original: seenLeft[i].Value.Name.Value,
New: seenRight[i].Value.Name.Value,
})
} }
// check if description has been modified // check if description has been modified
if seenLeft[i].Value.Description.Value != seenRight[i].Value.Description.Value { if seenLeft[i].Value.Description.Value != seenRight[i].Value.Description.Value {
var changeType int
changeType = Modified changeType = Modified
ctx = CreateContext(seenLeft[i].Value.Description.ValueNode, seenRight[i].Value.Description.ValueNode) ctx = CreateContext(seenLeft[i].Value.Description.ValueNode, seenRight[i].Value.Description.ValueNode)
if ctx.HasChanged() { if ctx.HasChanged() {
changeType = ModifiedAndMoved changeType = ModifiedAndMoved
} }
changes = append(changes, &Change{ CreateChange[*lowbase.Tag](&changes, changeType, lowv3.DescriptionLabel,
Context: ctx, seenLeft[i].Value.Description.ValueNode, seenRight[i].Value.Description.ValueNode,
ChangeType: changeType, false, seenLeft[i].Value, seenRight[i].Value)
Property: lowv3.DescriptionLabel,
Original: seenLeft[i].Value.Description.Value,
New: seenRight[i].Value.Description.Value,
})
} }
// check if description has moved // check if description has moved
if seenLeft[i].Value.Description.Value == seenRight[i].Value.Description.Value { if seenLeft[i].Value.Description.Value == seenRight[i].Value.Description.Value {
ctx = CreateContext(seenLeft[i].Value.Description.ValueNode, seenRight[i].Value.Description.ValueNode) ctx = CreateContext(seenLeft[i].Value.Description.ValueNode, seenRight[i].Value.Description.ValueNode)
if ctx.HasChanged() { if ctx.HasChanged() {
changeType = Moved CreateChange[*lowbase.Tag](&changes, Moved, lowv3.DescriptionLabel,
changes = append(changes, &Change{ seenLeft[i].Value.Description.ValueNode, seenRight[i].Value.Description.ValueNode,
Context: ctx, false, seenLeft[i].Value, seenRight[i].Value)
ChangeType: changeType,
Property: lowv3.DescriptionLabel,
Original: seenLeft[i].Value.Description.Value,
New: seenRight[i].Value.Description.Value,
})
} }
} }
@@ -130,13 +108,9 @@ func CompareTags(l, r []low.ValueReference[*lowbase.Tag]) *TagChanges {
for i := range seenRight { for i := range seenRight {
if seenLeft[i] == nil { if seenLeft[i] == nil {
// added // added
ctx := CreateContext(nil, seenRight[i].ValueNode) CreateChange[*lowbase.Tag](&changes, ObjectAdded, i,
changes = append(changes, &Change{ nil, seenRight[i].ValueNode,
Context: ctx, false, nil, seenRight[i].Value)
ChangeType: ObjectAdded,
Property: i,
New: fmt.Sprintf("%v", seenRight[i].Value),
})
} }
} }
if len(changes) <= 0 { if len(changes) <= 0 {

View File

@@ -37,22 +37,47 @@ func (c *ChangeContext) HasChanged() bool {
return c.NewLine != c.OrigLine || c.NewCol != c.OrigCol return c.NewLine != c.OrigLine || c.NewCol != c.OrigCol
} }
type Change struct { type Change[T any] struct {
Context *ChangeContext Context *ChangeContext
ChangeType int ChangeType int
Property string Property string
Original string Original string
New string New string
Breaking bool
OriginalObject T
NewObject T
} }
type PropertyChanges struct { type PropertyChanges[T any] struct {
Changes []*Change Changes []*Change[T]
} }
type Changes struct { type Changes struct {
TagChanges *TagChanges TagChanges *TagChanges
} }
func CreateChange[T any](changes *[]*Change[T], changeType int, property string, leftValueNode, rightValueNode *yaml.Node,
breaking bool, originalObject, newObject T) *[]*Change[T] {
ctx := CreateContext(leftValueNode, rightValueNode)
c := &Change[T]{
Context: ctx,
ChangeType: changeType,
Property: property,
Breaking: breaking,
}
if leftValueNode != nil && leftValueNode.Value != "" {
c.Original = leftValueNode.Value
}
if rightValueNode != nil && rightValueNode.Value != "" {
c.New = rightValueNode.Value
}
c.OriginalObject = originalObject
c.NewObject = newObject
*changes = append(*changes, c)
return changes
}
//func WhatChangedBetweenDocuments(leftDocument, rightDocument *lowv3.Document) *WhatChanged { //func WhatChangedBetweenDocuments(leftDocument, rightDocument *lowv3.Document) *WhatChanged {
// //
// // compare tags // // compare tags
@@ -80,93 +105,3 @@ func CreateContext(l, r *yaml.Node) *ChangeContext {
} }
return ctx return ctx
} }
//
//func compareTags(l, r []low.ValueReference[*lowbase.Tag]) *TagChanges {
//
// tc := new(TagChanges)
//
// // look at the original and then look through the new.
// seenLeft := make(map[string]*low.ValueReference[*lowbase.Tag])
// seenRight := make(map[string]*low.ValueReference[*lowbase.Tag])
// for i := range l {
// seenLeft[strings.ToLower(l[i].Value.Name.Value)] = &l[i]
// }
// for i := range r {
// seenRight[strings.ToLower(l[i].Value.Name.Value)] = &l[i]
// }
//
// for i := range seenLeft {
// if seenRight[i] == nil {
// // deleted
// //ctx := CreateContext(seenLeft[i].ValueNode, nil)
// //tc.Changes =
//
// }
// if seenRight[i] != nil {
//
// // potentially modified and or moved
// }
// }
//
// for i := range seenRight {
// if seenLeft[i] == nil {
// // added
// }
// }
//
// for i := range r {
// // if we find a match
// t := r[i]
// name := r[i].Value.Name.Value
// found := seenLeft[strings.ToLower(name)]
// if found.Value != nil {
//
// // check values
// if found.Value.Description.Value != t.Value.Description.Value {
// ctx := CreateContext(found.ValueNode, t.ValueNode)
// changeType := Modified
// if ctx.HasChanged() {
// changeType = ModifiedAndMoved
// }
// tc.Changes = append(tc.Changes, &Change{
// Context: ctx,
// ChangeType: changeType,
// Property: lowv3.DescriptionLabel,
// Original: found.Value.Description.Value,
// New: t.Value.Description.Value,
// })
// }
//
// } else {
//
// // new stuff
//
// }
//
// }
//
// // more tags in right hand-side
// if len(r) > len(l) {
//
// }
//
// // less tags in right hand-side
// if len(r) < len(l) {
//
// }
//
// //for i := range a {
// // eq, l, c := comparePositions(a)
// //
// //}
//
// return nil
//}
//
//func comparePositions(left, right *yaml.Node) (bool, int, int) {
// if left.Line == right.Line && left.Column == right.Column {
// return true, 0, 0
// }
// return false, right.Line, right.Column
//}