Skip to content

Commit

Permalink
schema: plumb TF version generator via go generate
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Aug 5, 2022
1 parent 669a0d7 commit 86bd319
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 1 deletion.
13 changes: 12 additions & 1 deletion schema/core_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ func CoreModuleSchemaForVersion(v *version.Version) (*schema.BodySchema, error)
return mod_v0_12.ModuleSchema(ver), nil
}

return nil, fmt.Errorf("no compatible schema found for %s", v.String())
return nil, NoCompatibleSchemaErr{Version: ver}
}

//go:generate go run ../internal/versiongen -w ./versions_gen.go
func CoreModuleSchemaForConstraint(vc version.Constraints) (*schema.BodySchema, error) {
for _, v := range terraformVersions {
if vc.Check(v) {
return CoreModuleSchemaForVersion(v)
}
}

return nil, NoCompatibleSchemaErr{Constraints: vc}
}

func semVer(ver *version.Version) (*version.Version, error) {
Expand Down
49 changes: 49 additions & 0 deletions schema/core_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,53 @@ func TestCoreModuleSchemaForVersion_matching(t *testing.T) {
}
}

func TestCoreModuleSchemaForConstraint(t *testing.T) {
testCases := []struct {
constraint version.Constraints
matchedSchema *schema.BodySchema
expectedErr error
}{
{
version.MustConstraints(version.NewConstraint(">= 0.12, < 0.13")),
mod_v0_12.ModuleSchema(version.Must(version.NewVersion("0.12.31"))),
nil,
},
{
version.Constraints{},
mod_v1_2.ModuleSchema(version.Must(version.NewVersion("1.3.0"))),
nil,
},
{
version.MustConstraints(version.NewConstraint("< 0.12")),
nil,
fmt.Errorf("no compatible schema found for 0.11.15"),
},
{
version.MustConstraints(version.NewConstraint("> 999.999.999")),
nil,
fmt.Errorf("no compatible schema found for > 999.999.999"),
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.constraint.String()), func(t *testing.T) {
bodySchema, err := CoreModuleSchemaForConstraint(tc.constraint)
if err != nil && tc.expectedErr == nil {
t.Fatal(err)
}
if err != nil && err.Error() != tc.expectedErr.Error() {
t.Fatalf("expected error: %q, given: %q", err.Error(), tc.expectedErr.Error())
}
if err == nil && tc.expectedErr != nil {
t.Fatalf("expected error: %q", tc.expectedErr.Error())
}

expectedSchema := tc.matchedSchema
if diff := cmp.Diff(expectedSchema, bodySchema, ctydebug.CmpOptions); diff != "" {
t.Fatalf("schema mismatch: %s", diff)
}
})
}
}

type versionedBodySchema func(*version.Version) *schema.BodySchema
21 changes: 21 additions & 0 deletions schema/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
package schema

import (
"fmt"

"github.com/hashicorp/go-version"
)

type coreSchemaRequiredErr struct{}

func (e coreSchemaRequiredErr) Error() string {
return "core schema required (none provided)"
}

type NoCompatibleSchemaErr struct {
Version *version.Version
Constraints version.Constraints
}

func (e NoCompatibleSchemaErr) Error() string {
if e.Version != nil {
return fmt.Sprintf("no compatible schema found for %s", e.Version)
}
if e.Constraints != nil && len(e.Constraints) > 0 {
return fmt.Sprintf("no compatible schema found for %s", e.Constraints)
}
return "no compatible schema found"
}
248 changes: 248 additions & 0 deletions schema/versions_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 86bd319

Please sign in to comment.