splitLabels splits a labels string into a label map mapping names to values.
(labels string)
| 179 | |
| 180 | // splitLabels splits a labels string into a label map mapping names to values. |
| 181 | func splitLabels(labels string) (map[string]string, error) { |
| 182 | result := map[string]string{} |
| 183 | if len(labels) <= 1 { |
| 184 | return result, nil |
| 185 | } |
| 186 | components := strings.Split(labels[1:], "/") |
| 187 | if len(components)%2 != 0 { |
| 188 | return nil, fmt.Errorf("odd number of components in label string %q", labels) |
| 189 | } |
| 190 | |
| 191 | for i := 0; i < len(components)-1; i += 2 { |
| 192 | name, value := components[i], components[i+1] |
| 193 | trimmedName := strings.TrimSuffix(name, Base64Suffix) |
| 194 | unescapedName := model.UnescapeName(trimmedName, EscapingScheme) |
| 195 | if !ValidationScheme.IsValidLabelName(unescapedName) || |
| 196 | strings.HasPrefix(trimmedName, model.ReservedLabelPrefix) { |
| 197 | return nil, fmt.Errorf("improper label name %q", trimmedName) |
| 198 | } |
| 199 | if name == trimmedName { |
| 200 | result[unescapedName] = value |
| 201 | continue |
| 202 | } |
| 203 | decodedValue, err := decodeBase64(value) |
| 204 | if err != nil { |
| 205 | return nil, fmt.Errorf("invalid base64 encoding for label %s=%q: %v", trimmedName, value, err) |
| 206 | } |
| 207 | result[unescapedName] = decodedValue |
| 208 | } |
| 209 | return result, nil |
| 210 | } |
searching dependent graphs…