Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate schema package and internal/proto6 Schema/Attribute into tfsdk package #77

Merged
merged 3 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/77.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:breaking-change
The `schema.Attribute` and `schema.Schema` types have been moved to `tfsdk.Attribute` and `tfsdk.Schema`. No changes beyond import names are required.
```
123 changes: 0 additions & 123 deletions internal/proto6/schema.go

This file was deleted.

80 changes: 0 additions & 80 deletions schema/schema_test.go

This file was deleted.

90 changes: 89 additions & 1 deletion schema/attribute.go → tfsdk/attribute.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package schema
package tfsdk

import (
"context"
"errors"
"sort"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -119,3 +122,88 @@ func (a Attribute) Equal(o Attribute) bool {
}
return true
}

// tfprotov6 returns the *tfprotov6.SchemaAttribute equivalent of an
// Attribute. Errors will be tftypes.AttributePathErrors based on
// `path`. `name` is the name of the attribute.
func (a Attribute) tfprotov6SchemaAttribute(ctx context.Context, name string, path *tftypes.AttributePath) (*tfprotov6.SchemaAttribute, error) {
schemaAttribute := &tfprotov6.SchemaAttribute{
Name: name,
Required: a.Required,
Optional: a.Optional,
Computed: a.Computed,
Sensitive: a.Sensitive,
}

if a.DeprecationMessage != "" {
schemaAttribute.Deprecated = true
}

if a.Description != "" {
schemaAttribute.Description = a.Description
schemaAttribute.DescriptionKind = tfprotov6.StringKindPlain
}

if a.MarkdownDescription != "" {
schemaAttribute.Description = a.MarkdownDescription
schemaAttribute.DescriptionKind = tfprotov6.StringKindMarkdown
}

if a.Attributes != nil && len(a.Attributes.GetAttributes()) > 0 && a.Type != nil {
return nil, path.NewErrorf("can't have both Attributes and Type set")
}

if (a.Attributes == nil || len(a.Attributes.GetAttributes()) < 1) && a.Type == nil {
return nil, path.NewErrorf("must have Attributes or Type set")
}

if a.Type != nil {
schemaAttribute.Type = a.Type.TerraformType(ctx)

return schemaAttribute, nil
}

object := &tfprotov6.SchemaObject{
MinItems: a.Attributes.GetMinItems(),
MaxItems: a.Attributes.GetMaxItems(),
}
nm := a.Attributes.GetNestingMode()
switch nm {
case NestingModeSingle:
object.Nesting = tfprotov6.SchemaObjectNestingModeSingle
case NestingModeList:
object.Nesting = tfprotov6.SchemaObjectNestingModeList
case NestingModeSet:
object.Nesting = tfprotov6.SchemaObjectNestingModeSet
case NestingModeMap:
object.Nesting = tfprotov6.SchemaObjectNestingModeMap
default:
return nil, path.NewErrorf("unrecognized nesting mode %v", nm)
}

for nestedName, nestedA := range a.Attributes.GetAttributes() {
nestedSchemaAttribute, err := nestedA.tfprotov6SchemaAttribute(ctx, nestedName, path.WithAttributeName(nestedName))

if err != nil {
return nil, err
}

object.Attributes = append(object.Attributes, nestedSchemaAttribute)
}

sort.Slice(object.Attributes, func(i, j int) bool {
if object.Attributes[i] == nil {
return true
}

if object.Attributes[j] == nil {
return false
}

return object.Attributes[i].Name < object.Attributes[j].Name
})

schemaAttribute.NestedType = object

return schemaAttribute, nil
}
Loading