|
1 | 1 | package cli |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
| 6 | + "strings" |
4 | 7 | "time" |
5 | 8 |
|
6 | 9 | "github.com/hashicorp/terraform-plugin-framework/types" |
@@ -45,26 +48,28 @@ type ( |
45 | 48 | } |
46 | 49 |
|
47 | 50 | 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"` |
| 51 | + Type string `json:"type,omitempty"` |
| 52 | + Title *string `json:"title,omitempty"` |
| 53 | + Identifier string `json:"identifier,omitempty"` |
| 54 | + Items map[string]any `json:"items,omitempty"` |
| 55 | + Default any `json:"default,omitempty"` |
| 56 | + Icon *string `json:"icon,omitempty"` |
| 57 | + Format *string `json:"format,omitempty"` |
| 58 | + MaxLength *int `json:"maxLength,omitempty"` |
| 59 | + MinLength *int `json:"minLength,omitempty"` |
| 60 | + MaxItems *int `json:"maxItems,omitempty"` |
| 61 | + MinItems *int `json:"minItems,omitempty"` |
| 62 | + Maximum *float64 `json:"maximum,omitempty"` |
| 63 | + Minimum *float64 `json:"minimum,omitempty"` |
| 64 | + Description *string `json:"description,omitempty"` |
| 65 | + Blueprint *string `json:"blueprint,omitempty"` |
| 66 | + Pattern *string `json:"pattern,omitempty"` |
| 67 | + Enum []any `json:"enum,omitempty"` |
| 68 | + Spec *string `json:"spec,omitempty"` |
| 69 | + SpecAuthentication *SpecAuthentication `json:"specAuthentication,omitempty"` |
| 70 | + EnumColors map[string]string `json:"enumColors,omitempty"` |
| 71 | + // UnknownFields captures any dynamic fields not explicitly defined above |
| 72 | + UnknownFields map[string]any `json:"-"` |
68 | 73 | } |
69 | 74 |
|
70 | 75 | EntitiesSortModel struct { |
@@ -100,6 +105,12 @@ type ( |
100 | 105 | Sort *EntitiesSortModel `json:"sort,omitempty"` |
101 | 106 | } |
102 | 107 |
|
| 108 | + ActionTitle struct { |
| 109 | + Title string `json:"title"` |
| 110 | + Description *string `json:"description,omitempty"` |
| 111 | + Visible any `json:"visible,omitempty"` |
| 112 | + } |
| 113 | + |
103 | 114 | SpecAuthentication struct { |
104 | 115 | ClientId string `json:"clientId,omitempty"` |
105 | 116 | AuthorizationUrl string `json:"authorizationUrl,omitempty"` |
@@ -218,6 +229,7 @@ type ( |
218 | 229 | Required any `json:"required,omitempty"` |
219 | 230 | Order []string `json:"order,omitempty"` |
220 | 231 | Steps []Step `json:"steps,omitempty"` |
| 232 | + Titles map[string]ActionTitle `json:"titles,omitempty"` |
221 | 233 | } |
222 | 234 |
|
223 | 235 | TriggerEvent struct { |
@@ -486,6 +498,94 @@ type ( |
486 | 498 | } |
487 | 499 | ) |
488 | 500 |
|
| 501 | +// getKnownFields uses reflection to extract JSON field names from BlueprintProperty struct |
| 502 | +func getKnownFields(bp *BlueprintProperty) map[string]bool { |
| 503 | + knownFields := make(map[string]bool) |
| 504 | + t := reflect.TypeOf(*bp) |
| 505 | + |
| 506 | + for i := 0; i < t.NumField(); i++ { |
| 507 | + field := t.Field(i) |
| 508 | + |
| 509 | + // Get the JSON tag |
| 510 | + jsonTag := field.Tag.Get("json") |
| 511 | + if jsonTag == "" || jsonTag == "-" { |
| 512 | + continue // Skip fields without JSON tags or with "-" |
| 513 | + } |
| 514 | + |
| 515 | + // Handle "fieldname,omitempty" format |
| 516 | + fieldName, _, _ := strings.Cut(jsonTag, ",") |
| 517 | + if fieldName != "" { |
| 518 | + knownFields[fieldName] = true |
| 519 | + } |
| 520 | + } |
| 521 | + |
| 522 | + return knownFields |
| 523 | +} |
| 524 | + |
| 525 | +// Custom UnmarshalJSON for BlueprintProperty to capture dynamic fields |
| 526 | +func (bp *BlueprintProperty) UnmarshalJSON(data []byte) error { |
| 527 | + // Define an alias to avoid infinite recursion |
| 528 | + type Alias BlueprintProperty |
| 529 | + |
| 530 | + // First, unmarshal into the alias to populate known fields |
| 531 | + aux := &struct { |
| 532 | + *Alias |
| 533 | + }{ |
| 534 | + Alias: (*Alias)(bp), |
| 535 | + } |
| 536 | + |
| 537 | + if err := json.Unmarshal(data, aux); err != nil { |
| 538 | + return err |
| 539 | + } |
| 540 | + |
| 541 | + // Now unmarshal into a map to capture all fields |
| 542 | + var all map[string]any |
| 543 | + if err := json.Unmarshal(data, &all); err != nil { |
| 544 | + return err |
| 545 | + } |
| 546 | + |
| 547 | + // Initialize UnknownFields map |
| 548 | + bp.UnknownFields = make(map[string]any) |
| 549 | + |
| 550 | + // Use reflection to get known fields instead of hardcoding |
| 551 | + knownFields := getKnownFields(bp) |
| 552 | + |
| 553 | + // Add any unknown fields to UnknownFields |
| 554 | + for key, value := range all { |
| 555 | + if !knownFields[key] { |
| 556 | + bp.UnknownFields[key] = value |
| 557 | + } |
| 558 | + } |
| 559 | + |
| 560 | + return nil |
| 561 | +} |
| 562 | + |
| 563 | +// Custom MarshalJSON for BlueprintProperty to include dynamic fields |
| 564 | +func (bp BlueprintProperty) MarshalJSON() ([]byte, error) { |
| 565 | + // Define an alias to avoid infinite recursion |
| 566 | + type Alias BlueprintProperty |
| 567 | + |
| 568 | + // Marshal the known fields first |
| 569 | + aux := Alias(bp) |
| 570 | + aux.UnknownFields = nil // Don't marshal this field directly |
| 571 | + |
| 572 | + data, err := json.Marshal(aux) |
| 573 | + if err != nil { |
| 574 | + return nil, err |
| 575 | + } |
| 576 | + |
| 577 | + var result map[string]any |
| 578 | + if err := json.Unmarshal(data, &result); err != nil { |
| 579 | + return nil, err |
| 580 | + } |
| 581 | + |
| 582 | + for key, value := range bp.UnknownFields { |
| 583 | + result[key] = value |
| 584 | + } |
| 585 | + |
| 586 | + return json.Marshal(result) |
| 587 | +} |
| 588 | + |
489 | 589 | type PortBody struct { |
490 | 590 | OK bool `json:"ok"` |
491 | 591 | Entity Entity `json:"entity"` |
|
0 commit comments