Skip to content

Commit 8391f44

Browse files
committed
Update
1 parent cebc24e commit 8391f44

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed

tools/cli/internal/apiversion/version.go

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ type APIVersion struct {
3232

3333
const (
3434
dateFormat = "2006-01-02"
35-
StableStabilityLevel = "STABLE"
36-
PreviewStabilityLevel = "PREVIEW"
35+
StableStabilityLevel = "stable"
36+
PreviewStabilityLevel = "preview"
3737
)
3838

3939
var contentPattern = regexp.MustCompile(`application/vnd\.atlas\.((\d{4})-(\d{2})-(\d{2})|preview)\+(.+)`)
@@ -52,33 +52,34 @@ func New(opts ...Option) (*APIVersion, error) {
5252
return version, nil
5353
}
5454

55+
func (v *APIVersion) newVersion(version string, date time.Time) {
56+
v.version = version
57+
v.stabilityVersion = StableStabilityLevel
58+
v.versionDate = date
59+
60+
if IsPreviewVersion(version) {
61+
v.versionDate = time.Now()
62+
v.stabilityVersion = PreviewStabilityLevel
63+
}
64+
}
65+
5566
// WithVersion sets the version on the APIVersion.
5667
func WithVersion(version string) Option {
5768
return func(v *APIVersion) error {
58-
if strings.EqualFold(version, PreviewStabilityLevel) {
59-
v.version = version
60-
v.stabilityVersion = PreviewStabilityLevel
61-
v.versionDate = time.Now() // make preview look like the latest version
62-
return nil
63-
}
64-
6569
versionDate, err := DateFromVersion(version)
6670
if err != nil {
6771
return err
6872
}
6973

70-
v.version = version
71-
v.versionDate = versionDate
74+
v.newVersion(version, versionDate)
7275
return nil
7376
}
7477
}
7578

7679
// WithDate sets the version on the APIVersion.
7780
func WithDate(date time.Time) Option {
7881
return func(v *APIVersion) error {
79-
v.version = date.Format(dateFormat)
80-
v.versionDate = date
81-
v.stabilityVersion = StableStabilityLevel
82+
v.newVersion(date.Format(dateFormat), date)
8283
return nil
8384
}
8485
}
@@ -91,23 +92,20 @@ func WithContent(contentType string) Option {
9192
return err
9293
}
9394

94-
v.version = version
95-
v.stabilityVersion = StableStabilityLevel
96-
if version == PreviewStabilityLevel {
97-
v.stabilityVersion = PreviewStabilityLevel
98-
v.versionDate = time.Now() // make preview look like the latest version
99-
return nil
100-
}
101-
102-
v.versionDate, err = DateFromVersion(version)
95+
versionDate, err := DateFromVersion(version)
10396
if err != nil {
10497
return err
10598
}
99+
100+
v.newVersion(version, versionDate)
106101
return nil
107102
}
108103
}
109104

110105
func DateFromVersion(version string) (time.Time, error) {
106+
if IsPreviewVersion(version) {
107+
return time.Now(), nil
108+
}
111109
return time.Parse(dateFormat, version)
112110
}
113111

@@ -139,6 +137,18 @@ func (v *APIVersion) Date() time.Time {
139137
return v.versionDate
140138
}
141139

140+
func (v *APIVersion) StabilityLevel() string {
141+
return v.stabilityVersion
142+
}
143+
144+
func (v *APIVersion) ExactMatchOnly() bool {
145+
return v.stabilityVersion == PreviewStabilityLevel
146+
}
147+
148+
func IsPreviewVersion(version string) bool {
149+
return strings.EqualFold(version, PreviewStabilityLevel)
150+
}
151+
142152
func FindMatchesFromContentType(contentType string) []string {
143153
return contentPattern.FindStringSubmatch(contentType)
144154
}
@@ -169,6 +179,7 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
169179
op response:
170180
"200":
171181
content: application/vnd.atlas.2023-01-01+json
182+
content: application/vnd.atlas.preview+json
172183
"201":
173184
content: application/vnd.atlas.2023-12-01+json
174185
content: application/vnd.atlas.2025-01-01+json
@@ -190,14 +201,19 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
190201
log.Printf("Ignoring invalid content type: %q", contentType)
191202
continue
192203
}
193-
if contentVersion.GreaterThan(requestedVersion) {
194-
continue
195-
}
196204

197205
if contentVersion.Equal(requestedVersion) {
198206
return contentVersion
199207
}
200208

209+
if contentVersion.ExactMatchOnly() || requestedVersion.ExactMatchOnly() {
210+
continue
211+
}
212+
213+
if contentVersion.GreaterThan(requestedVersion) {
214+
continue
215+
}
216+
201217
if latestVersionMatch == nil || contentVersion.GreaterThan(latestVersionMatch) {
202218
latestVersionMatch = contentVersion
203219
}

tools/cli/internal/apiversion/version_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,23 @@ func TestParseVersion(t *testing.T) {
4949
wantErr: false,
5050
},
5151
{
52-
name: "preview",
52+
name: "preview_json",
5353
contentType: "application/vnd.atlas.preview+json",
5454
expectedMatch: "preview",
5555
wantErr: false,
5656
},
57+
{
58+
name: "preview_yaml",
59+
contentType: "application/vnd.atlas.preview+yaml",
60+
expectedMatch: "preview",
61+
wantErr: false,
62+
},
63+
{
64+
name: "preview_csv",
65+
contentType: "application/vnd.atlas.preview+csv",
66+
expectedMatch: "preview",
67+
wantErr: false,
68+
},
5769
{
5870
name: "invalid",
5971
contentType: "application/vnd.test.2023-01-01",
@@ -428,6 +440,11 @@ func TestFindLatestContentVersionMatched(t *testing.T) {
428440
targetVersion: "2023-01-01",
429441
expectedMatch: "2023-01-01",
430442
},
443+
{
444+
name: "exact match preview",
445+
targetVersion: "preview",
446+
expectedMatch: "preview",
447+
},
431448
{
432449
name: "exact match 2023-11-15",
433450
targetVersion: "2023-11-15",
@@ -482,6 +499,7 @@ func oasOperationAllVersions() *openapi3.Operation {
482499
responses.Set("200", &openapi3.ResponseRef{
483500
Value: &openapi3.Response{
484501
Content: map[string]*openapi3.MediaType{
502+
"application/vnd.atlas.preview+json": {},
485503
"application/vnd.atlas.2023-01-01+json": {},
486504
"application/vnd.atlas.2023-01-01+csv": {},
487505
"application/vnd.atlas.2023-02-01+json": {},

0 commit comments

Comments
 (0)