Skip to content

Commit 795e4be

Browse files
authored
Migrate schema package and internal/proto6 Schema/Attribute into tfsdk package (#77)
* Migrate schema package into tfsdk package Reference: #76 This is now stuck at the below: ``` package github.com/hashicorp/terraform-plugin-framework/internal/proto6 imports github.com/hashicorp/terraform-plugin-framework/tfsdk imports github.com/hashicorp/terraform-plugin-framework/internal/proto6: import cycle not allowed ``` * Migrate internal/proto6 Schema and Attribute functions to unexported tfsdk receiver methods * Add CHANGELOG for #77
1 parent b61fdeb commit 795e4be

22 files changed

+783
-777
lines changed

.changelog/77.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:breaking-change
2+
The `schema.Attribute` and `schema.Schema` types have been moved to `tfsdk.Attribute` and `tfsdk.Schema`. No changes beyond import names are required.
3+
```

internal/proto6/schema.go

-123
This file was deleted.

schema/schema_test.go

-80
This file was deleted.

schema/attribute.go renamed to tfsdk/attribute.go

+89-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
package schema
1+
package tfsdk
22

33
import (
4+
"context"
45
"errors"
6+
"sort"
57

68
"github.com/hashicorp/terraform-plugin-framework/attr"
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
710
"github.com/hashicorp/terraform-plugin-go/tftypes"
811
)
912

@@ -119,3 +122,88 @@ func (a Attribute) Equal(o Attribute) bool {
119122
}
120123
return true
121124
}
125+
126+
// tfprotov6 returns the *tfprotov6.SchemaAttribute equivalent of an
127+
// Attribute. Errors will be tftypes.AttributePathErrors based on
128+
// `path`. `name` is the name of the attribute.
129+
func (a Attribute) tfprotov6SchemaAttribute(ctx context.Context, name string, path *tftypes.AttributePath) (*tfprotov6.SchemaAttribute, error) {
130+
schemaAttribute := &tfprotov6.SchemaAttribute{
131+
Name: name,
132+
Required: a.Required,
133+
Optional: a.Optional,
134+
Computed: a.Computed,
135+
Sensitive: a.Sensitive,
136+
}
137+
138+
if a.DeprecationMessage != "" {
139+
schemaAttribute.Deprecated = true
140+
}
141+
142+
if a.Description != "" {
143+
schemaAttribute.Description = a.Description
144+
schemaAttribute.DescriptionKind = tfprotov6.StringKindPlain
145+
}
146+
147+
if a.MarkdownDescription != "" {
148+
schemaAttribute.Description = a.MarkdownDescription
149+
schemaAttribute.DescriptionKind = tfprotov6.StringKindMarkdown
150+
}
151+
152+
if a.Attributes != nil && len(a.Attributes.GetAttributes()) > 0 && a.Type != nil {
153+
return nil, path.NewErrorf("can't have both Attributes and Type set")
154+
}
155+
156+
if (a.Attributes == nil || len(a.Attributes.GetAttributes()) < 1) && a.Type == nil {
157+
return nil, path.NewErrorf("must have Attributes or Type set")
158+
}
159+
160+
if a.Type != nil {
161+
schemaAttribute.Type = a.Type.TerraformType(ctx)
162+
163+
return schemaAttribute, nil
164+
}
165+
166+
object := &tfprotov6.SchemaObject{
167+
MinItems: a.Attributes.GetMinItems(),
168+
MaxItems: a.Attributes.GetMaxItems(),
169+
}
170+
nm := a.Attributes.GetNestingMode()
171+
switch nm {
172+
case NestingModeSingle:
173+
object.Nesting = tfprotov6.SchemaObjectNestingModeSingle
174+
case NestingModeList:
175+
object.Nesting = tfprotov6.SchemaObjectNestingModeList
176+
case NestingModeSet:
177+
object.Nesting = tfprotov6.SchemaObjectNestingModeSet
178+
case NestingModeMap:
179+
object.Nesting = tfprotov6.SchemaObjectNestingModeMap
180+
default:
181+
return nil, path.NewErrorf("unrecognized nesting mode %v", nm)
182+
}
183+
184+
for nestedName, nestedA := range a.Attributes.GetAttributes() {
185+
nestedSchemaAttribute, err := nestedA.tfprotov6SchemaAttribute(ctx, nestedName, path.WithAttributeName(nestedName))
186+
187+
if err != nil {
188+
return nil, err
189+
}
190+
191+
object.Attributes = append(object.Attributes, nestedSchemaAttribute)
192+
}
193+
194+
sort.Slice(object.Attributes, func(i, j int) bool {
195+
if object.Attributes[i] == nil {
196+
return true
197+
}
198+
199+
if object.Attributes[j] == nil {
200+
return false
201+
}
202+
203+
return object.Attributes[i].Name < object.Attributes[j].Name
204+
})
205+
206+
schemaAttribute.NestedType = object
207+
208+
return schemaAttribute, nil
209+
}

0 commit comments

Comments
 (0)