extractNameFromMetadata attempts to extract the name from metadata.name.
(content []byte)
| 103 | |
| 104 | // extractNameFromMetadata attempts to extract the name from metadata.name. |
| 105 | func extractNameFromMetadata(content []byte) (string, error) { |
| 106 | if len(content) == 0 { |
| 107 | return "", nil |
| 108 | } |
| 109 | |
| 110 | // Identify the format |
| 111 | format, err := unmarshal.IdentifyFormat(content) |
| 112 | if err != nil { |
| 113 | return "", err |
| 114 | } |
| 115 | |
| 116 | // Convert to JSON for consistent unmarshaling |
| 117 | var jsonData []byte |
| 118 | switch format { |
| 119 | case unmarshal.RawFormatJSON: |
| 120 | jsonData = content |
| 121 | case unmarshal.RawFormatYAML: |
| 122 | jsonData, err = unmarshal.LoadJSONBytes(content, ".yaml") |
| 123 | if err != nil { |
| 124 | return "", err |
| 125 | } |
| 126 | default: |
| 127 | return "", fmt.Errorf("unsupported format: %s", format) |
| 128 | } |
| 129 | |
| 130 | // Unmarshal just the metadata field |
| 131 | var schema metadataWithName |
| 132 | if err := json.Unmarshal(jsonData, &schema); err != nil { |
| 133 | // Not a v2 schema or invalid format |
| 134 | return "", nil |
| 135 | } |
| 136 | |
| 137 | return schema.Metadata.Name, nil |
| 138 | } |
no test coverage detected