@@ -12,10 +12,12 @@ import (
12
12
"strconv"
13
13
14
14
"github.com/google/uuid"
15
+ "github.com/hashicorp/go-cty/cty"
15
16
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
16
17
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
17
18
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
18
19
"github.com/mitchellh/mapstructure"
20
+ "golang.org/x/xerrors"
19
21
)
20
22
21
23
type Option struct {
@@ -62,19 +64,12 @@ func parameterDataSource() *schema.Resource {
62
64
ReadContext : func (ctx context.Context , rd * schema.ResourceData , i interface {}) diag.Diagnostics {
63
65
rd .SetId (uuid .NewString ())
64
66
65
- // 1. Read raw config to check if ptr fields are nil
66
- // 2. Update rd with nil values (it is already broken)
67
-
68
- //vm := rd.GetRawConfig().AsValueMap()["validation"].AsValueSlice()[0].AsValueMap()
69
- //log.Println(vm)
67
+ fixedValidation , err := fixValidationResourceData (rd .GetRawConfig (), rd .Get ("validation" ))
68
+ if err != nil {
69
+ return diag .FromErr (err )
70
+ }
70
71
71
- val := rd .Get ("validation" )
72
- v := val .([]interface {})
73
- k := v [0 ].(map [string ]interface {})
74
- k ["min" ] = nil
75
- k ["max" ] = nil
76
- v [0 ] = k
77
- err := rd .Set ("validation" , v )
72
+ err = rd .Set ("validation" , fixedValidation )
78
73
if err != nil {
79
74
return diag .FromErr (err )
80
75
}
@@ -115,7 +110,7 @@ func parameterDataSource() *schema.Resource {
115
110
}(),
116
111
Icon : rd .Get ("icon" ),
117
112
Option : rd .Get ("option" ),
118
- Validation : val ,
113
+ Validation : fixedValidation ,
119
114
Optional : func () bool {
120
115
// This hack allows for checking if the "default" field is present in the .tf file.
121
116
// If "default" is missing or is "null", then it means that this field is required,
@@ -339,6 +334,47 @@ func parameterDataSource() *schema.Resource {
339
334
}
340
335
}
341
336
337
+ func fixValidationResourceData (rawConfig cty.Value , validation interface {}) (interface {}, error ) {
338
+ // Read validation from raw config
339
+ rawValidation , ok := rawConfig .AsValueMap ()["validation" ]
340
+ if ! ok {
341
+ return validation , nil // no validation rules, nothing to fix
342
+ }
343
+
344
+ rawValidationArr := rawValidation .AsValueSlice ()
345
+ if len (rawValidationArr ) == 0 {
346
+ return validation , nil // no validation rules, nothing to fix
347
+ }
348
+
349
+ rawValidationRule := rawValidationArr [0 ].AsValueMap ()
350
+
351
+ // Load validation from resource data
352
+ vArr , ok := validation .([]interface {})
353
+ if ! ok {
354
+ return nil , xerrors .New ("validation should be an array" )
355
+ }
356
+
357
+ if len (vArr ) == 0 {
358
+ return validation , nil // no validation rules, nothing to fix
359
+ }
360
+
361
+ validationRule , ok := vArr [0 ].(map [string ]interface {})
362
+ if ! ok {
363
+ return nil , xerrors .New ("validation rule should be a map" )
364
+ }
365
+
366
+ // Fix the resource data
367
+ if rawValidationRule ["min" ].IsNull () {
368
+ validationRule ["min" ] = nil
369
+ }
370
+ if rawValidationRule ["max" ].IsNull () {
371
+ validationRule ["max" ] = nil
372
+ }
373
+
374
+ vArr [0 ] = validationRule
375
+ return vArr , nil
376
+ }
377
+
342
378
func valueIsType (typ , value string ) diag.Diagnostics {
343
379
switch typ {
344
380
case "number" :
0 commit comments