Collapsed repetitive code into generic functions.

Establishing patterns for individual objects, discovering designs to collapse what would be an obscene amount of repetitive code.
This commit is contained in:
Dave Shanley
2022-09-30 10:39:35 -04:00
parent cb7df7c9b2
commit ba37ca4e29
8 changed files with 390 additions and 126 deletions

View File

@@ -4,7 +4,6 @@
package what_changed
import (
"github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"
)
@@ -24,71 +23,37 @@ func (e *ExternalDocChanges) TotalChanges() int {
func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
var changes []*Change[*lowbase.ExternalDoc]
var props []*PropertyCheck[*lowbase.ExternalDoc]
skipURL := false
skipDescription := false
// check URL
props = append(props, &PropertyCheck[*lowbase.ExternalDoc]{
LeftNode: l.URL.ValueNode,
RightNode: r.URL.ValueNode,
Label: lowv3.URLLabel,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
// check if url was removed
if r.URL.Value == "" && l.URL.Value != "" {
CreateChange[*lowbase.ExternalDoc](&changes, PropertyRemoved, lowv3.URLLabel, l.URL.ValueNode,
nil, false, l, nil)
skipURL = true
}
props = append(props, &PropertyCheck[*lowbase.ExternalDoc]{
LeftNode: l.Description.ValueNode,
RightNode: r.Description.ValueNode,
Label: lowv3.DescriptionLabel,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
// check if description was removed
if r.Description.Value == "" && l.Description.Value != "" {
CreateChange[*lowbase.ExternalDoc](&changes, PropertyRemoved, lowv3.DescriptionLabel, l.Description.ValueNode,
nil, false, l, nil)
skipDescription = true
}
// check everything.
CheckProperties(props)
// check if url was added
if r.URL.Value != "" && l.URL.Value == "" {
CreateChange[*lowbase.ExternalDoc](&changes, PropertyAdded, lowv3.URLLabel, nil,
r.URL.ValueNode, false, nil, r)
skipURL = true
}
// check if description was added
if r.Description.Value != "" && l.Description.Value == "" {
CreateChange[*lowbase.ExternalDoc](&changes, PropertyAdded, lowv3.DescriptionLabel, nil,
r.Description.ValueNode, false, nil, r)
skipDescription = true
}
// if left and right URLs are set but not equal
if !skipURL && l != nil && r != nil && l.URL.Value != r.URL.Value {
var changeType int
changeType = Modified
ctx := CreateContext(l.URL.ValueNode, r.URL.ValueNode)
if ctx.HasChanged() {
changeType = ModifiedAndMoved
}
CreateChange[*lowbase.ExternalDoc](&changes, changeType, lowv3.URLLabel, l.URL.ValueNode,
r.URL.ValueNode, false, l, r)
}
// if left and right descriptions are set, but not equal
if !skipDescription && l != nil && r != nil && l.Description.Value != r.Description.Value {
var changeType int
changeType = Modified
ctx := CreateContext(l.Description.ValueNode, r.Description.ValueNode)
if ctx.HasChanged() {
changeType = ModifiedAndMoved
}
CreateChange[*lowbase.ExternalDoc](&changes, changeType, lowv3.DescriptionLabel, l.Description.ValueNode,
r.Description.ValueNode, false, l, r)
}
dc := new(ExternalDocChanges)
dc.Changes = changes
var lExt, rExt map[low.KeyReference[string]]low.ValueReference[any]
if l != nil && len(l.Extensions) > 0 {
lExt = l.Extensions
}
if r != nil && len(r.Extensions) > 0 {
rExt = r.Extensions
}
dc.ExtensionChanges = CompareExtensions(lExt, rExt)
// check extensions
dc.ExtensionChanges = CheckExtensions(l, r)
if len(dc.Changes) <= 0 && dc.ExtensionChanges == nil {
return nil
}