@@ -13,17 +13,19 @@ import (
1313)
1414
1515func ParameterFromBlock (block * terraform.Block ) (* types.Parameter , hcl.Diagnostics ) {
16- diags := required (block , "name" , "type" )
16+ diags := required (block , "name" )
1717 if diags .HasErrors () {
1818 return nil , diags
1919 }
2020
21- pType , typDiag := requiredString ("type" , block )
21+ pType , typDiag := optionalStringEnum [types.ParameterType ](block , "type" , types .ParameterTypeString , func (s types.ParameterType ) error {
22+ return s .Valid ()
23+ })
2224 if typDiag != nil {
2325 diags = diags .Append (typDiag )
2426 }
2527
26- pName , nameDiag := requiredString ("name" , block )
28+ pName , nameDiag := requiredString (block , "name" )
2729 if nameDiag != nil {
2830 diags = diags .Append (nameDiag )
2931 }
@@ -87,7 +89,7 @@ func ParameterValidationFromBlock(block *terraform.Block) (types.ParameterValida
8789 return types.ParameterValidation {}, diags
8890 }
8991
90- pErr , errDiag := requiredString ("error" , block )
92+ pErr , errDiag := requiredString (block , "error" )
9193 if errDiag != nil {
9294 diags = diags .Append (errDiag )
9395 }
@@ -97,11 +99,11 @@ func ParameterValidationFromBlock(block *terraform.Block) (types.ParameterValida
9799 }
98100
99101 p := types.ParameterValidation {
100- Regex : optionalString (block , "regex" ),
102+ Regex : nullableString (block , "regex" ),
101103 Error : pErr ,
102104 Min : nullableInteger (block , "min" ),
103105 Max : nullableInteger (block , "max" ),
104- Monotonic : optionalString (block , "monotonic" ),
106+ Monotonic : nullableString (block , "monotonic" ),
105107 }
106108
107109 return p , diags
@@ -113,12 +115,12 @@ func ParameterOptionFromBlock(block *terraform.Block) (types.ParameterOption, hc
113115 return types.ParameterOption {}, diags
114116 }
115117
116- pName , nameDiag := requiredString ("name" , block )
118+ pName , nameDiag := requiredString (block , "name" )
117119 if nameDiag != nil {
118120 diags = diags .Append (nameDiag )
119121 }
120122
121- pVal , valDiag := requiredString ("value" , block )
123+ pVal , valDiag := requiredString (block , "value" )
122124 if valDiag != nil {
123125 diags = diags .Append (valDiag )
124126 }
@@ -137,7 +139,29 @@ func ParameterOptionFromBlock(block *terraform.Block) (types.ParameterOption, hc
137139 return p , diags
138140}
139141
140- func requiredString (key string , block * terraform.Block ) (string , * hcl.Diagnostic ) {
142+ func optionalStringEnum [T ~ string ](block * terraform.Block , key string , def T , valid func (s T ) error ) (T , * hcl.Diagnostic ) {
143+ str := optionalString (block , key )
144+ if str == "" {
145+ return def , nil
146+ }
147+
148+ if err := valid (T (str )); err != nil {
149+ tyAttr := block .GetAttribute (key )
150+ return "" , & hcl.Diagnostic {
151+ Severity : hcl .DiagError ,
152+ Summary : fmt .Sprintf ("Invalid %q attribute" , key ),
153+ Detail : err .Error (),
154+ Subject : & (tyAttr .HCLAttribute ().Range ),
155+ //Context: &(block.HCLBlock().DefRange),
156+ Expression : tyAttr .HCLAttribute ().Expr ,
157+ EvalContext : block .Context ().Inner (),
158+ }
159+ }
160+
161+ return T (str ), nil
162+ }
163+
164+ func requiredString (block * terraform.Block , key string ) (string , * hcl.Diagnostic ) {
141165 tyAttr := block .GetAttribute (key )
142166 tyVal := tyAttr .Value ()
143167 if tyVal .Type () != cty .String {
@@ -210,6 +234,20 @@ func optionalInteger(block *terraform.Block, key string) int64 {
210234 return i
211235}
212236
237+ func nullableString (block * terraform.Block , key string ) * string {
238+ attr := block .GetAttribute (key )
239+ if attr == nil || attr .IsNil () {
240+ return nil
241+ }
242+ val := attr .Value ()
243+ if val .Type () != cty .String {
244+ return nil
245+ }
246+
247+ str := val .AsString ()
248+ return & str
249+ }
250+
213251func optionalString (block * terraform.Block , key string ) string {
214252 attr := block .GetAttribute (key )
215253 if attr == nil || attr .IsNil () {
0 commit comments