ResolveLabels matches user-provided label names (case-insensitive) against a set of known labels and returns the corresponding IDs. If any names cannot be matched, all unrecognized names are reported in the returned error.
(allLabels []client.DiscussionLabel, names []string)
| 11 | // set of known labels and returns the corresponding IDs. If any names cannot be |
| 12 | // matched, all unrecognized names are reported in the returned error. |
| 13 | func ResolveLabels(allLabels []client.DiscussionLabel, names []string) ([]string, error) { |
| 14 | byName := make(map[string]string, len(allLabels)) |
| 15 | for _, l := range allLabels { |
| 16 | byName[strings.ToLower(l.Name)] = l.ID |
| 17 | } |
| 18 | |
| 19 | var ids []string |
| 20 | var missing []string |
| 21 | |
| 22 | for _, name := range names { |
| 23 | trimmed := strings.TrimSpace(name) |
| 24 | id, ok := byName[strings.ToLower(trimmed)] |
| 25 | if !ok { |
| 26 | missing = append(missing, trimmed) |
| 27 | } else { |
| 28 | ids = append(ids, id) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | if len(missing) > 0 { |
| 33 | return nil, fmt.Errorf("labels not found: %s", strings.Join(missing, ", ")) |
| 34 | } |
| 35 | |
| 36 | return ids, nil |
| 37 | } |