ParseUUIDList parses a JSON-encoded array of UUID strings (e.g. `["uuid1","uuid2"]`) and returns the corresponding slice of uuid.UUID values. An empty input (including whitespace-only) returns an empty (non-nil) slice.
(raw string)
| 13 | // slice of uuid.UUID values. An empty input (including |
| 14 | // whitespace-only) returns an empty (non-nil) slice. |
| 15 | func ParseUUIDList(raw string) ([]uuid.UUID, error) { |
| 16 | raw = strings.TrimSpace(raw) |
| 17 | if raw == "" { |
| 18 | return []uuid.UUID{}, nil |
| 19 | } |
| 20 | |
| 21 | var strs []string |
| 22 | if err := json.Unmarshal([]byte(raw), &strs); err != nil { |
| 23 | return nil, xerrors.Errorf("unmarshal uuid list: %w", err) |
| 24 | } |
| 25 | |
| 26 | ids := make([]uuid.UUID, 0, len(strs)) |
| 27 | for _, s := range strs { |
| 28 | id, err := uuid.Parse(s) |
| 29 | if err != nil { |
| 30 | return nil, xerrors.Errorf("parse uuid %q: %w", s, err) |
| 31 | } |
| 32 | ids = append(ids, id) |
| 33 | } |
| 34 | return ids, nil |
| 35 | } |