forked from Tufin/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_type_diff.go
75 lines (60 loc) · 2.2 KB
/
media_type_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package diff
import (
"fmt"
"github.com/rjmohammad/kin-openapi/openapi3"
)
// MediaTypeDiff describes the changes between a pair of media type objects
type MediaTypeDiff struct {
ExtensionsDiff *ExtensionsDiff `json:"extensions,omitempty" yaml:"extensions,omitempty"`
SchemaDiff *SchemaDiff `json:"schema,omitempty" yaml:"schema,omitempty"`
ExampleDiff *ValueDiff `json:"example,omitempty" yaml:"example,omitempty"`
ExamplesDiff *ExamplesDiff `json:"examples,omitempty" yaml:"examples,omitempty"`
EncodingsDiff *EncodingsDiff `json:"encoding,omitempty" yaml:"encoding,omitempty"`
}
// Empty indicates whether a change was found in this element
func (diff *MediaTypeDiff) Empty() bool {
return diff == nil || *diff == MediaTypeDiff{}
}
func (diff *MediaTypeDiff) removeNonBreaking() {
if diff.Empty() {
return
}
diff.ExtensionsDiff = nil
diff.ExampleDiff = nil
diff.ExampleDiff = nil
}
func getMediaTypeDiff(config *Config, state *state, mediaType1 *openapi3.MediaType, mediaType2 *openapi3.MediaType) (*MediaTypeDiff, error) {
diff, err := getMediaTypeDiffInternal(config, state, mediaType1, mediaType2)
if err != nil {
return nil, err
}
if config.BreakingOnly {
diff.removeNonBreaking()
}
if diff.Empty() {
return nil, nil
}
return diff, nil
}
func getMediaTypeDiffInternal(config *Config, state *state, mediaType1 *openapi3.MediaType, mediaType2 *openapi3.MediaType) (*MediaTypeDiff, error) {
result := MediaTypeDiff{}
var err error
if mediaType1 == nil || mediaType2 == nil {
return nil, fmt.Errorf("media type is nil")
}
result.ExtensionsDiff = getExtensionsDiff(config, state, mediaType1.ExtensionProps, mediaType2.ExtensionProps)
result.SchemaDiff, err = getSchemaDiff(config, state, mediaType1.Schema, mediaType2.Schema)
if err != nil {
return nil, err
}
result.ExampleDiff = getValueDiffConditional(config.ExcludeExamples, mediaType1.Example, mediaType2.Example)
result.EncodingsDiff, err = getEncodingsDiff(config, state, mediaType1.Encoding, mediaType2.Encoding)
if err != nil {
return nil, err
}
result.ExamplesDiff, err = getExamplesDiff(config, state, mediaType1.Examples, mediaType2.Examples)
if err != nil {
return nil, err
}
return &result, nil
}