Skip to content

Commit

Permalink
feat: filter experimental endpoints out of simple build
Browse files Browse the repository at this point in the history
  • Loading branch information
tinygrasshopper committed Sep 23, 2024
1 parent 070d950 commit aec51b9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
22 changes: 20 additions & 2 deletions internal/simplebuild/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,16 @@ type VersionedDoc struct {
type DocSet []VersionedDoc

func (ops Operations) Build(startVersion vervet.Version) DocSet {
versionDates := ops.VersionDates()
filteredOps := filterOutExperimentalVersions(ops)
versionDates := filteredOps.VersionDates()
versionDates = filterVersionByStartDate(versionDates, startVersion.Date)
output := make(DocSet, len(versionDates))
for idx, versionDate := range versionDates {
output[idx] = VersionedDoc{
Doc: &openapi3.T{},
VersionDate: versionDate,
}
for path, spec := range ops {
for path, spec := range filteredOps {
op := spec.GetLatest(versionDate)
if op == nil {
continue
Expand All @@ -157,6 +158,23 @@ func (ops Operations) Build(startVersion vervet.Version) DocSet {
return output
}

func filterOutExperimentalVersions(ops Operations) Operations {
filteredOps := make(Operations, len(ops))
for opKey, versionSet := range ops {
filteredVersionSet := VersionSet{}
for _, versionedOp := range versionSet {
if versionedOp.Version.Stability == vervet.StabilityExperimental {
continue
}
filteredVersionSet = append(filteredVersionSet, versionedOp)
}
if len(filteredVersionSet) != 0 {
filteredOps[opKey] = filteredVersionSet
}
}
return filteredOps
}

func filterVersionByStartDate(dates []time.Time, startDate time.Time) []time.Time {
resultDates := []time.Time{startDate}
for _, d := range dates {
Expand Down
77 changes: 77 additions & 0 deletions internal/simplebuild/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,83 @@ func TestBuild(t *testing.T) {
c.Assert(output[2].Doc.Paths.Value("/foo").Post, qt.Equals, postFoo)
c.Assert(output[2].Doc.Paths.Value("/bar").Get, qt.Equals, getBar)
})

c.Run("experimental version are filtered out", func(c *qt.C) {
versionBetaA := vervet.MustParseVersion("2024-01-01~beta")
versionGA := vervet.MustParseVersion("2024-01-02")
versionBetaB := vervet.MustParseVersion("2024-01-03~beta")
versionExperimental := vervet.MustParseVersion("2024-01-04~experimental")
versionExperimentalBeforePivotDate := vervet.MustParseVersion("2023-01-01~experimental")

getFoo := openapi3.NewOperation()
postFoo := openapi3.NewOperation()
getBar := openapi3.NewOperation()

ops := simplebuild.Operations{
simplebuild.OpKey{
Path: "/foo",
Method: "GET",
}: simplebuild.VersionSet{simplebuild.VersionedOp{
Version: versionGA,
Operation: getFoo,
ResourceName: "foo",
}},
simplebuild.OpKey{
Path: "/foo",
Method: "POST",
}: simplebuild.VersionSet{simplebuild.VersionedOp{
Version: versionBetaB,
Operation: postFoo,
ResourceName: "foo",
}},
simplebuild.OpKey{
Path: "/bar",
Method: "GET",
}: simplebuild.VersionSet{simplebuild.VersionedOp{
Version: versionBetaA,
Operation: getBar,
ResourceName: "bar",
}},
simplebuild.OpKey{
Path: "/experimental-path",
Method: "GET",
}: simplebuild.VersionSet{simplebuild.VersionedOp{
Version: versionExperimental,
Operation: openapi3.NewOperation(),
ResourceName: "experimental-path",
}},
simplebuild.OpKey{
Path: "/experimental-path-before-pivot-date",
Method: "GET",
}: simplebuild.VersionSet{simplebuild.VersionedOp{
Version: versionExperimentalBeforePivotDate,
Operation: openapi3.NewOperation(),
ResourceName: "experimental-path-before-pivot-date",
}},
}
output := ops.Build(vervet.MustParseVersion("2024-01-01"))

slices.SortFunc(output, compareDocs)

c.Assert(output, qt.HasLen, 3)

c.Assert(output[0].VersionDate, qt.Equals, versionBetaA.Date)
c.Assert(output[0].Doc.Paths.Value("/foo"), qt.IsNil)
c.Assert(output[0].Doc.Paths.Value("/bar").Get, qt.Equals, getBar)
c.Assert(output[0].Doc.Paths.Value("/experimental-path-before-pivot-date"), qt.IsNil)

c.Assert(output[1].VersionDate, qt.Equals, versionGA.Date)
c.Assert(output[1].Doc.Paths.Value("/foo").Get, qt.Equals, getFoo)
c.Assert(output[1].Doc.Paths.Value("/foo").Post, qt.IsNil)
c.Assert(output[0].Doc.Paths.Value("/bar").Get, qt.Equals, getBar)

c.Assert(output[2].VersionDate, qt.Equals, versionBetaB.Date)
c.Assert(output[2].Doc.Paths.Value("/foo").Get, qt.Equals, getFoo)
c.Assert(output[2].Doc.Paths.Value("/foo").Post, qt.Equals, postFoo)
c.Assert(output[2].Doc.Paths.Value("/bar").Get, qt.Equals, getBar)
c.Assert(output[2].Doc.Paths.Value("/experimental-path-before-pivot-date"), qt.IsNil)
c.Assert(output[2].Doc.Paths.Value("/experimental-path"), qt.IsNil)
})
}

func TestAnnotate(t *testing.T) {
Expand Down

0 comments on commit aec51b9

Please sign in to comment.