Skip to content

Commit 5c911de

Browse files
committed
wip
1 parent 5eb9007 commit 5c911de

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

internal/fwschema/sdk_test.go

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
package fwschema_test
25

36
import (
47
"context"
8+
"fmt"
59
"maps"
610
"slices"
711
"testing"
812

913
"github.com/hashicorp/terraform-plugin-framework/attr"
1014
"github.com/hashicorp/terraform-plugin-framework/diag"
1115
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
16+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschemadata"
1217
"github.com/hashicorp/terraform-plugin-framework/path"
18+
"github.com/hashicorp/terraform-plugin-framework/types"
1319
"github.com/hashicorp/terraform-plugin-go/tftypes"
1420
sdkschema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1521
)
@@ -31,7 +37,7 @@ func TestFromSDK(t *testing.T) { //nolint:paralleltest
3137
Schema: map[string]*sdkschema.Schema{
3238
"capacity": {
3339
Type: sdkschema.TypeInt,
34-
Required: true,
40+
Optional: true,
3541
},
3642
},
3743
},
@@ -68,6 +74,58 @@ func TestFromSDK(t *testing.T) { //nolint:paralleltest
6874
}
6975
}
7076

77+
func TestReify(t *testing.T) {
78+
defer func() {
79+
if r := recover(); r != nil {
80+
t.Fatalf("Recovered from panic: %v\n", r)
81+
}
82+
}()
83+
sdkBlockSchema := sdkschema.Resource{
84+
Schema: map[string]*sdkschema.Schema{
85+
"capacity": {
86+
Type: sdkschema.TypeInt,
87+
Required: true,
88+
},
89+
},
90+
}
91+
sdkResourceSchema := sdkschema.Resource{
92+
Schema: map[string]*sdkschema.Schema{
93+
"cpu": {
94+
Type: sdkschema.TypeInt,
95+
Required: true,
96+
},
97+
"disk": {
98+
Type: sdkschema.TypeList,
99+
Elem: &sdkBlockSchema,
100+
},
101+
},
102+
}
103+
104+
ctx := context.Background()
105+
fwSchema := NewSDKSchema(sdkResourceSchema)
106+
107+
fwSchemaType := fwSchema.Type()
108+
fmt.Printf("fwSchemaType: %#v\n", fwSchemaType)
109+
tfSchemaType := fwSchemaType.TerraformType(ctx)
110+
tfBlockSchemaType := NewSDKSchema(sdkBlockSchema).Type().TerraformType(ctx)
111+
tfBlockValue := tftypes.NewValue(tfBlockSchemaType, map[string]tftypes.Value{})
112+
tfValue := tftypes.NewValue(
113+
tfSchemaType,
114+
map[string]tftypes.Value{
115+
"disk": tftypes.NewValue(tfBlockSchemaType, tfBlockValue),
116+
},
117+
)
118+
119+
fwData := fwschemadata.Data{
120+
Description: fwschemadata.DataDescriptionResourceIdentity,
121+
Schema: fwSchema,
122+
TerraformValue: tfValue,
123+
}
124+
fwData.ReifyNullCollectionBlocks(ctx)
125+
fmt.Printf("%#v\n", fwData.TerraformValue)
126+
127+
}
128+
71129
var _ fwschema.Schema = &SDKSchema{}
72130
var _ fwschema.Attribute = &SDKAttribute{}
73131
var _ fwschema.Block = &SDKBlock{}
@@ -81,6 +139,7 @@ type SDKSchema struct {
81139
}
82140

83141
type SDKAttribute struct {
142+
typ attr.Type
84143
sdkSchema *sdkschema.Schema
85144
}
86145

@@ -138,7 +197,9 @@ func (s *SDKBlock) GetNestingMode() fwschema.BlockNestingMode {
138197

139198
// Type should return the framework type of a block.
140199
func (s *SDKBlock) Type() attr.Type {
141-
return nil
200+
return types.ListType{
201+
// ElemType: s.NestedObject().Type(),
202+
}
142203
}
143204

144205
// Return the attribute or element the AttributePathStep is referring
@@ -179,7 +240,7 @@ func (s *SDKAttribute) GetMarkdownDescription() string {
179240
// differently than Type to prevent a conflict with the tfsdk.Attribute
180241
// field name.
181242
func (s *SDKAttribute) GetType() attr.Type {
182-
return nil
243+
return s.typ
183244
}
184245

185246
// IsComputed should return true if the attribute configuration value is
@@ -261,8 +322,10 @@ func (s *SDKSchema) GetAttributes() map[string]fwschema.Attribute {
261322
schemaMap := s.sdkResourceSchema.Schema
262323
for name, sdkAttr := range schemaMap {
263324
switch sdkAttr.Type {
264-
case sdkschema.TypeInt, sdkschema.TypeString:
265-
attributes[name] = &SDKAttribute{sdkSchema: sdkAttr}
325+
case sdkschema.TypeInt:
326+
attributes[name] = &SDKAttribute{typ: types.NumberType, sdkSchema: sdkAttr}
327+
case sdkschema.TypeString:
328+
attributes[name] = &SDKAttribute{typ: types.StringType, sdkSchema: sdkAttr}
266329
}
267330
}
268331

@@ -317,7 +380,7 @@ func (s *SDKSchema) GetVersion() int64 {
317380

318381
// Type should return the framework type of the schema.
319382
func (s *SDKSchema) Type() attr.Type {
320-
return nil
383+
return fwschema.SchemaType(s)
321384
}
322385

323386
// TypeAtPath should return the framework type of the Attribute at the

0 commit comments

Comments
 (0)