Skip to content

Commit 9a3d43d

Browse files
committed
chore: FindLatestContentVersionMatched never errors
1 parent 4cc8dec commit 9a3d43d

File tree

3 files changed

+40
-76
lines changed

3 files changed

+40
-76
lines changed

tools/cli/internal/apiversion/version.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
package apiversion
1516

1617
import (
@@ -128,7 +129,7 @@ func Parse(contentType string) (string, error) {
128129
}
129130

130131
// FindLatestContentVersionMatched finds the latest content version that matches the requested version.
131-
func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *APIVersion) (*APIVersion, error) {
132+
func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *APIVersion) *APIVersion {
132133
/*
133134
given:
134135
version: 2024-01-01
@@ -142,7 +143,7 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
142143
*/
143144
var latestVersionMatch *APIVersion
144145
if op.Responses == nil {
145-
return requestedVersion, nil
146+
return requestedVersion
146147
}
147148

148149
for _, response := range op.Responses.Map() {
@@ -153,15 +154,15 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
153154
for contentType := range response.Value.Content {
154155
contentVersion, err := New(WithContent(contentType))
155156
if err != nil {
156-
log.Printf("Ignoring invalid content type: %s", contentType)
157+
log.Printf("Ignoring invalid content type: %q", contentType)
157158
continue
158159
}
159160
if contentVersion.GreaterThan(requestedVersion) {
160161
continue
161162
}
162163

163164
if contentVersion.Equal(requestedVersion) {
164-
return contentVersion, nil
165+
return contentVersion
165166
}
166167

167168
if latestVersionMatch == nil || contentVersion.GreaterThan(latestVersionMatch) {
@@ -171,10 +172,10 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
171172
}
172173

173174
if latestVersionMatch == nil {
174-
return requestedVersion, nil
175+
return requestedVersion
175176
}
176177

177-
return latestVersionMatch, nil
178+
return latestVersionMatch
178179
}
179180

180181
// Sort versions

tools/cli/internal/apiversion/version_test.go

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"time"
2020

2121
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
2223
)
2324

2425
func TestParseVersion(t *testing.T) {
@@ -126,6 +127,7 @@ func TestNewAPIVersionFromContentType(t *testing.T) {
126127
for _, tt := range testCases {
127128
t.Run(tt.name, func(t *testing.T) {
128129
version, err := New(WithContent(tt.contentType))
130+
t.Parallel()
129131
if tt.wantErr {
130132
assert.Error(t, err)
131133
} else {
@@ -158,6 +160,7 @@ func TestApiVersion_GreaterThan(t *testing.T) {
158160

159161
for _, tt := range testCases {
160162
t.Run(tt.name, func(t *testing.T) {
163+
t.Parallel()
161164
v1, _ := New(WithVersion(tt.version1))
162165
v2, _ := New(WithVersion(tt.version2))
163166
assert.Equal(t, tt.expected, v1.GreaterThan(v2))
@@ -188,6 +191,7 @@ func TestApiVersion_LessThan(t *testing.T) {
188191

189192
for _, tt := range testCases {
190193
t.Run(tt.name, func(t *testing.T) {
194+
t.Parallel()
191195
v1, _ := New(WithVersion(tt.version1))
192196
v2, _ := New(WithVersion(tt.version2))
193197
assert.Equal(t, tt.expected, v1.LessThan(v2))
@@ -215,6 +219,7 @@ func TestApiVersion_IsZero(t *testing.T) {
215219

216220
for _, tt := range testCases {
217221
t.Run(tt.name, func(t *testing.T) {
222+
t.Parallel()
218223
v, _ := New(WithVersion(tt.version))
219224
assert.Equal(t, tt.expected, v.IsZero())
220225
})
@@ -275,6 +280,7 @@ func TestApiVersion_Equal(t *testing.T) {
275280

276281
for _, tt := range testCases {
277282
t.Run(tt.name, func(t *testing.T) {
283+
t.Parallel()
278284
v1, _ := New(WithVersion(tt.version1))
279285
v2, _ := New(WithVersion(tt.version2))
280286
assert.Equal(t, tt.expected, v1.Equal(v2))
@@ -311,6 +317,7 @@ func TestNewAPIVersionFromTime(t *testing.T) {
311317

312318
for _, tt := range testCases {
313319
t.Run(tt.name, func(t *testing.T) {
320+
t.Parallel()
314321
timeValue, _ := time.Parse("2006-01-02", tt.time)
315322
match, err := New(WithDate(timeValue))
316323
if tt.wantErr {
@@ -327,114 +334,71 @@ func TestNewVersionDate(t *testing.T) {
327334
name string
328335
version string
329336
expectedMatch string
330-
wantErr bool
337+
wantErr require.ErrorAssertionFunc
331338
}{
332339
{
333340
name: "2023-01-01",
334341
version: "2023-01-01",
335342
expectedMatch: "2023-01-01",
336-
wantErr: false,
343+
wantErr: require.NoError,
337344
},
338345
{
339346
name: "2023-01-02",
340347
version: "2023-01-02",
341348
expectedMatch: "2023-01-02",
342-
wantErr: false,
349+
wantErr: require.NoError,
343350
},
344351
{
345352
name: "2030-02-20",
346353
version: "2030-02-20",
347354
expectedMatch: "2030-02-20",
348-
wantErr: false,
355+
wantErr: require.NoError,
349356
},
350357
}
351358

352359
for _, tt := range testCases {
353360
t.Run(tt.name, func(t *testing.T) {
361+
t.Parallel()
354362
match, err := DateFromVersion(tt.version)
355-
if tt.wantErr {
356-
assert.Error(t, err)
357-
} else {
358-
assert.Equal(t, tt.expectedMatch, match.Format(dateFormat))
359-
}
363+
tt.wantErr(t, err)
364+
assert.Equal(t, tt.expectedMatch, match.Format(dateFormat))
360365
})
361366
}
362367
}
363368

364-
func TestNewAPIVersionFromDateString(t *testing.T) {
369+
func TestNew_WithVersion(t *testing.T) {
365370
testCases := []struct {
366371
name string
367372
version string
368373
expectedMatch string
369-
wantErr bool
374+
wantErr require.ErrorAssertionFunc
370375
}{
371376
{
372377
name: "2023-01-01",
373378
version: "2023-01-01",
374379
expectedMatch: "2023-01-01",
375-
wantErr: false,
380+
wantErr: require.NoError,
376381
},
377382
{
378383
name: "2023-01-02",
379384
version: "2023-01-02",
380385
expectedMatch: "2023-01-02",
381-
wantErr: false,
386+
wantErr: require.NoError,
382387
},
383388
{
384389
name: "2030-02-20",
385390
version: "2030-02-20",
386391
expectedMatch: "2030-02-20",
387-
wantErr: false,
392+
wantErr: require.NoError,
388393
},
389394
}
390395

391396
for _, tt := range testCases {
392397
t.Run(tt.name, func(t *testing.T) {
398+
t.Parallel()
393399
match, err := New(WithVersion(tt.version))
394-
if tt.wantErr {
395-
assert.Error(t, err)
396-
} else {
397-
assert.Equal(t, tt.expectedMatch, match.String())
398-
}
399-
})
400-
}
401-
}
402-
403-
func TestNewAPIVersion(t *testing.T) {
404-
testCases := []struct {
405-
name string
406-
version string
407-
expectedMatch string
408-
wantErr bool
409-
}{
410-
{
411-
name: "2023-01-01",
412-
version: "2023-01-01",
413-
expectedMatch: "2023-01-01",
414-
wantErr: false,
415-
},
416-
{
417-
name: "2023-01-02",
418-
version: "2023-01-02",
419-
expectedMatch: "2023-01-02",
420-
wantErr: false,
421-
},
422-
{
423-
name: "2030-02-20",
424-
version: "2030-02-20",
425-
expectedMatch: "2030-02-20",
426-
wantErr: false,
427-
},
428-
}
429-
430-
for _, tt := range testCases {
431-
t.Run(tt.name, func(t *testing.T) {
432-
match, err := New(WithVersion(tt.version))
433-
if tt.wantErr {
434-
assert.Error(t, err)
435-
} else {
436-
assert.Equal(t, tt.expectedMatch, match.String())
437-
}
400+
tt.wantErr(t, err)
401+
assert.Equal(t, tt.expectedMatch, match.String())
438402
})
439403
}
440404
}

tools/cli/internal/openapi/filter/extension.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
package filter
1516

1617
import (
@@ -29,9 +30,11 @@ type ExtensionFilter struct {
2930
metadata *Metadata
3031
}
3132

32-
const sunsetExtension = "x-sunset"
33-
const xGenExtension = "x-xgen-version"
34-
const format = "2006-01-02T15:04:05Z07:00"
33+
const (
34+
sunsetExtension = "x-sunset"
35+
xGenExtension = "x-xgen-version"
36+
format = "2006-01-02T15:04:05Z07:00"
37+
)
3538

3639
func (f *ExtensionFilter) Apply() error {
3740
for _, pathItem := range f.oas.Paths.Map() {
@@ -47,11 +50,7 @@ func (f *ExtensionFilter) Apply() error {
4750

4851
updateExtensionToDateString(operation.Extensions)
4952

50-
latestVersionMatch, err := apiversion.FindLatestContentVersionMatched(operation, f.metadata.targetVersion)
51-
if err != nil {
52-
return err
53-
}
54-
53+
latestVersionMatch := apiversion.FindLatestContentVersionMatched(operation, f.metadata.targetVersion)
5554
for _, response := range operation.Responses.Map() {
5655
if response == nil {
5756
continue
@@ -89,15 +88,15 @@ func updateExtensionToDateString(extensions map[string]any) {
8988
return
9089
}
9190

92-
for key, value := range extensions {
93-
if key != sunsetExtension && key != xGenExtension {
91+
for k, v := range extensions {
92+
if k != sunsetExtension && k != xGenExtension {
9493
continue
9594
}
96-
date, err := time.Parse(format, value.(string))
95+
date, err := time.Parse(format, v.(string))
9796
if err != nil {
9897
continue
9998
}
100-
extensions[key] = date.Format("2006-01-02")
99+
extensions[k] = date.Format("2006-01-02")
101100
}
102101
}
103102

0 commit comments

Comments
 (0)