Files
libopenapi/what-changed/example.go
Dave Shanley f7703c4266 Added example object to what-changed feature.
Ready to tackle the main event now. The Schema!

Signed-off-by: Dave Shanley <dave@quobix.com>
2022-10-05 11:33:35 -04:00

89 lines
2.0 KiB
Go

// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package what_changed
import (
"github.com/pb33f/libopenapi/datamodel/low/base"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
)
// ExampleChanges represent changes to an Example object, part of an OpenAPI specification.
type ExampleChanges struct {
PropertyChanges[*base.Example]
ExtensionChanges *ExtensionChanges
}
// TotalChanges returns the total number of changes made to Example
func (e *ExampleChanges) TotalChanges() int {
l := e.PropertyChanges.TotalChanges()
if e.ExtensionChanges != nil {
l += e.ExtensionChanges.PropertyChanges.TotalChanges()
}
return l
}
// TotalChanges
func CompareExamples(l, r *base.Example) *ExampleChanges {
ec := new(ExampleChanges)
var changes []*Change[*base.Example]
var props []*PropertyCheck[*base.Example]
// Summary
props = append(props, &PropertyCheck[*base.Example]{
LeftNode: l.Summary.ValueNode,
RightNode: r.Summary.ValueNode,
Label: v3.SummaryLabel,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
// Description
props = append(props, &PropertyCheck[*base.Example]{
LeftNode: l.Description.ValueNode,
RightNode: r.Description.ValueNode,
Label: v3.DescriptionLabel,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
// Value
props = append(props, &PropertyCheck[*base.Example]{
LeftNode: l.Value.ValueNode,
RightNode: r.Value.ValueNode,
Label: v3.ValueLabel,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
// ExternalValue
props = append(props, &PropertyCheck[*base.Example]{
LeftNode: l.ExternalValue.ValueNode,
RightNode: r.ExternalValue.ValueNode,
Label: v3.ExternalValue,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
// check properties
CheckProperties(props)
// check extensions
ec.ExtensionChanges = CheckExtensions(l, r)
ec.Changes = changes
if ec.TotalChanges() <= 0 {
return nil
}
return ec
}