Refactoring the what-changed again

I cannot pull these models apart, they are too inter-connected and imports will cycle.
This commit is contained in:
Dave Shanley
2022-10-23 12:14:31 -04:00
parent 1197562438
commit fcd4a0f57d
36 changed files with 407 additions and 372 deletions

View File

@@ -8,7 +8,6 @@ import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/base"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/what-changed/core"
"gopkg.in/yaml.v3"
"sort"
"sync"
@@ -21,7 +20,7 @@ import (
// changes, and not the child for example, adding a new schema to `anyOf` will create a new change result in
// PropertyChanges.Changes, and not in the AnyOfChanges property.
type SchemaChanges struct {
core.PropertyChanges
PropertyChanges
DiscriminatorChanges *DiscriminatorChanges
AllOfChanges []*SchemaChanges
AnyOfChanges []*SchemaChanges
@@ -131,18 +130,18 @@ func (s *SchemaChanges) TotalBreakingChanges() int {
func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
sc := new(SchemaChanges)
var changes []*core.Change
var changes []*Change
// Added
if l == nil && r != nil {
core.CreateChange(&changes, core.ObjectAdded, v3.SchemaLabel,
CreateChange(&changes, ObjectAdded, v3.SchemaLabel,
nil, nil, true, nil, r)
sc.Changes = changes
}
// Removed
if l != nil && r == nil {
core.CreateChange(&changes, core.ObjectRemoved, v3.SchemaLabel,
CreateChange(&changes, ObjectRemoved, v3.SchemaLabel,
nil, nil, true, l, nil)
sc.Changes = changes
}
@@ -157,7 +156,7 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
return nil
} else {
// references are different, that's all we care to know.
core.CreateChange(&changes, core.Modified, v3.RefLabel,
CreateChange(&changes, Modified, v3.RefLabel,
l.GetValueNode().Content[1], r.GetValueNode().Content[1], true, l.GetSchemaReference(),
r.GetSchemaReference())
sc.Changes = changes
@@ -167,7 +166,7 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
// changed from inline to ref
if !l.IsSchemaReference() && r.IsSchemaReference() {
core.CreateChange(&changes, core.Modified, v3.RefLabel,
CreateChange(&changes, Modified, v3.RefLabel,
l.GetValueNode(), r.GetValueNode().Content[1], true, l, r.GetSchemaReference())
sc.Changes = changes
return sc // we're done here
@@ -175,7 +174,7 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
// changed from ref to inline
if l.IsSchemaReference() && !r.IsSchemaReference() {
core.CreateChange(&changes, core.Modified, v3.RefLabel,
CreateChange(&changes, Modified, v3.RefLabel,
l.GetValueNode().Content[1], r.GetValueNode(), true, l.GetSchemaReference(), r)
sc.Changes = changes
return sc // done, nothing else to do.
@@ -235,15 +234,15 @@ func CompareSchemas(l, r *base.SchemaProxy) *SchemaChanges {
return sc
}
func checkSchemaXML(lSchema *base.Schema, rSchema *base.Schema, changes *[]*core.Change, sc *SchemaChanges) {
func checkSchemaXML(lSchema *base.Schema, rSchema *base.Schema, changes *[]*Change, sc *SchemaChanges) {
// XML removed
if lSchema.XML.Value != nil && rSchema.XML.Value == nil {
core.CreateChange(changes, core.ObjectRemoved, v3.XMLLabel,
CreateChange(changes, ObjectRemoved, v3.XMLLabel,
lSchema.XML.GetValueNode(), nil, true, lSchema.XML.GetValue(), nil)
}
// XML added
if lSchema.XML.Value == nil && rSchema.XML.Value != nil {
core.CreateChange(changes, core.ObjectAdded, v3.XMLLabel,
CreateChange(changes, ObjectAdded, v3.XMLLabel,
nil, rSchema.XML.GetValueNode(), false, nil, rSchema.XML.GetValue())
}
@@ -258,7 +257,7 @@ func checkSchemaXML(lSchema *base.Schema, rSchema *base.Schema, changes *[]*core
func checkPropertiesPropertyOfASchema(
lSchema *base.Schema,
rSchema *base.Schema,
changes *[]*core.Change,
changes *[]*Change,
sc *SchemaChanges,
doneChan chan bool) int {
@@ -319,10 +318,10 @@ func checkPropertiesPropertyOfASchema(
if lProps[w] != rProps[w] {
// old removed, new added.
core.CreateChange(changes, core.ObjectAdded, v3.PropertiesLabel,
CreateChange(changes, ObjectAdded, v3.PropertiesLabel,
nil, rKeyNodes[rProps[w]], false, nil, rEntities[rProps[w]])
core.CreateChange(changes, core.ObjectRemoved, v3.PropertiesLabel,
CreateChange(changes, ObjectRemoved, v3.PropertiesLabel,
lKeyNodes[lProps[w]], nil, true, lEntities[lProps[w]], nil)
}
@@ -337,7 +336,7 @@ func checkPropertiesPropertyOfASchema(
go checkProperty(lProps[w], lEntities[lProps[w]], rEntities[rProps[w]], propChanges, doneChan)
}
if w >= len(rProps) {
core.CreateChange(changes, core.ObjectRemoved, v3.PropertiesLabel,
CreateChange(changes, ObjectRemoved, v3.PropertiesLabel,
lKeyNodes[lProps[w]], nil, true, lEntities[lProps[w]], nil)
}
}
@@ -351,7 +350,7 @@ func checkPropertiesPropertyOfASchema(
go checkProperty(rProps[w], lEntities[lProps[w]], rEntities[rProps[w]], propChanges, doneChan)
}
if w >= len(lProps) {
core.CreateChange(changes, core.ObjectAdded, v3.PropertiesLabel,
CreateChange(changes, ObjectAdded, v3.PropertiesLabel,
nil, rKeyNodes[rProps[w]], false, nil, rEntities[rProps[w]])
}
}
@@ -363,12 +362,12 @@ func checkPropertiesPropertyOfASchema(
func checkSchemaPropertyChanges(
lSchema *base.Schema,
rSchema *base.Schema,
changes *[]*core.Change, sc *SchemaChanges) {
changes *[]*Change, sc *SchemaChanges) {
var props []*core.PropertyCheck
var props []*PropertyCheck
// $schema (breaking change)
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.SchemaTypeRef.ValueNode,
RightNode: rSchema.SchemaTypeRef.ValueNode,
Label: v3.SchemaDialectLabel,
@@ -379,7 +378,7 @@ func checkSchemaPropertyChanges(
})
// ExclusiveMaximum
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.ExclusiveMaximum.ValueNode,
RightNode: rSchema.ExclusiveMaximum.ValueNode,
Label: v3.ExclusiveMaximumLabel,
@@ -390,7 +389,7 @@ func checkSchemaPropertyChanges(
})
// ExclusiveMinimum
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.ExclusiveMinimum.ValueNode,
RightNode: rSchema.ExclusiveMinimum.ValueNode,
Label: v3.ExclusiveMinimumLabel,
@@ -401,7 +400,7 @@ func checkSchemaPropertyChanges(
})
// Type
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Type.ValueNode,
RightNode: rSchema.Type.ValueNode,
Label: v3.TypeLabel,
@@ -412,7 +411,7 @@ func checkSchemaPropertyChanges(
})
// Title
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Title.ValueNode,
RightNode: rSchema.Title.ValueNode,
Label: v3.TitleLabel,
@@ -423,7 +422,7 @@ func checkSchemaPropertyChanges(
})
// MultipleOf
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.MultipleOf.ValueNode,
RightNode: rSchema.MultipleOf.ValueNode,
Label: v3.MultipleOfLabel,
@@ -434,7 +433,7 @@ func checkSchemaPropertyChanges(
})
// Maximum
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Maximum.ValueNode,
RightNode: rSchema.Maximum.ValueNode,
Label: v3.MaximumLabel,
@@ -445,7 +444,7 @@ func checkSchemaPropertyChanges(
})
// Minimum
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Minimum.ValueNode,
RightNode: rSchema.Minimum.ValueNode,
Label: v3.MinimumLabel,
@@ -456,7 +455,7 @@ func checkSchemaPropertyChanges(
})
// MaxLength
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.MaxLength.ValueNode,
RightNode: rSchema.MaxLength.ValueNode,
Label: v3.MaxLengthLabel,
@@ -467,7 +466,7 @@ func checkSchemaPropertyChanges(
})
// MinLength
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.MinLength.ValueNode,
RightNode: rSchema.MinLength.ValueNode,
Label: v3.MinLengthLabel,
@@ -478,7 +477,7 @@ func checkSchemaPropertyChanges(
})
// Pattern
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Pattern.ValueNode,
RightNode: rSchema.Pattern.ValueNode,
Label: v3.PatternLabel,
@@ -489,7 +488,7 @@ func checkSchemaPropertyChanges(
})
// Format
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Format.ValueNode,
RightNode: rSchema.Format.ValueNode,
Label: v3.FormatLabel,
@@ -500,7 +499,7 @@ func checkSchemaPropertyChanges(
})
// MaxItems
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.MaxItems.ValueNode,
RightNode: rSchema.MaxItems.ValueNode,
Label: v3.MaxItemsLabel,
@@ -511,7 +510,7 @@ func checkSchemaPropertyChanges(
})
// MinItems
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.MinItems.ValueNode,
RightNode: rSchema.MinItems.ValueNode,
Label: v3.MinItemsLabel,
@@ -522,7 +521,7 @@ func checkSchemaPropertyChanges(
})
// MaxProperties
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.MaxProperties.ValueNode,
RightNode: rSchema.MaxProperties.ValueNode,
Label: v3.MaxPropertiesLabel,
@@ -533,7 +532,7 @@ func checkSchemaPropertyChanges(
})
// MinProperties
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.MinProperties.ValueNode,
RightNode: rSchema.MinProperties.ValueNode,
Label: v3.MinPropertiesLabel,
@@ -544,7 +543,7 @@ func checkSchemaPropertyChanges(
})
// UniqueItems
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.UniqueItems.ValueNode,
RightNode: rSchema.UniqueItems.ValueNode,
Label: v3.UniqueItemsLabel,
@@ -555,7 +554,7 @@ func checkSchemaPropertyChanges(
})
// AdditionalProperties
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.AdditionalProperties.ValueNode,
RightNode: rSchema.AdditionalProperties.ValueNode,
Label: v3.AdditionalPropertiesLabel,
@@ -566,7 +565,7 @@ func checkSchemaPropertyChanges(
})
// Description
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Description.ValueNode,
RightNode: rSchema.Description.ValueNode,
Label: v3.DescriptionLabel,
@@ -577,7 +576,7 @@ func checkSchemaPropertyChanges(
})
// ContentEncoding
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.ContentEncoding.ValueNode,
RightNode: rSchema.ContentEncoding.ValueNode,
Label: v3.ContentEncodingLabel,
@@ -588,7 +587,7 @@ func checkSchemaPropertyChanges(
})
// ContentMediaType
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.ContentMediaType.ValueNode,
RightNode: rSchema.ContentMediaType.ValueNode,
Label: v3.ContentMediaType,
@@ -599,7 +598,7 @@ func checkSchemaPropertyChanges(
})
// Default
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Default.ValueNode,
RightNode: rSchema.Default.ValueNode,
Label: v3.DefaultLabel,
@@ -610,7 +609,7 @@ func checkSchemaPropertyChanges(
})
// Nullable
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Nullable.ValueNode,
RightNode: rSchema.Nullable.ValueNode,
Label: v3.NullableLabel,
@@ -621,7 +620,7 @@ func checkSchemaPropertyChanges(
})
// ReadOnly
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.ReadOnly.ValueNode,
RightNode: rSchema.ReadOnly.ValueNode,
Label: v3.ReadOnlyLabel,
@@ -632,7 +631,7 @@ func checkSchemaPropertyChanges(
})
// WriteOnly
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.WriteOnly.ValueNode,
RightNode: rSchema.WriteOnly.ValueNode,
Label: v3.WriteOnlyLabel,
@@ -643,7 +642,7 @@ func checkSchemaPropertyChanges(
})
// Example
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Example.ValueNode,
RightNode: rSchema.Example.ValueNode,
Label: v3.ExampleLabel,
@@ -654,7 +653,7 @@ func checkSchemaPropertyChanges(
})
// Deprecated
props = append(props, &core.PropertyCheck{
props = append(props, &PropertyCheck{
LeftNode: lSchema.Deprecated.ValueNode,
RightNode: rSchema.Deprecated.ValueNode,
Label: v3.DeprecatedLabel,
@@ -675,14 +674,14 @@ func checkSchemaPropertyChanges(
}
for g := range k {
if _, ok := j[g]; !ok {
core.CreateChange(changes, core.PropertyAdded, v3.RequiredLabel,
CreateChange(changes, PropertyAdded, v3.RequiredLabel,
nil, rSchema.Required.Value[k[g]].GetValueNode(), true, nil,
rSchema.Required.Value[k[g]].GetValue)
}
}
for g := range j {
if _, ok := k[g]; !ok {
core.CreateChange(changes, core.PropertyRemoved, v3.RequiredLabel,
CreateChange(changes, PropertyRemoved, v3.RequiredLabel,
lSchema.Required.Value[j[g]].GetValueNode(), nil, true, lSchema.Required.Value[j[g]].GetValue,
nil)
}
@@ -699,14 +698,14 @@ func checkSchemaPropertyChanges(
}
for g := range k {
if _, ok := j[g]; !ok {
core.CreateChange(changes, core.PropertyAdded, v3.EnumLabel,
CreateChange(changes, PropertyAdded, v3.EnumLabel,
nil, rSchema.Enum.Value[k[g]].GetValueNode(), false, nil,
rSchema.Enum.Value[k[g]].GetValue)
}
}
for g := range j {
if _, ok := k[g]; !ok {
core.CreateChange(changes, core.PropertyRemoved, v3.EnumLabel,
CreateChange(changes, PropertyRemoved, v3.EnumLabel,
lSchema.Enum.Value[j[g]].GetValueNode(), nil, true, lSchema.Enum.Value[j[g]].GetValue,
nil)
}
@@ -721,12 +720,12 @@ func checkSchemaPropertyChanges(
}
// added Discriminator
if lSchema.Discriminator.Value == nil && rSchema.Discriminator.Value != nil {
core.CreateChange(changes, core.ObjectAdded, v3.DiscriminatorLabel,
CreateChange(changes, ObjectAdded, v3.DiscriminatorLabel,
nil, rSchema.Discriminator.ValueNode, true, nil, rSchema.Discriminator.Value)
}
// removed Discriminator
if lSchema.Discriminator.Value != nil && rSchema.Discriminator.Value == nil {
core.CreateChange(changes, core.ObjectRemoved, v3.DiscriminatorLabel,
CreateChange(changes, ObjectRemoved, v3.DiscriminatorLabel,
lSchema.Discriminator.ValueNode, nil, true, lSchema.Discriminator.Value, nil)
}
@@ -739,12 +738,12 @@ func checkSchemaPropertyChanges(
}
// added ExternalDocs
if lSchema.ExternalDocs.Value == nil && rSchema.ExternalDocs.Value != nil {
core.CreateChange(changes, core.ObjectAdded, v3.ExternalDocsLabel,
CreateChange(changes, ObjectAdded, v3.ExternalDocsLabel,
nil, rSchema.ExternalDocs.ValueNode, false, nil, rSchema.ExternalDocs.Value)
}
// removed ExternalDocs
if lSchema.ExternalDocs.Value != nil && rSchema.ExternalDocs.Value == nil {
core.CreateChange(changes, core.ObjectRemoved, v3.ExternalDocsLabel,
CreateChange(changes, ObjectRemoved, v3.ExternalDocsLabel,
lSchema.ExternalDocs.ValueNode, nil, false, lSchema.ExternalDocs.Value, nil)
}
@@ -752,10 +751,10 @@ func checkSchemaPropertyChanges(
sc.ExtensionChanges = CompareExtensions(lSchema.Extensions, rSchema.Extensions)
// check core properties
core.CheckProperties(props)
CheckProperties(props)
}
func checkExamples(lSchema *base.Schema, rSchema *base.Schema, changes *[]*core.Change) {
func checkExamples(lSchema *base.Schema, rSchema *base.Schema, changes *[]*Change) {
// check examples (3.1+)
var lExampKey, rExampKey []string
lExampN := make(map[string]*yaml.Node)
@@ -786,7 +785,7 @@ func checkExamples(lSchema *base.Schema, rSchema *base.Schema, changes *[]*core.
if len(lExampKey) == len(rExampKey) {
for i := range lExampKey {
if lExampKey[i] != rExampKey[i] {
core.CreateChange(changes, core.Modified, v3.ExamplesLabel,
CreateChange(changes, Modified, v3.ExamplesLabel,
lExampN[lExampKey[i]], rExampN[rExampKey[i]], false,
lExampVal[lExampKey[i]], rExampVal[rExampKey[i]])
}
@@ -796,12 +795,12 @@ func checkExamples(lSchema *base.Schema, rSchema *base.Schema, changes *[]*core.
if len(lExampKey) > len(rExampKey) {
for i := range lExampKey {
if i < len(rExampKey) && lExampKey[i] != rExampKey[i] {
core.CreateChange(changes, core.Modified, v3.ExamplesLabel,
CreateChange(changes, Modified, v3.ExamplesLabel,
lExampN[lExampKey[i]], rExampN[rExampKey[i]], false,
lExampVal[lExampKey[i]], rExampVal[rExampKey[i]])
}
if i >= len(rExampKey) {
core.CreateChange(changes, core.ObjectRemoved, v3.ExamplesLabel,
CreateChange(changes, ObjectRemoved, v3.ExamplesLabel,
lExampN[lExampKey[i]], nil, false,
lExampVal[lExampKey[i]], nil)
}
@@ -812,12 +811,12 @@ func checkExamples(lSchema *base.Schema, rSchema *base.Schema, changes *[]*core.
if len(lExampKey) < len(rExampKey) {
for i := range rExampKey {
if i < len(lExampKey) && lExampKey[i] != rExampKey[i] {
core.CreateChange(changes, core.Modified, v3.ExamplesLabel,
CreateChange(changes, Modified, v3.ExamplesLabel,
lExampN[lExampKey[i]], rExampN[rExampKey[i]], false,
lExampVal[lExampKey[i]], rExampVal[rExampKey[i]])
}
if i >= len(lExampKey) {
core.CreateChange(changes, core.ObjectAdded, v3.ExamplesLabel,
CreateChange(changes, ObjectAdded, v3.ExamplesLabel,
nil, rExampN[rExampKey[i]], false,
nil, rExampVal[rExampKey[i]])
}
@@ -830,7 +829,7 @@ func extractSchemaChanges(
rSchema []low.ValueReference[*base.SchemaProxy],
label string,
sc *[]*SchemaChanges,
changes *[]*core.Change,
changes *[]*Change,
done chan bool) {
// if there is nothing here, there is nothing to do.
@@ -885,7 +884,7 @@ func extractSchemaChanges(
*sc = append(*sc, CompareSchemas(lEntities[lKeys[w]], rEntities[rKeys[w]]))
}
if w >= len(rKeys) {
core.CreateChange(changes, core.ObjectRemoved, label,
CreateChange(changes, ObjectRemoved, label,
lEntities[lKeys[w]].GetValueNode(), nil, true, lEntities[lKeys[w]], nil)
}
}
@@ -898,7 +897,7 @@ func extractSchemaChanges(
*sc = append(*sc, CompareSchemas(lEntities[lKeys[w]], rEntities[rKeys[w]]))
}
if w >= len(lKeys) {
core.CreateChange(changes, core.ObjectAdded, label,
CreateChange(changes, ObjectAdded, label,
nil, rEntities[rKeys[w]].GetValueNode(), false, nil, rEntities[rKeys[w]])
}
}