-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathserver.go
92 lines (82 loc) · 2.79 KB
/
server.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
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package model
import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/v3"
)
// ServerChanges represents changes found between two OpenAPI Server Objects
type ServerChanges struct {
*PropertyChanges
Server *v3.Server
ServerVariableChanges map[string]*ServerVariableChanges `json:"serverVariables,omitempty" yaml:"serverVariables,omitempty"`
ExtensionChanges *ExtensionChanges `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}
// GetAllChanges returns a slice of all changes made between SecurityRequirement objects
func (s *ServerChanges) GetAllChanges() []*Change {
var changes []*Change
changes = append(changes, s.Changes...)
for k := range s.ServerVariableChanges {
changes = append(changes, s.ServerVariableChanges[k].GetAllChanges()...)
}
if s.ExtensionChanges != nil {
changes = append(changes, s.ExtensionChanges.GetAllChanges()...)
}
return changes
}
// TotalChanges returns total changes found between two OpenAPI Server Objects
func (s *ServerChanges) TotalChanges() int {
c := s.PropertyChanges.TotalChanges()
for k := range s.ServerVariableChanges {
c += s.ServerVariableChanges[k].TotalChanges()
}
if s.ExtensionChanges != nil {
c += s.ExtensionChanges.TotalChanges()
}
return c
}
// TotalBreakingChanges returns the total number of breaking changes found between two OpenAPI Server objects.
func (s *ServerChanges) TotalBreakingChanges() int {
c := s.PropertyChanges.TotalBreakingChanges()
for k := range s.ServerVariableChanges {
c += s.ServerVariableChanges[k].TotalBreakingChanges()
}
return c
}
// CompareServers compares two OpenAPI Server objects for any changes. If anything is found, returns a pointer
// to a ServerChanges instance, or returns nil if nothing is found.
func CompareServers(l, r *v3.Server) *ServerChanges {
if low.AreEqual(l, r) {
return nil
}
var changes []*Change
var props []*PropertyCheck
// URL
props = append(props, &PropertyCheck{
LeftNode: l.URL.ValueNode,
RightNode: r.URL.ValueNode,
Label: v3.URLLabel,
Changes: &changes,
Breaking: true,
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,
})
CheckProperties(props)
sc := new(ServerChanges)
sc.PropertyChanges = NewPropertyChanges(changes)
sc.ServerVariableChanges = CheckMapForChanges(l.Variables.Value, r.Variables.Value,
&changes, v3.VariablesLabel, CompareServerVariables)
sc.ExtensionChanges = CompareExtensions(l.Extensions, r.Extensions)
sc.Server = r
return sc
}