Refactored total change counts

Design is more uniformed and standardized, patterns are bubbling up. I think we're ready to tackle the schema soon.

Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
Dave Shanley
2022-10-05 11:24:37 -04:00
parent 0b2c3c1201
commit 3649dc594f
11 changed files with 36 additions and 26 deletions

View File

@@ -66,6 +66,11 @@ func (ex *Example) Build(root *yaml.Node, idx *index.SpecIndex) error {
return nil return nil
} }
// GetExtensions will return Example extensions to satisfy the HasExtensions interface.
func (ex *Example) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
return ex.Extensions
}
// ExtractExampleValue will extract a primitive example value (if possible), or just the raw Value property if not. // ExtractExampleValue will extract a primitive example value (if possible), or just the raw Value property if not.
func ExtractExampleValue(exp *yaml.Node) any { func ExtractExampleValue(exp *yaml.Node) any {
if utils.IsNodeBoolValue(exp) { if utils.IsNodeBoolValue(exp) {

View File

@@ -52,4 +52,7 @@ const (
AttributeLabel = "attribute" AttributeLabel = "attribute"
WrappedLabel = "wrapped" WrappedLabel = "wrapped"
PropertyNameLabel = "propertyName" PropertyNameLabel = "propertyName"
SummaryLabel = "summary"
ValueLabel = "value"
ExternalValue = "externalValue"
) )

View File

@@ -15,7 +15,7 @@ type ContactChanges struct {
// TotalChanges represents the total number of changes that have occurred to a Contact object // TotalChanges represents the total number of changes that have occurred to a Contact object
func (c *ContactChanges) TotalChanges() int { func (c *ContactChanges) TotalChanges() int {
return len(c.Changes) return c.PropertyChanges.TotalChanges()
} }
// TotalBreakingChanges always returns 0 for Contact objects, they are non-binding. // TotalBreakingChanges always returns 0 for Contact objects, they are non-binding.
@@ -69,7 +69,7 @@ func CompareContact(l, r *base.Contact) *ContactChanges {
dc := new(ContactChanges) dc := new(ContactChanges)
dc.Changes = changes dc.Changes = changes
if len(changes) <= 0 { if dc.TotalChanges() <= 0 {
return nil return nil
} }
return dc return dc

View File

@@ -76,11 +76,11 @@ func CompareDiscriminator(l, r *base.Discriminator) *DiscriminatorChanges {
} }
} }
if len(changes) <= 0 && len(mapping) <= 0 {
return nil
}
dc.Changes = changes dc.Changes = changes
dc.MappingChanges = mapping dc.MappingChanges = mapping
if dc.TotalChanges() <= 0 {
return nil
}
return dc return dc
} }

View File

@@ -14,7 +14,7 @@ type ExtensionChanges struct {
} }
func (e *ExtensionChanges) TotalChanges() int { func (e *ExtensionChanges) TotalChanges() int {
return len(e.Changes) return e.PropertyChanges.TotalChanges()
} }
// TotalBreakingChanges always returns 0 for Extension objects, they are non-binding. // TotalBreakingChanges always returns 0 for Extension objects, they are non-binding.
@@ -69,10 +69,10 @@ func CompareExtensions(l, r map[low.KeyReference[string]]low.ValueReference[any]
CheckForObjectAdditionOrRemoval[any](seenLeft, seenRight, i, &changes, false, true) CheckForObjectAdditionOrRemoval[any](seenLeft, seenRight, i, &changes, false, true)
} }
} }
if len(changes) <= 0 {
return nil
}
ex := new(ExtensionChanges) ex := new(ExtensionChanges)
ex.Changes = changes ex.Changes = changes
if ex.TotalChanges() <= 0 {
return nil
}
return ex return ex
} }

View File

