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 {
370
370
if attr == nil || attr .IsNil () {
371
371
return nil
372
372
}
373
- val := attr .Value ()
374
- if val .Type () != cty .String {
373
+
374
+ str , ok := hclext .AsString (attr .Value ())
375
+ if ! ok {
375
376
return nil
376
377
}
377
-
378
- //nolint:gocritic // string type asserted
379
- str := val .AsString ()
380
378
return & str
381
379
}
382
380
@@ -385,13 +383,9 @@ func optionalString(block *terraform.Block, key string) string {
385
383
if attr == nil || attr .IsNil () {
386
384
return ""
387
385
}
388
- val := attr .Value ()
389
- if ! val .Type ().Equals (cty .String ) {
390
- return ""
391
- }
392
386
393
- //nolint:gocritic // string type asserted
394
- return val . AsString ()
387
+ str , _ := hclext . AsString ( attr . Value ())
388
+ return str
395
389
}
396
390
397
391
func 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 7
7
"github.com/hashicorp/hcl/v2"
8
8
"github.com/hashicorp/hcl/v2/hclsyntax"
9
9
"github.com/zclconf/go-cty/cty"
10
+
11
+ "github.com/coder/preview/hclext"
10
12
)
11
13
12
14
const (
@@ -86,20 +88,9 @@ func NullString() HCLString {
86
88
// calling this function.
87
89
func (s HCLString ) AsString () string {
88
90
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
103
94
}
104
95
}
105
96
You can’t perform that action at this time.
0 commit comments