File tree Expand file tree Collapse file tree 3 files changed +36
-25
lines changed
Expand file tree Collapse file tree 3 files changed +36
-25
lines changed Original file line number Diff line number Diff line change @@ -370,13 +370,11 @@ func nullableString(block *terraform.Block, key string) *string {
370370 if attr == nil || attr .IsNil () {
371371 return nil
372372 }
373- val := attr .Value ()
374- if val .Type () != cty .String {
373+
374+ str , ok := hclext .AsString (attr .Value ())
375+ if ! ok {
375376 return nil
376377 }
377-
378- //nolint:gocritic // string type asserted
379- str := val .AsString ()
380378 return & str
381379}
382380
@@ -385,13 +383,9 @@ func optionalString(block *terraform.Block, key string) string {
385383 if attr == nil || attr .IsNil () {
386384 return ""
387385 }
388- val := attr .Value ()
389- if ! val .Type ().Equals (cty .String ) {
390- return ""
391- }
392386
393- //nolint:gocritic // string type asserted
394- return val . AsString ()
387+ str , _ := hclext . AsString ( attr . Value ())
388+ return str
395389}
396390
397391func required (block * terraform.Block , keys ... string ) hcl.Diagnostics {
Original file line number Diff line number Diff line change 1+ package hclext
2+
3+ import "github.com/zclconf/go-cty/cty"
4+
5+ func AsString (v cty.Value ) (string , bool ) {
6+ if v .IsNull () || ! v .IsKnown () {
7+ return "" , false
8+ }
9+
10+ switch {
11+ case v .Type ().Equals (cty .String ):
12+ //nolint:gocritic // string type asserted
13+ return v .AsString (), true
14+ case v .Type ().Equals (cty .Number ):
15+ // TODO: Float vs Int?
16+ return v .AsBigFloat ().String (), true
17+ case v .Type ().Equals (cty .Bool ):
18+ if v .True () {
19+ return "true" , true
20+ }
21+ return "false" , true
22+
23+ }
24+
25+ return "" , false
26+ }
Original file line number Diff line number Diff line change 77 "github.com/hashicorp/hcl/v2"
88 "github.com/hashicorp/hcl/v2/hclsyntax"
99 "github.com/zclconf/go-cty/cty"
10+
11+ "github.com/coder/preview/hclext"
1012)
1113
1214const (
@@ -86,20 +88,9 @@ func NullString() HCLString {
8688// calling this function.
8789func (s HCLString ) AsString () string {
8890 if s .Valid () && s .Value .IsKnown () {
89- switch {
90- case s .Value .Type ().Equals (cty .String ):
91- //nolint:gocritic // string type asserted
92- return s .Value .AsString ()
93- case s .Value .Type ().Equals (cty .Number ):
94- // TODO: Float vs Int?
95- return s .Value .AsBigFloat ().String ()
96- case s .Value .Type ().Equals (cty .Bool ):
97- if s .Value .True () {
98- return "true"
99- }
100- return "false"
101- default :
102- // ?? What to do?
91+ str , ok := hclext .AsString (s .Value )
92+ if ok {
93+ return str
10394 }
10495 }
10596
You can’t perform that action at this time.
0 commit comments