@@ -24,7 +24,7 @@ func TestCompareExtensions(t *testing.T) {
extChanges := CompareExtensions(lExt, rExt) extChanges := CompareExtensions(lExt, rExt)
assert.Len(t, extChanges.TotalChanges(), 1) assert.Equal(t, extChanges.TotalChanges(), 1)
assert.Equal(t, Modified, extChanges.Changes[0].ChangeType) assert.Equal(t, Modified, extChanges.Changes[0].ChangeType)
assert.Equal(t, "1", extChanges.Changes[0].Original) assert.Equal(t, "1", extChanges.Changes[0].Original)
assert.Equal(t, "2", extChanges.Changes[0].New) assert.Equal(t, "2", extChanges.Changes[0].New)

View File

@@ -16,9 +16,9 @@ type ExternalDocChanges struct {
// TotalChanges returns a count of everything that changed // TotalChanges returns a count of everything that changed
func (e *ExternalDocChanges) TotalChanges() int { func (e *ExternalDocChanges) TotalChanges() int {
c := len(e.Changes) c := e.PropertyChanges.TotalChanges()
if e.ExtensionChanges != nil { if e.ExtensionChanges != nil {
c += len(e.ExtensionChanges.Changes) c += e.ExtensionChanges.TotalChanges()
} }
return c return c
} }
@@ -65,7 +65,7 @@ func CompareExternalDocs(l, r *base.ExternalDoc) *ExternalDocChanges {
// check extensions // check extensions
dc.ExtensionChanges = CheckExtensions(l, r) dc.ExtensionChanges = CheckExtensions(l, r)
if len(dc.Changes) <= 0 && dc.ExtensionChanges == nil { if dc.TotalChanges() <= 0 {
return nil return nil
} }
return dc return dc

View File

@@ -17,7 +17,7 @@ type InfoChanges struct {
// TotalChanges represents the total number of changes made to an Info object. // TotalChanges represents the total number of changes made to an Info object.
func (i *InfoChanges) TotalChanges() int { func (i *InfoChanges) TotalChanges() int {
t := len(i.Changes) t := i.PropertyChanges.TotalChanges()
if i.ContactChanges != nil { if i.ContactChanges != nil {
t += i.ContactChanges.TotalChanges() t += i.ContactChanges.TotalChanges()
} }
@@ -32,6 +32,9 @@ func (i *InfoChanges) TotalBreakingChanges() int {
return 0 return 0
} }
// CompareInfo will compare a left (original) and a right (new) Info object. Any changes
// will be returned in a pointer to InfoChanges, otherwise if nothing is found, then nil is
// returned instead.
func CompareInfo(l, r *base.Info) *InfoChanges { func CompareInfo(l, r *base.Info) *InfoChanges {
var changes []*Change[*base.Info] var changes []*Change[*base.Info]
@@ -113,9 +116,9 @@ func CompareInfo(l, r *base.Info) *InfoChanges {
l.License.ValueNode, nil, false, r.License.Value, nil) l.License.ValueNode, nil, false, r.License.Value, nil)
} }
} }
if len(changes) <= 0 && i.ContactChanges == nil && i.LicenseChanges == nil { i.Changes = changes
if i.TotalChanges() <= 0 {
return nil return nil
} }
i.Changes = changes
return i return i
} }

View File

@@ -15,7 +15,7 @@ type LicenseChanges struct {
// TotalChanges represents the total number of changes made to a License instance. // TotalChanges represents the total number of changes made to a License instance.
func (l *LicenseChanges) TotalChanges() int { func (l *LicenseChanges) TotalChanges() int {
return len(l.Changes) return l.PropertyChanges.TotalChanges()
} }
// TotalBreakingChanges always returns 0 for License objects, they are non-binding. // TotalBreakingChanges always returns 0 for License objects, they are non-binding.
@@ -58,7 +58,7 @@ func CompareLicense(l, r *base.License) *LicenseChanges {
lc := new(LicenseChanges) lc := new(LicenseChanges)
lc.Changes = changes lc.Changes = changes
if len(changes) <= 0 { if lc.TotalChanges() <= 0 {
return nil return nil
} }
return lc return lc

View File

@@ -19,12 +19,12 @@ type TagChanges struct {
// TotalChanges returns a count of everything that changed within tags. // TotalChanges returns a count of everything that changed within tags.
func (t *TagChanges) TotalChanges() int { func (t *TagChanges) TotalChanges() int {
c := len(t.Changes) c := t.PropertyChanges.TotalChanges()
if t.ExternalDocs != nil { if t.ExternalDocs != nil {
c += t.ExternalDocs.TotalChanges() c += t.ExternalDocs.TotalChanges()
} }
if t.ExtensionChanges != nil { if t.ExtensionChanges != nil {
c += len(t.ExtensionChanges.Changes) c += t.ExtensionChanges.TotalChanges()
} }
return c return c
} }
@@ -105,10 +105,9 @@ func CompareTags(l, r []low.ValueReference[*base.Tag]) *TagChanges {
false, nil, seenRight[i].GetValue()) false, nil, seenRight[i].GetValue())
} }
} }
tc.Changes = changes
if len(changes) <= 0 { if tc.TotalChanges() <= 0 {
return nil return nil
} }
tc.Changes = changes
return tc return tc
} }

View File

@@ -16,9 +16,9 @@ type XMLChanges struct {
// TotalChanges returns a count of everything that was changed within an XML object. // TotalChanges returns a count of everything that was changed within an XML object.
func (x *XMLChanges) TotalChanges() int { func (x *XMLChanges) TotalChanges() int {
c := len(x.Changes) c := x.PropertyChanges.TotalChanges()
if x.ExtensionChanges != nil { if x.ExtensionChanges != nil {
c += len(x.ExtensionChanges.Changes) c += x.ExtensionChanges.TotalChanges()
} }
return c return c
} }
@@ -97,7 +97,7 @@ func CompareXML(l, r *base.XML) *XMLChanges {
// check extensions // check extensions
xc.ExtensionChanges = CheckExtensions(l, r) xc.ExtensionChanges = CheckExtensions(l, r)
xc.Changes = changes xc.Changes = changes
if len(xc.Changes) <= 0 && xc.ExtensionChanges == nil { if xc.TotalChanges() <= 0 {
return nil return nil
} }
return xc return xc