|
1 |
| -package schema |
| 1 | +package tfsdk |
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "errors"
|
| 6 | + "sort" |
5 | 7 |
|
6 | 8 | "github.com/hashicorp/terraform-plugin-framework/attr"
|
| 9 | + "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
7 | 10 | "github.com/hashicorp/terraform-plugin-go/tftypes"
|
8 | 11 | )
|
9 | 12 |
|
@@ -119,3 +122,88 @@ func (a Attribute) Equal(o Attribute) bool {
|
119 | 122 | }
|
120 | 123 | return true
|
121 | 124 | }
|
| 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