/
block.go
94 lines (74 loc) · 2.78 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package toproto5
import (
"context"
"sort"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Block returns the *tfprotov5.SchemaNestedBlock equivalent of a Block.
// Errors will be tftypes.AttributePathErrors based on `path`. `name` is the
// name of the attribute.
func Block(ctx context.Context, name string, path *tftypes.AttributePath, b fwschema.Block) (*tfprotov5.SchemaNestedBlock, error) {
schemaNestedBlock := &tfprotov5.SchemaNestedBlock{
Block: &tfprotov5.SchemaBlock{
Deprecated: b.GetDeprecationMessage() != "",
},
TypeName: name,
}
if b.GetDescription() != "" {
schemaNestedBlock.Block.Description = b.GetDescription()
schemaNestedBlock.Block.DescriptionKind = tfprotov5.StringKindPlain
}
if b.GetMarkdownDescription() != "" {
schemaNestedBlock.Block.Description = b.GetMarkdownDescription()
schemaNestedBlock.Block.DescriptionKind = tfprotov5.StringKindMarkdown
}
nm := b.GetNestingMode()
switch nm {
case fwschema.BlockNestingModeList:
schemaNestedBlock.Nesting = tfprotov5.SchemaNestedBlockNestingModeList
case fwschema.BlockNestingModeSet:
schemaNestedBlock.Nesting = tfprotov5.SchemaNestedBlockNestingModeSet
case fwschema.BlockNestingModeSingle:
schemaNestedBlock.Nesting = tfprotov5.SchemaNestedBlockNestingModeSingle
default:
return nil, path.NewErrorf("unrecognized nesting mode %v", nm)
}
nestedBlockObject := b.GetNestedObject()
for attrName, attr := range nestedBlockObject.GetAttributes() {
attrPath := path.WithAttributeName(attrName)
attrProto5, err := SchemaAttribute(ctx, attrName, attrPath, attr)
if err != nil {
return nil, err
}
schemaNestedBlock.Block.Attributes = append(schemaNestedBlock.Block.Attributes, attrProto5)
}
for blockName, block := range nestedBlockObject.GetBlocks() {
blockPath := path.WithAttributeName(blockName)
blockProto5, err := Block(ctx, blockName, blockPath, block)
if err != nil {
return nil, err
}
schemaNestedBlock.Block.BlockTypes = append(schemaNestedBlock.Block.BlockTypes, blockProto5)
}
sort.Slice(schemaNestedBlock.Block.Attributes, func(i, j int) bool {
if schemaNestedBlock.Block.Attributes[i] == nil {
return true
}
if schemaNestedBlock.Block.Attributes[j] == nil {
return false
}
return schemaNestedBlock.Block.Attributes[i].Name < schemaNestedBlock.Block.Attributes[j].Name
})
sort.Slice(schemaNestedBlock.Block.BlockTypes, func(i, j int) bool {
if schemaNestedBlock.Block.BlockTypes[i] == nil {
return true
}
if schemaNestedBlock.Block.BlockTypes[j] == nil {
return false
}
return schemaNestedBlock.Block.BlockTypes[i].TypeName < schemaNestedBlock.Block.BlockTypes[j].TypeName
})
return schemaNestedBlock, nil
}