Skip to content

Commit

Permalink
Embed cty.Type in tfschema.Type
Browse files Browse the repository at this point in the history
Since Terraform now uses `go mod` instead of `govendor`,
the vendor types mismatch problem has been resolved.
  • Loading branch information
minamijoyo committed May 7, 2019
1 parent 2b5315a commit 06e4a39
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
5 changes: 1 addition & 4 deletions formatter/json/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ type Attribute struct {
Name string `json:"name"`

// 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 defined in tfschema package.
// Note that Type is not cty.Type to customize string representation.
Type tfschema.Type `json:"type"`
// Required is a flag whether this attribute is required.
Required bool `json:"required"`
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/olekukonko/tablewriter v0.0.0-20180506121414-d4647c9c7a84
github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15
github.com/posener/complete v1.2.1
github.com/zclconf/go-cty v0.0.0-20190320224746-fd76348b9329
golang.org/x/lint v0.0.0-20190409202823-959b441ac422
golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
Expand Down
5 changes: 1 addition & 4 deletions tfschema/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import (
// 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 defined in this package.
// Note that Type is not cty.Type to customize string representation.
Type Type `json:"type"`
// Required is a flag whether this attribute is required.
Required bool `json:"required"`
Expand Down
29 changes: 8 additions & 21 deletions tfschema/type.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package tfschema

import (
"fmt"
"reflect"
"strings"

"github.com/zclconf/go-cty/cty"
)

// Type is a type of the attribute's value.
type Type struct {
// T is an instance of github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty.Type
// but we cannot import it, so we embed it here with the interface{}.
ctyType interface{}
// We embed cty.Type to customize string representation.
cty.Type
}

// NewType creates a new Type instance.
func NewType(t interface{}) *Type {
func NewType(t cty.Type) *Type {
return &Type{
ctyType: t,
Type: t,
}
}

Expand All @@ -31,21 +30,9 @@ func (t *Type) MarshalJSON() ([]byte, error) {
}

// Name returns a name of type.
// This method depends on the private method of cty.typeImpl.GoString().
// It's fragile but in the meantime easy to implement.
// Ideally it should be implemented by looking at the type of cty.typeImpl.
// This method customize cty.GoString() to make it easy to read.
func (t *Type) Name() (string, error) {
v := reflect.ValueOf(t.ctyType).MethodByName("GoString")
if !v.IsValid() {
return "", fmt.Errorf("Faild to find GoString(): %#v", t)
}

nv := v.Call([]reflect.Value{})
if len(nv) == 0 {
return "", fmt.Errorf("Faild to call GoString(): %#v", v)
}

goString := nv[0].String()
goString := t.GoString()
// drop `cty.` prefix for simplicity. (e.g. cty.String => String)
name := strings.Replace(goString, "cty.", "", -1)

Expand Down

0 comments on commit 06e4a39

Please sign in to comment.