forked from Jonathan-Rosenberg/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoding_diff.go
56 lines (45 loc) · 1.96 KB
/
encoding_diff.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
package diff
import (
"fmt"
"github.com/getkin/kin-openapi/openapi3"
)
// EncodingDiff describes the changes between a pair of encoding objects: https://swagger.io/specification/#encoding-object
type EncodingDiff struct {
ExtensionsDiff *ExtensionsDiff `json:"extensions,omitempty" yaml:"extensions,omitempty"`
ContentTypeDiff *ValueDiff `json:"contentType,omitempty" yaml:"contentType,omitempty"`
HeadersDiff *HeadersDiff `json:"headers,omitempty" yaml:"headers,omitempty"`
StyleDiff *ValueDiff `json:"styleDiff,omitempty" yaml:"styleDiff,omitempty"`
ExplodeDiff *ValueDiff `json:"explode,omitempty" yaml:"explode,omitempty"`
AllowReservedDiff *ValueDiff `json:"allowReservedDiff,omitempty" yaml:"allowReservedDiff,omitempty"`
}
// Empty indicates whether a change was found in this element
func (diff *EncodingDiff) Empty() bool {
return diff == nil || *diff == EncodingDiff{}
}
func getEncodingDiff(config *Config, state *state, value1, value2 *openapi3.Encoding) (*EncodingDiff, error) {
diff, err := getEncodingDiffInternal(config, state, value1, value2)
if err != nil {
return nil, err
}
if diff.Empty() {
return nil, nil
}
return diff, nil
}
func getEncodingDiffInternal(config *Config, state *state, value1, value2 *openapi3.Encoding) (*EncodingDiff, error) {
result := EncodingDiff{}
var err error
if value1 == nil || value2 == nil {
return nil, fmt.Errorf("encoding is nil")
}
result.ExtensionsDiff = getExtensionsDiff(config, state, value1.Extensions, value2.Extensions)
result.ContentTypeDiff = getValueDiff(value1.ContentType, value2.ContentType)
result.HeadersDiff, err = getHeadersDiff(config, state, value1.Headers, value2.Headers)
if err != nil {
return nil, err
}
result.StyleDiff = getValueDiff(value1.Style, value2.Style)
result.ExplodeDiff = getBoolRefDiff(value1.Explode, value2.Explode)
result.AllowReservedDiff = getValueDiff(value1.AllowReserved, value2.AllowReserved)
return &result, nil
}