Skip to content

Commit dcda3f4

Browse files
add dynamic unknown fields to the blueprint property types (#260)
1 parent 4092e98 commit dcda3f4

File tree

1 file changed

+92
-20
lines changed

1 file changed

+92
-20
lines changed

internal/cli/models.go

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"encoding/json"
45
"time"
56

67
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -45,26 +46,28 @@ type (
4546
}
4647

4748
BlueprintProperty struct {
48-
Type string `json:"type,omitempty"`
49-
Title *string `json:"title,omitempty"`
50-
Identifier string `json:"identifier,omitempty"`
51-
Items map[string]any `json:"items,omitempty"`
52-
Default any `json:"default,omitempty"`
53-
Icon *string `json:"icon,omitempty"`
54-
Format *string `json:"format,omitempty"`
55-
MaxLength *int `json:"maxLength,omitempty"`
56-
MinLength *int `json:"minLength,omitempty"`
57-
MaxItems *int `json:"maxItems,omitempty"`
58-
MinItems *int `json:"minItems,omitempty"`
59-
Maximum *float64 `json:"maximum,omitempty"`
60-
Minimum *float64 `json:"minimum,omitempty"`
61-
Description *string `json:"description,omitempty"`
62-
Blueprint *string `json:"blueprint,omitempty"`
63-
Pattern *string `json:"pattern,omitempty"`
64-
Enum []any `json:"enum,omitempty"`
65-
Spec *string `json:"spec,omitempty"`
66-
SpecAuthentication *SpecAuthentication `json:"specAuthentication,omitempty"`
67-
EnumColors map[string]string `json:"enumColors,omitempty"`
49+
Type string `json:"type,omitempty"`
50+
Title *string `json:"title,omitempty"`
51+
Identifier string `json:"identifier,omitempty"`
52+
Items map[string]any `json:"items,omitempty"`
53+
Default any `json:"default,omitempty"`
54+
Icon *string `json:"icon,omitempty"`
55+
Format *string `json:"format,omitempty"`
56+
MaxLength *int `json:"maxLength,omitempty"`
57+
MinLength *int `json:"minLength,omitempty"`
58+
MaxItems *int `json:"maxItems,omitempty"`
59+
MinItems *int `json:"minItems,omitempty"`
60+
Maximum *float64 `json:"maximum,omitempty"`
61+
Minimum *float64 `json:"minimum,omitempty"`
62+
Description *string `json:"description,omitempty"`
63+
Blueprint *string `json:"blueprint,omitempty"`
64+
Pattern *string `json:"pattern,omitempty"`
65+
Enum []any `json:"enum,omitempty"`
66+
Spec *string `json:"spec,omitempty"`
67+
SpecAuthentication *SpecAuthentication `json:"specAuthentication,omitempty"`
68+
EnumColors map[string]string `json:"enumColors,omitempty"`
69+
// UnknownFields captures any dynamic fields not explicitly defined above
70+
UnknownFields map[string]any `json:"-"`
6871
}
6972

7073
EntitiesSortModel struct {
@@ -486,6 +489,75 @@ type (
486489
}
487490
)
488491

492+
// Custom UnmarshalJSON for BlueprintProperty to capture dynamic fields
493+
func (bp *BlueprintProperty) UnmarshalJSON(data []byte) error {
494+
// Define an alias to avoid infinite recursion
495+
type Alias BlueprintProperty
496+
497+
// First, unmarshal into the alias to populate known fields
498+
aux := &struct {
499+
*Alias
500+
}{
501+
Alias: (*Alias)(bp),
502+
}
503+
504+
if err := json.Unmarshal(data, aux); err != nil {
505+
return err
506+
}
507+
508+
// Now unmarshal into a map to capture all fields
509+
var all map[string]interface{}
510+
if err := json.Unmarshal(data, &all); err != nil {
511+
return err
512+
}
513+
514+
bp.UnknownFields = make(map[string]interface{})
515+
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+
}
524+
525+
// Add any unknown fields to UnknownFields
526+
for key, value := range all {
527+
if !knownFields[key] {
528+
bp.UnknownFields[key] = value
529+
}
530+
}
531+
532+
return nil
533+
}
534+
535+
// Custom MarshalJSON for BlueprintProperty to include dynamic fields
536+
func (bp BlueprintProperty) MarshalJSON() ([]byte, error) {
537+
// Define an alias to avoid infinite recursion
538+
type Alias BlueprintProperty
539+
540+
// Marshal the known fields first
541+
aux := Alias(bp)
542+
aux.UnknownFields = nil // Don't marshal this field directly
543+
544+
data, err := json.Marshal(aux)
545+
if err != nil {
546+
return nil, err
547+
}
548+
549+
var result map[string]interface{}
550+
if err := json.Unmarshal(data, &result); err != nil {
551+
return nil, err
552+
}
553+
554+
for key, value := range bp.UnknownFields {
555+
result[key] = value
556+
}
557+
558+
return json.Marshal(result)
559+
}
560+
489561
type PortBody struct {
490562
OK bool `json:"ok"`
491563
Entity Entity `json:"entity"`

0 commit comments

Comments
 (0)