-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathxml.go
114 lines (100 loc) · 2.94 KB
/
xml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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"
)
// XMLChanges represents changes made to the XML object of an OpenAPI document.
type XMLChanges struct {
*PropertyChanges
ExtensionChanges *ExtensionChanges `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}
// GetAllChanges returns a slice of all changes made between XML objects
func (x *XMLChanges) GetAllChanges() []*Change {
var changes []*Change
changes = append(changes, x.Changes...)
if x.ExtensionChanges != nil {
changes = append(changes, x.ExtensionChanges.GetAllChanges()...)
}
return changes
}
// TotalChanges returns a count of everything that was changed within an XML object.
func (x *XMLChanges) TotalChanges() int {
c := x.PropertyChanges.TotalChanges()
if x.ExtensionChanges != nil {
c += x.ExtensionChanges.TotalChanges()
}
return c
}
// TotalBreakingChanges returns the number of breaking changes made by the XML object.
func (x *XMLChanges) TotalBreakingChanges() int {
return x.PropertyChanges.TotalBreakingChanges()
}
// CompareXML will compare a left (original) and a right (new) XML instance, and check for
// any changes between them. If changes are found, the function returns a pointer to XMLChanges,
// otherwise, if nothing changed - it will return nil
func CompareXML(l, r *base.XML) *XMLChanges {
xc := new(XMLChanges)
var changes []*Change
var props []*PropertyCheck
// Name (breaking change)
props = append(props, &PropertyCheck{
LeftNode: l.Name.ValueNode,
RightNode: r.Name.ValueNode,
Label: v3.NameLabel,
Changes: &changes,
Breaking: true,
Original: l,
New: r,
})
// Namespace (breaking change)
props = append(props, &PropertyCheck{
LeftNode: l.Namespace.ValueNode,
RightNode: r.Namespace.ValueNode,
Label: v3.NamespaceLabel,
Changes: &changes,
Breaking: true,
Original: l,
New: r,
})
// Prefix (breaking change)
props = append(props, &PropertyCheck{
LeftNode: l.Prefix.ValueNode,
RightNode: r.Prefix.ValueNode,
Label: v3.PrefixLabel,
Changes: &changes,
Breaking: true,
Original: l,
New: r,
})
// Attribute (breaking change)
props = append(props, &PropertyCheck{
LeftNode: l.Attribute.ValueNode,
RightNode: r.Attribute.ValueNode,
Label: v3.AttributeLabel,
Changes: &changes,
Breaking: true,
Original: l,
New: r,
})
// Wrapped (breaking change)
props = append(props, &PropertyCheck{
LeftNode: l.Wrapped.ValueNode,
RightNode: r.Wrapped.ValueNode,
Label: v3.WrappedLabel,
Changes: &changes,
Breaking: true,
Original: l,
New: r,
})
// check properties
CheckProperties(props)
// check extensions
xc.ExtensionChanges = CheckExtensions(l, r)
xc.PropertyChanges = NewPropertyChanges(changes)
if xc.TotalChanges() <= 0 {
return nil
}
return xc
}