Skip to content

Commit

Permalink
Split schema.go to block/nested_block/attribute.go
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Feb 3, 2018
1 parent b24f726 commit b320ed9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 92 deletions.
29 changes: 29 additions & 0 deletions tfschema/attribute.go
@@ -0,0 +1,29 @@
package tfschema

import (
"github.com/hashicorp/terraform/config/configschema"
)

// Attribute is wrapper for configschema.Attribute
type Attribute struct {
// Type is a type of the attribute's value.
// Note that Type is not cty.Type
// We cannot import github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty/cty
// On the other hand, tfschema does not need a dynamic type.
// So, we use a simple representation of type.
Type Type `json:"type"`
Required bool `json:"required"`
Optional bool `json:"optional"`
Computed bool `json:"computed"`
Sensitive bool `json:"sensitive"`
}

func NewAttribute(a *configschema.Attribute) *Attribute {
return &Attribute{
Type: *NewType(a.Type),
Required: a.Required,
Optional: a.Optional,
Computed: a.Computed,
Sensitive: a.Sensitive,
}
}
49 changes: 49 additions & 0 deletions tfschema/block.go
@@ -0,0 +1,49 @@
package tfschema

import (
"encoding/json"

"github.com/hashicorp/terraform/config/configschema"
)

// Block is wrapper for configschema.Block
type Block struct {
Attributes map[string]*Attribute `json:"attributes"`
BlockTypes map[string]*NestedBlock `json:"block_types"`
}

func NewBlock(b *configschema.Block) *Block {
return &Block{
Attributes: NewAttributes(b.Attributes),
BlockTypes: NewBlockTypes(b.BlockTypes),
}
}

func NewAttributes(as map[string]*configschema.Attribute) map[string]*Attribute {
m := make(map[string]*Attribute)

for k, v := range as {
m[k] = NewAttribute(v)
}

return m
}

func NewBlockTypes(bs map[string]*configschema.NestedBlock) map[string]*NestedBlock {
m := make(map[string]*NestedBlock)

for k, v := range bs {
m[k] = NewNestedBlock(v)
}

return m
}

func (b *Block) FormatJSON() (string, error) {
bytes, err := json.MarshalIndent(b, "", " ")
if err != nil {
return "", err
}

return string(bytes), nil
}
12 changes: 0 additions & 12 deletions tfschema/format.go

This file was deleted.

23 changes: 23 additions & 0 deletions tfschema/nested_block.go
@@ -0,0 +1,23 @@
package tfschema

import (
"github.com/hashicorp/terraform/config/configschema"
)

// NestedBlock is wrapper for configschema.NestedBlock
type NestedBlock struct {
Block
Nesting configschema.NestingMode `json:"nesting"`
MinItems int `json:"min_items"`
MaxItems int `json:"max_items"`
}

func NewNestedBlock(b *configschema.NestedBlock) *NestedBlock {
block := NewBlock(&b.Block)
return &NestedBlock{
Block: *block,
Nesting: b.Nesting,
MinItems: b.MinItems,
MaxItems: b.MaxItems,
}
}
80 changes: 0 additions & 80 deletions tfschema/schema.go

This file was deleted.

0 comments on commit b320ed9

Please sign in to comment.