Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tfprotov5+tfprotov6: Introduce ValueType() methods for Schema types #158

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/158.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
tfprotov5: Added `ValueType()` methods to `Schema`, `SchemaAttribute`, `SchemaBlock`, and `SchemaNestedBlock` types.
```

```release-note:enhancement
tfprotov6: Added `ValueType()` methods to `Schema`, `SchemaAttribute`, `SchemaBlock`, `SchemaNestedBlock`, and `SchemaObject` types.
```
102 changes: 102 additions & 0 deletions tfprotov5/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ type Schema struct {
Block *SchemaBlock
}

// ValueType returns the tftypes.Type for a Schema.
//
// If Schema is missing, an empty Object is returned.
func (s *Schema) ValueType() tftypes.Type {
if s == nil {
return tftypes.Object{
AttributeTypes: map[string]tftypes.Type{},
}
}

return s.Block.ValueType()
}

// SchemaBlock represents a block in a schema. Blocks are how Terraform creates
// groupings of attributes. In configurations, they don't use the equals sign
// and use dynamic instead of list comprehensions.
Expand Down Expand Up @@ -105,6 +118,51 @@ type SchemaBlock struct {
Deprecated bool
}

// ValueType returns the tftypes.Type for a SchemaBlock.
//
// If SchemaBlock is missing, an empty Object is returned.
func (s *SchemaBlock) ValueType() tftypes.Type {
if s == nil {
return tftypes.Object{
AttributeTypes: map[string]tftypes.Type{},
}
}

attributeTypes := map[string]tftypes.Type{}

for _, attribute := range s.Attributes {
if attribute == nil {
continue
}

attributeType := attribute.ValueType()

if attributeType == nil {
continue
}

attributeTypes[attribute.Name] = attributeType
}

for _, block := range s.BlockTypes {
if block == nil {
continue
}

blockType := block.ValueType()

if blockType == nil {
continue
}

attributeTypes[block.TypeName] = blockType
}

return tftypes.Object{
AttributeTypes: attributeTypes,
}
}

// SchemaAttribute represents a single attribute within a schema block.
// Attributes are the fields users can set in configuration using the equals
// sign, can assign to variables, can interpolate, and can use list
Expand Down Expand Up @@ -162,6 +220,17 @@ type SchemaAttribute struct {
Deprecated bool
}

// ValueType returns the tftypes.Type for a SchemaAttribute.
//
// If SchemaAttribute is missing, nil is returned.
func (s *SchemaAttribute) ValueType() tftypes.Type {
if s == nil {
return nil
}

return s.Type
}

// SchemaNestedBlock is a nested block within another block. See SchemaBlock
// for more information on blocks.
type SchemaNestedBlock struct {
Expand Down Expand Up @@ -198,6 +267,39 @@ type SchemaNestedBlock struct {
MaxItems int64
}

// ValueType returns the tftypes.Type for a SchemaNestedBlock.
//
// If SchemaNestedBlock is missing or the Nesting mode is invalid, nil is
// returned.
func (s *SchemaNestedBlock) ValueType() tftypes.Type {
if s == nil {
return nil
}

blockType := s.Block.ValueType()

switch s.Nesting {
case SchemaNestedBlockNestingModeGroup:
return blockType
case SchemaNestedBlockNestingModeList:
return tftypes.List{
ElementType: blockType,
}
case SchemaNestedBlockNestingModeMap:
return tftypes.Map{
ElementType: blockType,
}
case SchemaNestedBlockNestingModeSet:
return tftypes.Set{
ElementType: blockType,
}
case SchemaNestedBlockNestingModeSingle:
return blockType
default:
return nil
}
}

// SchemaNestedBlockNestingMode indicates the nesting mode for
// SchemaNestedBlocks. The nesting mode determines the number of instances of
// the block allowed, how many labels the block expects, and the data structure
Expand Down
Loading