expandAnnotations turns annotations with dots in their key into nested maps. For example, annotations["foo.bar"] becomes annotations["foo"]["bar"].
(annotations map[string]any)
| 476 | // expandAnnotations turns annotations with dots in their key into nested maps. |
| 477 | // For example, annotations["foo.bar"] becomes annotations["foo"]["bar"]. |
| 478 | func expandAnnotations(annotations map[string]any) (map[string]any, error) { |
| 479 | if len(annotations) == 0 { |
| 480 | return nil, nil |
| 481 | } |
| 482 | res := make(map[string]any) |
| 483 | for k, v := range annotations { |
| 484 | parts := strings.Split(k, ".") |
| 485 | if len(parts) < 2 { |
| 486 | res[k] = v |
| 487 | continue |
| 488 | } |
| 489 | |
| 490 | m := res |
| 491 | for i := 0; i < len(parts)-1; i++ { |
| 492 | part := parts[i] |
| 493 | |
| 494 | // Check if a map already exists for this part; if yes, assign to m |
| 495 | x, ok := m[part] |
| 496 | if ok { |
| 497 | m, ok = x.(map[string]any) |
| 498 | if !ok { |
| 499 | return nil, fmt.Errorf("invalid annotation %q: nesting incompatible with other keys", k) |
| 500 | } |
| 501 | continue |
| 502 | } |
| 503 | |
| 504 | // Create a map for this part, then update m |
| 505 | tmp := make(map[string]any) |
| 506 | m[part] = tmp |
| 507 | m = tmp |
| 508 | } |
| 509 | |
| 510 | // Check the last part of this key isn't an intermediate part of a previously expanded key |
| 511 | k2 := parts[len(parts)-1] |
| 512 | if _, ok := m[k2]; ok { |
| 513 | return nil, fmt.Errorf("invalid annotation2 %q: nesting incompatible with other keys", k) |
| 514 | } |
| 515 | |
| 516 | // Assign the value to the last part |
| 517 | m[k2] = v |
| 518 | } |
| 519 | return res, nil |
| 520 | } |