Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
16 changes: 5 additions & 11 deletions extract/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,11 @@ func nullableString(block *terraform.Block, key string) *string {
if attr == nil || attr.IsNil() {
return nil
}
val := attr.Value()
if val.Type() != cty.String {

str, ok := hclext.AsString(attr.Value())
if !ok {
return nil
}

//nolint:gocritic // string type asserted
str := val.AsString()
return &str
}

Expand All @@ -385,13 +383,9 @@ func optionalString(block *terraform.Block, key string) string {
if attr == nil || attr.IsNil() {
return ""
}
val := attr.Value()
if !val.Type().Equals(cty.String) {
return ""
}

//nolint:gocritic // string type asserted
return val.AsString()
str, _ := hclext.AsString(attr.Value())
return str
}

func required(block *terraform.Block, keys ...string) hcl.Diagnostics {
Expand Down
26 changes: 26 additions & 0 deletions hclext/asstring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package hclext

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

func AsString(v cty.Value) (string, bool) {
if v.IsNull() || !v.IsKnown() {
return "", false
}

switch {
case v.Type().Equals(cty.String):
//nolint:gocritic // string type asserted
return v.AsString(), true
case v.Type().Equals(cty.Number):
// TODO: Float vs Int?
return v.AsBigFloat().String(), true
case v.Type().Equals(cty.Bool):
if v.True() {
return "true", true
}
return "false", true

}

return "", false
}
19 changes: 5 additions & 14 deletions types/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"

"github.com/coder/preview/hclext"
)

const (
Expand Down Expand Up @@ -86,20 +88,9 @@ func NullString() HCLString {
// calling this function.
func (s HCLString) AsString() string {
if s.Valid() && s.Value.IsKnown() {
switch {
case s.Value.Type().Equals(cty.String):
//nolint:gocritic // string type asserted
return s.Value.AsString()
case s.Value.Type().Equals(cty.Number):
// TODO: Float vs Int?
return s.Value.AsBigFloat().String()
case s.Value.Type().Equals(cty.Bool):
if s.Value.True() {
return "true"
}
return "false"
default:
// ?? What to do?
str, ok := hclext.AsString(s.Value)
if ok {
return str
}
}

Expand Down
Loading