Extension2Variables converts a runtime.RawExtension to a map[string]any variables structure. It first tries to unmarshal the extension as a map[string]any. If that fails, it tries to unmarshal as a []any (array). If that also fails, it tries to unmarshal as a string (useful for template strings enco
(ext runtime.RawExtension)
| 420 | // If that also fails, it tries to unmarshal as a string (useful for template strings encoded as JSON). |
| 421 | // If all attempts fail, it logs an error and returns the raw bytes as a string under the "raw" key. |
| 422 | func Extension2Variables(ext runtime.RawExtension) map[string]any { |
| 423 | if len(ext.Raw) == 0 { |
| 424 | return make(map[string]any) |
| 425 | } |
| 426 | |
| 427 | // Try to unmarshal as map[string]any |
| 428 | var data map[string]any |
| 429 | if err := json.Unmarshal(ext.Raw, &data); err == nil { |
| 430 | return data |
| 431 | } |
| 432 | |
| 433 | // If failed, try to unmarshal as []any (array) |
| 434 | var arr []any |
| 435 | if err := json.Unmarshal(ext.Raw, &arr); err == nil { |
| 436 | return map[string]any{ |
| 437 | "raw": arr, |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | // If still failed, try to unmarshal as string (e.g., a marshaled template string) |
| 442 | var str string |
| 443 | if err2 := json.Unmarshal(ext.Raw, &str); err2 == nil { |
| 444 | return map[string]any{ |
| 445 | "raw": str, |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | // If still failed, log error and return raw bytes as string |
| 450 | klog.V(4).ErrorS(nil, "failed to unmarshal extension to variables", "raw", string(ext.Raw)) |
| 451 | return map[string]any{ |
| 452 | "raw": string(ext.Raw), |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | // Extension2String converts a runtime.RawExtension to a string, optionally parsing it as a template. |
| 457 | // If the extension is empty, it returns nil. If the string is quoted, it unquotes it first. |
no outgoing calls