Files
libopenapi/what-changed/model/example.go
Dave Shanley fcd4a0f57d Refactoring the what-changed again
I cannot pull these models apart, they are too inter-connected and imports will cycle.
2022-11-18 11:00:34 -05:00

95 lines
2.1 KiB
Go

// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package model
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
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
}
// TotalBreakingChanges returns the total number of breaking changes made to Example
func (e *ExampleChanges) TotalBreakingChanges() int {
l := e.PropertyChanges.TotalBreakingChanges()
return l
}
// TotalChanges
func CompareExamples(l, r *base.Example) *ExampleChanges {
ec := new(ExampleChanges)
var changes []*Change
var props []*PropertyCheck
// Summary
props = append(props, &PropertyCheck{
LeftNode: l.Summary.ValueNode,
RightNode: r.Summary.ValueNode,
Label: v3.SummaryLabel,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
// Description
props = append(props, &PropertyCheck{
LeftNode: l.Description.ValueNode,
RightNode: r.Description.ValueNode,
Label: v3.DescriptionLabel,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
// Value
props = append(props, &PropertyCheck{
LeftNode: l.Value.ValueNode,
RightNode: r.Value.ValueNode,
Label: v3.ValueLabel,
Changes: &changes,
Breaking: false,
Original: l,
New: r,
})
// ExternalValue
props = append(props, &PropertyCheck{
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
}