-
Notifications
You must be signed in to change notification settings - Fork 91
/
block.go
92 lines (73 loc) · 2.58 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
package toproto6
import (
"context"
"sort"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Block returns the *tfprotov6.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 tfsdk.Block) (*tfprotov6.SchemaNestedBlock, error) {
schemaNestedBlock := &tfprotov6.SchemaNestedBlock{
Block: &tfprotov6.SchemaBlock{
Deprecated: b.DeprecationMessage != "",
},
MinItems: b.MinItems,
MaxItems: b.MaxItems,
TypeName: name,
}
if b.Description != "" {
schemaNestedBlock.Block.Description = b.Description
schemaNestedBlock.Block.DescriptionKind = tfprotov6.StringKindPlain
}
if b.MarkdownDescription != "" {
schemaNestedBlock.Block.Description = b.MarkdownDescription
schemaNestedBlock.Block.DescriptionKind = tfprotov6.StringKindMarkdown
}
nm := b.NestingMode
switch nm {
case tfsdk.BlockNestingModeList:
schemaNestedBlock.Nesting = tfprotov6.SchemaNestedBlockNestingModeList
case tfsdk.BlockNestingModeSet:
schemaNestedBlock.Nesting = tfprotov6.SchemaNestedBlockNestingModeSet
default:
return nil, path.NewErrorf("unrecognized nesting mode %v", nm)
}
for attrName, attr := range b.Attributes {
attrPath := path.WithAttributeName(attrName)
attrProto6, err := SchemaAttribute(ctx, attrName, attrPath, attr)
if err != nil {
return nil, err
}
schemaNestedBlock.Block.Attributes = append(schemaNestedBlock.Block.Attributes, attrProto6)
}
for blockName, block := range b.Blocks {
blockPath := path.WithAttributeName(blockName)
blockProto6, err := Block(ctx, blockName, blockPath, block)
if err != nil {
return nil, err
}
schemaNestedBlock.Block.BlockTypes = append(schemaNestedBlock.Block.BlockTypes, blockProto6)
}
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
}