@@ -2,6 +2,8 @@ package cli
2
2
3
3
import (
4
4
"encoding/json"
5
+ "reflect"
6
+ "strings"
5
7
"time"
6
8
7
9
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -489,6 +491,30 @@ type (
489
491
}
490
492
)
491
493
494
+ // getKnownFields uses reflection to extract JSON field names from BlueprintProperty struct
495
+ func getKnownFields (bp * BlueprintProperty ) map [string ]bool {
496
+ knownFields := make (map [string ]bool )
497
+ t := reflect .TypeOf (* bp )
498
+
499
+ for i := 0 ; i < t .NumField (); i ++ {
500
+ field := t .Field (i )
501
+
502
+ // Get the JSON tag
503
+ jsonTag := field .Tag .Get ("json" )
504
+ if jsonTag == "" || jsonTag == "-" {
505
+ continue // Skip fields without JSON tags or with "-"
506
+ }
507
+
508
+ // Handle "fieldname,omitempty" format
509
+ fieldName , _ , _ := strings .Cut (jsonTag , "," )
510
+ if fieldName != "" {
511
+ knownFields [fieldName ] = true
512
+ }
513
+ }
514
+
515
+ return knownFields
516
+ }
517
+
492
518
// Custom UnmarshalJSON for BlueprintProperty to capture dynamic fields
493
519
func (bp * BlueprintProperty ) UnmarshalJSON (data []byte ) error {
494
520
// Define an alias to avoid infinite recursion
@@ -506,21 +532,16 @@ func (bp *BlueprintProperty) UnmarshalJSON(data []byte) error {
506
532
}
507
533
508
534
// Now unmarshal into a map to capture all fields
509
- var all map [string ]interface {}
535
+ var all map [string ]any
510
536
if err := json .Unmarshal (data , & all ); err != nil {
511
537
return err
512
538
}
513
539
514
- bp .UnknownFields = make (map [string ]interface {})
540
+ // Initialize UnknownFields map
541
+ bp .UnknownFields = make (map [string ]any )
515
542
516
- // List of known fields that shouldn't go into UnknownFields
517
- knownFields := map [string ]bool {
518
- "type" : true , "title" : true , "identifier" : true , "items" : true ,
519
- "default" : true , "icon" : true , "format" : true , "maxLength" : true ,
520
- "minLength" : true , "maxItems" : true , "minItems" : true , "maximum" : true ,
521
- "minimum" : true , "description" : true , "blueprint" : true , "pattern" : true ,
522
- "enum" : true , "spec" : true , "specAuthentication" : true , "enumColors" : true ,
523
- }
543
+ // Use reflection to get known fields instead of hardcoding
544
+ knownFields := getKnownFields (bp )
524
545
525
546
// Add any unknown fields to UnknownFields
526
547
for key , value := range all {
@@ -546,7 +567,7 @@ func (bp BlueprintProperty) MarshalJSON() ([]byte, error) {
546
567
return nil , err
547
568
}
548
569
549
- var result map [string ]interface {}
570
+ var result map [string ]any
550
571
if err := json .Unmarshal (data , & result ); err != nil {
551
572
return nil , err
552
573
}
0 commit comments