mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 20:47:49 +00:00
Adding more use cases
Fleshing out design to see how it holds up, needs more work.
This commit is contained in:
@@ -24,8 +24,41 @@ func (e *ExternalDocChanges) TotalChanges() int {
|
|||||||
|
|
||||||
func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
|
func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
|
||||||
var changes []*Change[*lowbase.ExternalDoc]
|
var changes []*Change[*lowbase.ExternalDoc]
|
||||||
changeType := 0
|
|
||||||
if l != nil && r != nil && l.URL.Value != r.URL.Value {
|
skipURL := false
|
||||||
|
skipDescription := false
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 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
|
changeType = Modified
|
||||||
ctx := CreateContext(l.URL.ValueNode, r.URL.ValueNode)
|
ctx := CreateContext(l.URL.ValueNode, r.URL.ValueNode)
|
||||||
if ctx.HasChanged() {
|
if ctx.HasChanged() {
|
||||||
@@ -34,7 +67,10 @@ func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
|
|||||||
CreateChange[*lowbase.ExternalDoc](&changes, changeType, lowv3.URLLabel, l.URL.ValueNode,
|
CreateChange[*lowbase.ExternalDoc](&changes, changeType, lowv3.URLLabel, l.URL.ValueNode,
|
||||||
r.URL.ValueNode, false, l, r)
|
r.URL.ValueNode, false, l, r)
|
||||||
}
|
}
|
||||||
if l != nil && r != nil && l.Description.Value != r.Description.Value {
|
|
||||||
|
// 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
|
changeType = Modified
|
||||||
ctx := CreateContext(l.Description.ValueNode, r.Description.ValueNode)
|
ctx := CreateContext(l.Description.ValueNode, r.Description.ValueNode)
|
||||||
if ctx.HasChanged() {
|
if ctx.HasChanged() {
|
||||||
@@ -43,10 +79,6 @@ func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
|
|||||||
CreateChange[*lowbase.ExternalDoc](&changes, changeType, lowv3.DescriptionLabel, l.Description.ValueNode,
|
CreateChange[*lowbase.ExternalDoc](&changes, changeType, lowv3.DescriptionLabel, l.Description.ValueNode,
|
||||||
r.Description.ValueNode, false, l, r)
|
r.Description.ValueNode, false, l, r)
|
||||||
}
|
}
|
||||||
if changeType == 0 {
|
|
||||||
// no change, return nothing.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
dc := new(ExternalDocChanges)
|
dc := new(ExternalDocChanges)
|
||||||
dc.Changes = changes
|
dc.Changes = changes
|
||||||
var lExt, rExt map[low.KeyReference[string]]low.ValueReference[any]
|
var lExt, rExt map[low.KeyReference[string]]low.ValueReference[any]
|
||||||
@@ -57,5 +89,8 @@ func CompareExternalDocs(l, r *lowbase.ExternalDoc) *ExternalDocChanges {
|
|||||||
rExt = r.Extensions
|
rExt = r.Extensions
|
||||||
}
|
}
|
||||||
dc.ExtensionChanges = CompareExtensions(lExt, rExt)
|
dc.ExtensionChanges = CompareExtensions(lExt, rExt)
|
||||||
|
if len(dc.Changes) <= 0 && dc.ExtensionChanges == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return dc
|
return dc
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -153,3 +153,109 @@ x-testing: hello`
|
|||||||
extChanges := CompareExternalDocs(&lDoc, &rDoc)
|
extChanges := CompareExternalDocs(&lDoc, &rDoc)
|
||||||
assert.Nil(t, extChanges)
|
assert.Nil(t, extChanges)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCompareExternalDocs_DescriptionAdded(t *testing.T) {
|
||||||
|
|
||||||
|
left := `url: https://pb33f.io
|
||||||
|
x-testing: hello`
|
||||||
|
|
||||||
|
right := `url: https://pb33f.io
|
||||||
|
description: this is a test
|
||||||
|
x-testing: hello`
|
||||||
|
|
||||||
|
var lNode, rNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||||
|
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||||
|
|
||||||
|
// create low level objects
|
||||||
|
var lDoc lowbase.ExternalDoc
|
||||||
|
var rDoc lowbase.ExternalDoc
|
||||||
|
_ = low.BuildModel(&lNode, &lDoc)
|
||||||
|
_ = low.BuildModel(&rNode, &rDoc)
|
||||||
|
_ = lDoc.Build(lNode.Content[0], nil)
|
||||||
|
_ = rDoc.Build(rNode.Content[0], nil)
|
||||||
|
|
||||||
|
// compare.
|
||||||
|
// compare.
|
||||||
|
extChanges := CompareExternalDocs(&lDoc, &rDoc)
|
||||||
|
assert.Equal(t, 2, extChanges.TotalChanges())
|
||||||
|
assert.Equal(t, PropertyAdded, extChanges.Changes[0].ChangeType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCompareExternalDocs_URLAdded(t *testing.T) {
|
||||||
|
|
||||||
|
left := `description: hi!`
|
||||||
|
|
||||||
|
right := `description: hi!
|
||||||
|
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.ExternalDoc
|
||||||
|
var rDoc lowbase.ExternalDoc
|
||||||
|
_ = low.BuildModel(&lNode, &lDoc)
|
||||||
|
_ = low.BuildModel(&rNode, &rDoc)
|
||||||
|
_ = lDoc.Build(lNode.Content[0], nil)
|
||||||
|
_ = rDoc.Build(rNode.Content[0], nil)
|
||||||
|
|
||||||
|
// compare.
|
||||||
|
// compare.
|
||||||
|
extChanges := CompareExternalDocs(&lDoc, &rDoc)
|
||||||
|
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||||
|
assert.Equal(t, PropertyAdded, extChanges.Changes[0].ChangeType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCompareExternalDocs_DescriptionRemoved(t *testing.T) {
|
||||||
|
|
||||||
|
left := `url: https://pb33f.io
|
||||||
|
description: something`
|
||||||
|
|
||||||
|
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.ExternalDoc
|
||||||
|
var rDoc lowbase.ExternalDoc
|
||||||
|
_ = low.BuildModel(&lNode, &lDoc)
|
||||||
|
_ = low.BuildModel(&rNode, &rDoc)
|
||||||
|
_ = lDoc.Build(lNode.Content[0], nil)
|
||||||
|
_ = rDoc.Build(rNode.Content[0], nil)
|
||||||
|
|
||||||
|
// compare.
|
||||||
|
// compare.
|
||||||
|
extChanges := CompareExternalDocs(&lDoc, &rDoc)
|
||||||
|
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||||
|
assert.Equal(t, PropertyRemoved, extChanges.Changes[0].ChangeType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCompareExternalDocs_URLRemoved(t *testing.T) {
|
||||||
|
|
||||||
|
left := `url: https://pb33f.io
|
||||||
|
description: something`
|
||||||
|
|
||||||
|
right := `description: something`
|
||||||
|
|
||||||
|
var lNode, rNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||||
|
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||||
|
|
||||||
|
// create low level objects
|
||||||
|
var lDoc lowbase.ExternalDoc
|
||||||
|
var rDoc lowbase.ExternalDoc
|
||||||
|
_ = low.BuildModel(&lNode, &lDoc)
|
||||||
|
_ = low.BuildModel(&rNode, &rDoc)
|
||||||
|
_ = lDoc.Build(lNode.Content[0], nil)
|
||||||
|
_ = rDoc.Build(rNode.Content[0], nil)
|
||||||
|
|
||||||
|
// compare.
|
||||||
|
// compare.
|
||||||
|
extChanges := CompareExternalDocs(&lDoc, &rDoc)
|
||||||
|
assert.Equal(t, 1, extChanges.TotalChanges())
|
||||||
|
assert.Equal(t, PropertyRemoved, extChanges.Changes[0].ChangeType)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user