mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 04:20:24 +00:00
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:
@@ -30,3 +30,10 @@ func (ex *ExternalDoc) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||
ex.Extensions = low.ExtractExtensions(root)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ex *ExternalDoc) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
|
||||
if ex == nil {
|
||||
return nil
|
||||
}
|
||||
return ex.Extensions
|
||||
}
|
||||
|
||||
@@ -22,6 +22,11 @@ type HasValueNode[T any] interface {
|
||||
*T
|
||||
}
|
||||
|
||||
// HasExtensions is implemented by any object that exposes extensions
|
||||
type HasExtensions[T any] interface {
|
||||
GetExtensions() map[KeyReference[string]]ValueReference[any]
|
||||
}
|
||||
|
||||
// HasValue is implemented by NodeReference and ValueReference to return the yaml.Node backing the value.
|
||||
type HasValue[T any] interface {
|
||||
GetValue() T
|
||||
|
||||
@@ -19,61 +19,49 @@ func (c *ContactChanges) TotalChanges() int {
|
||||
func CompareContact(l, r *lowbase.Contact) *ContactChanges {
|
||||
|
||||
var changes []*Change[*lowbase.Contact]
|
||||
changeType := 0
|
||||
var props []*PropertyCheck[*lowbase.Contact]
|
||||
|
||||
// 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 URL
|
||||
props = append(props, &PropertyCheck[*lowbase.Contact]{
|
||||
LeftNode: l.URL.ValueNode,
|
||||
RightNode: r.URL.ValueNode,
|
||||
Label: lowv3.URLLabel,
|
||||
Changes: &changes,
|
||||
Breaking: false,
|
||||
Original: l,
|
||||
New: 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)
|
||||
}
|
||||
// check name
|
||||
props = append(props, &PropertyCheck[*lowbase.Contact]{
|
||||
LeftNode: l.Name.ValueNode,
|
||||
RightNode: r.Name.ValueNode,
|
||||
Label: lowv3.NameLabel,
|
||||
Changes: &changes,
|
||||
Breaking: false,
|
||||
Original: l,
|
||||
New: 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)
|
||||
}
|
||||
// check email
|
||||
props = append(props, &PropertyCheck[*lowbase.Contact]{
|
||||
LeftNode: l.Email.ValueNode,
|
||||
RightNode: r.Email.ValueNode,
|
||||
Label: lowv3.EmailLabel,
|
||||
Changes: &changes,
|
||||
Breaking: false,
|
||||
Original: l,
|
||||
New: 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)
|
||||
}
|
||||
// check everything.
|
||||
CheckProperties(props)
|
||||
|
||||
// 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
|
||||
if len(changes) <= 0 {
|
||||
return nil
|
||||
}
|
||||
return dc
|
||||
|
||||
}
|
||||
|
||||
|
||||
244
what-changed/contact_test.go
Normal file
244
what-changed/contact_test.go
Normal file
@@ -0,0 +1,244 @@
|
||||
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package what_changed
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompareContact_URLAdded(t *testing.T) {
|
||||
|
||||
left := `name: buckaroo`
|
||||
|
||||
right := `name: buckaroo
|
||||
url: https://pb33f.io`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc lowbase.Contact
|
||||
var rDoc lowbase.Contact
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareContact(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, PropertyAdded, extChanges.Changes[0].ChangeType)
|
||||
|
||||
}
|
||||
|
||||
func TestCompareContact_URLRemoved(t *testing.T) {
|
||||
|
||||
left := `name: buckaroo
|
||||
url: https://pb33f.io`
|
||||
|
||||
right := `name: buckaroo`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc lowbase.Contact
|
||||
var rDoc lowbase.Contact
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareContact(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, PropertyRemoved, extChanges.Changes[0].ChangeType)
|
||||
|
||||
}
|
||||
|
||||
func TestCompareContact_NameAdded(t *testing.T) {
|
||||
|
||||
left := `url: https://pb33f.io`
|
||||
|
||||
right := `url: https://pb33f.io
|
||||
name: buckaroo`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc lowbase.Contact
|
||||
var rDoc lowbase.Contact
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareContact(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, PropertyAdded, extChanges.Changes[0].ChangeType)
|
||||
|
||||
}
|
||||
|
||||
func TestCompareContact_NameRemoved(t *testing.T) {
|
||||
|
||||
left := `url: https://pb33f.io
|
||||
name: buckaroo`
|
||||
|
||||
right := `url: https://pb33f.io`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc lowbase.Contact
|
||||
var rDoc lowbase.Contact
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareContact(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, PropertyRemoved, extChanges.Changes[0].ChangeType)
|
||||
}
|
||||
|
||||
func TestCompareContact_EmailAdded(t *testing.T) {
|
||||
|
||||
left := `url: https://pb33f.io`
|
||||
|
||||
right := `url: https://pb33f.io
|
||||
email: buckaroo@pb33f.io`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc lowbase.Contact
|
||||
var rDoc lowbase.Contact
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareContact(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, PropertyAdded, extChanges.Changes[0].ChangeType)
|
||||
|
||||
}
|
||||
|
||||
func TestCompareContact_EmailRemoved(t *testing.T) {
|
||||
|
||||
left := `url: https://pb33f.io
|
||||
email: buckaroo@pb33f.io`
|
||||
|
||||
right := `url: https://pb33f.io`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc lowbase.Contact
|
||||
var rDoc lowbase.Contact
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareContact(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, PropertyRemoved, extChanges.Changes[0].ChangeType)
|
||||
}
|
||||
|
||||
func TestCompareContact_EmailModified(t *testing.T) {
|
||||
|
||||
left := `url: https://pb33f.io
|
||||
email: buckaroo@pb33f.io`
|
||||
|
||||
right := `url: https://pb33f.io
|
||||
email: dave@quobix.com`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc lowbase.Contact
|
||||
var rDoc lowbase.Contact
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareContact(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, Modified, extChanges.Changes[0].ChangeType)
|
||||
}
|
||||
|
||||
func TestCompareContact_EmailModifiedAndMoved(t *testing.T) {
|
||||
|
||||
left := `email: buckaroo@pb33f.io
|
||||
url: https://pb33f.io`
|
||||
|
||||
right := `url: https://pb33f.io
|
||||
email: dave@quobix.com`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc lowbase.Contact
|
||||
var rDoc lowbase.Contact
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareContact(&lDoc, &rDoc)
|
||||
assert.Equal(t, 2, extChanges.TotalChanges())
|
||||
assert.Equal(t, Moved, extChanges.Changes[0].ChangeType)
|
||||
assert.Equal(t, ModifiedAndMoved, extChanges.Changes[1].ChangeType)
|
||||
}
|
||||
|
||||
func TestCompareContact_Identical(t *testing.T) {
|
||||
|
||||
left := `email: buckaroo@pb33f.io
|
||||
url: https://pb33f.io`
|
||||
|
||||
right := `email: buckaroo@pb33f.io
|
||||
url: https://pb33f.io`
|
||||
|
||||
var lNode, rNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||
|
||||
// create low level objects
|
||||
var lDoc lowbase.Contact
|
||||
var rDoc lowbase.Contact
|
||||
_ = low.BuildModel(&lNode, &lDoc)
|
||||
_ = low.BuildModel(&rNode, &rDoc)
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
extChanges := CompareContact(&lDoc, &rDoc)
|
||||
assert.Nil(t, extChanges)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -175,7 +175,6 @@ x-testing: hello`
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
// compare.
|
||||
extChanges := CompareExternalDocs(&lDoc, &rDoc)
|
||||
assert.Equal(t, 2, extChanges.TotalChanges())
|
||||
@@ -201,7 +200,6 @@ url: https://pb33f.io`
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
// compare.
|
||||
extChanges := CompareExternalDocs(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
@@ -227,7 +225,6 @@ description: something`
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
// compare.
|
||||
extChanges := CompareExternalDocs(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
@@ -236,8 +233,8 @@ description: something`
|
||||
|
||||
func TestCompareExternalDocs_URLRemoved(t *testing.T) {
|
||||
|
||||
left := `url: https://pb33f.io
|
||||
description: something`
|
||||
left := `description: something
|
||||
url: https://pb33f.io`
|
||||
|
||||
right := `description: something`
|
||||
|
||||
@@ -253,8 +250,7 @@ description: something`
|
||||
_ = lDoc.Build(lNode.Content[0], nil)
|
||||
_ = rDoc.Build(rNode.Content[0], nil)
|
||||
|
||||
// compare.
|
||||
// compare.
|
||||
// compare
|
||||
extChanges := CompareExternalDocs(&lDoc, &rDoc)
|
||||
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||
assert.Equal(t, PropertyRemoved, extChanges.Changes[0].ChangeType)
|
||||
|
||||
@@ -202,7 +202,7 @@ tags:
|
||||
|
||||
// evaluate.
|
||||
assert.Len(t, changes.Changes, 1)
|
||||
assert.Equal(t, 2, changes.TotalChanges())
|
||||
assert.Equal(t, 4, changes.TotalChanges())
|
||||
|
||||
nameChange := changes.Changes[0]
|
||||
assert.Equal(t, Moved, nameChange.ChangeType)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package what_changed
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -78,15 +79,6 @@ func CreateChange[T any](changes *[]*Change[T], changeType int, property string,
|
||||
return changes
|
||||
}
|
||||
|
||||
//func WhatChangedBetweenDocuments(leftDocument, rightDocument *lowv3.Document) *WhatChanged {
|
||||
//
|
||||
// // compare tags
|
||||
// //leftTags := leftDocument.Tags.Value
|
||||
// //rightTags := rightDocument.Tags.Value
|
||||
//
|
||||
// return nil
|
||||
//}
|
||||
|
||||
func CreateContext(l, r *yaml.Node) *ChangeContext {
|
||||
ctx := new(ChangeContext)
|
||||
if l != nil {
|
||||
@@ -105,3 +97,70 @@ func CreateContext(l, r *yaml.Node) *ChangeContext {
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
type PropertyCheck[T any] struct {
|
||||
Original T
|
||||
New T
|
||||
Label string
|
||||
LeftNode *yaml.Node
|
||||
RightNode *yaml.Node
|
||||
Breaking bool
|
||||
Changes *[]*Change[T]
|
||||
}
|
||||
|
||||
func CheckProperties[T any](properties []*PropertyCheck[T]) {
|
||||
for _, n := range properties {
|
||||
CheckPropertyAdditionOrRemoval(n.LeftNode, n.RightNode, n.Label, n.Changes, n.Breaking, n.Original, n.New)
|
||||
CheckForModification(n.LeftNode, n.RightNode, n.Label, n.Changes, n.Breaking, n.Original, n.New)
|
||||
CheckForMove(n.LeftNode, n.RightNode, n.Label, n.Changes, n.Breaking, n.Original, n.New)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckPropertyAdditionOrRemoval[T any](l, r *yaml.Node,
|
||||
label string, changes *[]*Change[T], breaking bool, orig, new T) {
|
||||
CheckForRemoval[T](l, r, label, changes, breaking, orig, new)
|
||||
CheckForAddition[T](l, r, label, changes, breaking, orig, new)
|
||||
}
|
||||
|
||||
func CheckForRemoval[T any](l, r *yaml.Node, label string, changes *[]*Change[T], breaking bool, orig, new T) {
|
||||
if l != nil && l.Value != "" && (r == nil || r.Value == "") {
|
||||
CreateChange[T](changes, PropertyRemoved, label, l, r, breaking, orig, new)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckForAddition[T any](l, r *yaml.Node, label string, changes *[]*Change[T], breaking bool, orig, new T) {
|
||||
if (l == nil || l.Value == "") && r != nil && r.Value != "" {
|
||||
CreateChange[T](changes, PropertyAdded, label, l, r, breaking, orig, new)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckForModification[T any](l, r *yaml.Node, label string, changes *[]*Change[T], breaking bool, orig, new T) {
|
||||
if l != nil && l.Value != "" && r != nil && r.Value != "" && r.Value != l.Value {
|
||||
changeType := Modified
|
||||
ctx := CreateContext(l, r)
|
||||
if ctx.HasChanged() {
|
||||
changeType = ModifiedAndMoved
|
||||
}
|
||||
CreateChange[T](changes, changeType, label, l, r, breaking, orig, new)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckForMove[T any](l, r *yaml.Node, label string, changes *[]*Change[T], breaking bool, orig, new T) {
|
||||
if l != nil && l.Value != "" && r != nil && r.Value != "" && r.Value == l.Value { // everything is equal
|
||||
ctx := CreateContext(l, r)
|
||||
if ctx.HasChanged() {
|
||||
CreateChange[T](changes, Moved, label, l, r, breaking, orig, new)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CheckExtensions[T low.HasExtensions[T]](l, r T) *ExtensionChanges {
|
||||
var lExt, rExt map[low.KeyReference[string]]low.ValueReference[any]
|
||||
if len(l.GetExtensions()) > 0 {
|
||||
lExt = l.GetExtensions()
|
||||
}
|
||||
if len(r.GetExtensions()) > 0 {
|
||||
rExt = r.GetExtensions()
|
||||
}
|
||||
return CompareExtensions(lExt, rExt)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user