@@ -44,6 +44,62 @@ type Results struct {
4444 ServedVersionValidation map [string ]map [string ][]validations.ComparisonResult `json:"servedVersionValidation,omitempty"`
4545}
4646
47+ // MarshalJSON is a custom JSON marshalling function
48+ // to ensure that we only include in the JSON/YAML rendered
49+ // output the set of validations that returned some form
50+ // of information (warnings/errors).
51+ func (rr * Results ) MarshalJSON () ([]byte , error ) {
52+ out := & struct {
53+ CRDValidation []validations.ComparisonResult `json:"crdValidation,omitempty"`
54+ SameVersionValidation map [string ]map [string ][]validations.ComparisonResult `json:"sameVersionValidation,omitempty"`
55+ ServedVersionValidation map [string ]map [string ][]validations.ComparisonResult `json:"servedVersionValidation,omitempty"`
56+ }{}
57+
58+ for _ , result := range rr .CRDValidation {
59+ if result .IsZero () {
60+ continue
61+ }
62+
63+ out .CRDValidation = append (out .CRDValidation , result )
64+ }
65+
66+ out .SameVersionValidation = dropZeroResultsFromVersionedComparisonResults (rr .SameVersionValidation )
67+ out .ServedVersionValidation = dropZeroResultsFromVersionedComparisonResults (rr .ServedVersionValidation )
68+
69+ return json .Marshal (out ) //nolint:wrapcheck
70+ }
71+
72+ // dropZeroResultsFromVersionedComparisonResults is a utility
73+ // function for dropping any results from a versioned comparison result
74+ // that does not contain any additional information (warnings/errors),
75+ // which is useful for only rendering the exact validations
76+ // that failed/warned.
77+ func dropZeroResultsFromVersionedComparisonResults (versionedComparisonResults map [string ]map [string ][]validations.ComparisonResult ) map [string ]map [string ][]validations.ComparisonResult {
78+ versionMap := map [string ]map [string ][]validations.ComparisonResult {}
79+
80+ for version , paths := range versionedComparisonResults {
81+ pathMap := map [string ][]validations.ComparisonResult {}
82+
83+ for path , comparisonResults := range paths {
84+ results := []validations.ComparisonResult {}
85+
86+ for _ , result := range comparisonResults {
87+ if result .IsZero () {
88+ continue
89+ }
90+
91+ results = append (results , result )
92+ }
93+
94+ pathMap [path ] = results
95+ }
96+
97+ versionMap [version ] = pathMap
98+ }
99+
100+ return versionMap
101+ }
102+
47103// Format is a representation of an output format.
48104type Format string
49105
@@ -100,8 +156,6 @@ func (rr *Results) RenderYAML() (string, error) {
100156func (rr * Results ) RenderMarkdown () string { //nolint:gocognit,cyclop
101157 var out strings.Builder
102158
103- out .WriteString ("# CRD Validations\n " )
104-
105159 for _ , result := range rr .CRDValidation {
106160 if len (result .Errors ) > 0 {
107161 for _ , err := range result .Errors {
@@ -114,15 +168,8 @@ func (rr *Results) RenderMarkdown() string { //nolint:gocognit,cyclop
114168 out .WriteString (fmt .Sprintf ("- **%s** - `WARNING` - %s\n " , result .Name , err ))
115169 }
116170 }
117-
118- if len (result .Errors ) == 0 && len (result .Warnings ) == 0 {
119- out .WriteString (fmt .Sprintf ("- **%s** - ✓\n " , result .Name ))
120- }
121171 }
122172
123- out .WriteString ("\n \n " )
124- out .WriteString ("# Same Version Validations\n " )
125-
126173 for version , result := range rr .SameVersionValidation {
127174 for property , results := range result {
128175 for _ , propertyResult := range results {
@@ -137,17 +184,10 @@ func (rr *Results) RenderMarkdown() string { //nolint:gocognit,cyclop
137184 out .WriteString (fmt .Sprintf ("- **%s** - *%s* - %s - `WARNING` - %s\n " , version , property , propertyResult .Name , err ))
138185 }
139186 }
140-
141- if len (propertyResult .Errors ) == 0 && len (propertyResult .Warnings ) == 0 {
142- out .WriteString (fmt .Sprintf ("- **%s** - *%s* - %s - ✓\n " , version , property , propertyResult .Name ))
143- }
144187 }
145188 }
146189 }
147190
148- out .WriteString ("\n \n " )
149- out .WriteString ("# Served Version Validations\n " )
150-
151191 for version , result := range rr .ServedVersionValidation {
152192 for property , results := range result {
153193 for _ , propertyResult := range results {
@@ -162,10 +202,6 @@ func (rr *Results) RenderMarkdown() string { //nolint:gocognit,cyclop
162202 out .WriteString (fmt .Sprintf ("- **%s** - *%s* - %s - `WARNING` - %s\n " , version , property , propertyResult .Name , err ))
163203 }
164204 }
165-
166- if len (propertyResult .Errors ) == 0 && len (propertyResult .Warnings ) == 0 {
167- out .WriteString (fmt .Sprintf ("- **%s** - *%s* - %s - ✓\n " , version , property , propertyResult .Name ))
168- }
169205 }
170206 }
171207 }
@@ -179,8 +215,6 @@ func (rr *Results) RenderMarkdown() string { //nolint:gocognit,cyclop
179215func (rr * Results ) RenderPlainText () string { //nolint:gocognit,cyclop
180216 var out strings.Builder
181217
182- out .WriteString ("CRD Validations\n " )
183-
184218 for _ , result := range rr .CRDValidation {
185219 if len (result .Errors ) > 0 {
186220 for _ , err := range result .Errors {
@@ -193,15 +227,8 @@ func (rr *Results) RenderPlainText() string { //nolint:gocognit,cyclop
193227 out .WriteString (fmt .Sprintf ("- %s - WARNING - %s\n " , result .Name , err ))
194228 }
195229 }
196-
197- if len (result .Errors ) == 0 && len (result .Warnings ) == 0 {
198- out .WriteString (fmt .Sprintf ("- %s - ✓\n " , result .Name ))
199- }
200230 }
201231
202- out .WriteString ("\n \n " )
203- out .WriteString ("Same Version Validations\n " )
204-
205232 for version , result := range rr .SameVersionValidation {
206233 for property , results := range result {
207234 for _ , propertyResult := range results {
@@ -216,17 +243,10 @@ func (rr *Results) RenderPlainText() string { //nolint:gocognit,cyclop
216243 out .WriteString (fmt .Sprintf ("- %s - %s - %s - WARNING - %s\n " , version , property , propertyResult .Name , err ))
217244 }
218245 }
219-
220- if len (propertyResult .Errors ) == 0 && len (propertyResult .Warnings ) == 0 {
221- out .WriteString (fmt .Sprintf ("- %s - %s - %s - ✓\n " , version , property , propertyResult .Name ))
222- }
223246 }
224247 }
225248 }
226249
227- out .WriteString ("\n \n " )
228- out .WriteString ("Served Version Validations\n " )
229-
230250 for version , result := range rr .ServedVersionValidation {
231251 for property , results := range result {
232252 for _ , propertyResult := range results {
@@ -241,10 +261,6 @@ func (rr *Results) RenderPlainText() string { //nolint:gocognit,cyclop
241261 out .WriteString (fmt .Sprintf ("- %s - %s - %s - WARNING - %s\n " , version , property , propertyResult .Name , err ))
242262 }
243263 }
244-
245- if len (propertyResult .Errors ) == 0 && len (propertyResult .Warnings ) == 0 {
246- out .WriteString (fmt .Sprintf ("- %s - %s - %s - ✓\n " , version , property , propertyResult .Name ))
247- }
248264 }
249265 }
250266 }
0 commit comments