@@ -18,6 +18,7 @@ import (
1818 "fmt"
1919 "log"
2020 "regexp"
21+ "strings"
2122 "time"
2223
2324 "github.com/getkin/kin-openapi/openapi3"
@@ -31,8 +32,8 @@ type APIVersion struct {
3132
3233const (
3334 dateFormat = "2006-01-02"
34- StableStabilityLevel = "STABLE "
35- PreviewStabilityLevel = "PREVIEW "
35+ StableStabilityLevel = "stable "
36+ PreviewStabilityLevel = "preview "
3637)
3738
3839var contentPattern = regexp .MustCompile (`application/vnd\.atlas\.((\d{4})-(\d{2})-(\d{2})|preview)\+(.+)` )
@@ -51,6 +52,17 @@ func New(opts ...Option) (*APIVersion, error) {
5152 return version , nil
5253}
5354
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 IsPreviewSabilityLevel (version ) {
61+ v .versionDate = time .Now ().AddDate (10 , 0 , 0 ) // set preview date to the future
62+ v .stabilityVersion = PreviewStabilityLevel
63+ }
64+ }
65+
5466// WithVersion sets the version on the APIVersion.
5567func WithVersion (version string ) Option {
5668 return func (v * APIVersion ) error {
@@ -59,18 +71,15 @@ func WithVersion(version string) Option {
5971 return err
6072 }
6173
62- v .version = version
63- v .versionDate = versionDate
74+ v .newVersion (version , versionDate )
6475 return nil
6576 }
6677}
6778
6879// WithDate sets the version on the APIVersion.
6980func WithDate (date time.Time ) Option {
7081 return func (v * APIVersion ) error {
71- v .version = date .Format (dateFormat )
72- v .versionDate = date
73- v .stabilityVersion = StableStabilityLevel
82+ v .newVersion (date .Format (dateFormat ), date )
7483 return nil
7584 }
7685}
@@ -83,22 +92,20 @@ func WithContent(contentType string) Option {
8392 return err
8493 }
8594
86- v .version = version
87- v .stabilityVersion = StableStabilityLevel
88- if version == PreviewStabilityLevel {
89- v .stabilityVersion = PreviewStabilityLevel
90- return nil
91- }
92-
93- v .versionDate , err = DateFromVersion (version )
95+ versionDate , err := DateFromVersion (version )
9496 if err != nil {
9597 return err
9698 }
99+
100+ v .newVersion (version , versionDate )
97101 return nil
98102 }
99103}
100104
101105func DateFromVersion (version string ) (time.Time , error ) {
106+ if IsPreviewSabilityLevel (version ) {
107+ return time .Now (), nil
108+ }
102109 return time .Parse (dateFormat , version )
103110}
104111
@@ -130,6 +137,26 @@ func (v *APIVersion) Date() time.Time {
130137 return v .versionDate
131138}
132139
140+ func (v * APIVersion ) StabilityLevel () string {
141+ return v .stabilityVersion
142+ }
143+
144+ func (v * APIVersion ) ExactMatchOnly () bool {
145+ return v .IsPreview ()
146+ }
147+
148+ func (v * APIVersion ) IsPreview () bool {
149+ return IsPreviewSabilityLevel (v .version )
150+ }
151+
152+ func IsPreviewSabilityLevel (value string ) bool {
153+ return strings .EqualFold (value , PreviewStabilityLevel )
154+ }
155+
156+ func IsStableSabilityLevel (value string ) bool {
157+ return strings .EqualFold (value , StableStabilityLevel )
158+ }
159+
133160func FindMatchesFromContentType (contentType string ) []string {
134161 return contentPattern .FindStringSubmatch (contentType )
135162}
@@ -160,6 +187,7 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
160187 op response:
161188 "200":
162189 content: application/vnd.atlas.2023-01-01+json
190+ content: application/vnd.atlas.preview+json
163191 "201":
164192 content: application/vnd.atlas.2023-12-01+json
165193 content: application/vnd.atlas.2025-01-01+json
@@ -181,14 +209,19 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
181209 log .Printf ("Ignoring invalid content type: %q" , contentType )
182210 continue
183211 }
184- if contentVersion .GreaterThan (requestedVersion ) {
185- continue
186- }
187212
188213 if contentVersion .Equal (requestedVersion ) {
189214 return contentVersion
190215 }
191216
217+ if contentVersion .ExactMatchOnly () || requestedVersion .ExactMatchOnly () {
218+ continue
219+ }
220+
221+ if contentVersion .GreaterThan (requestedVersion ) {
222+ continue
223+ }
224+
192225 if latestVersionMatch == nil || contentVersion .GreaterThan (latestVersionMatch ) {
193226 latestVersionMatch = contentVersion
194227 }
0 commit comments