Add inserts ep under name. Returns ErrAlreadyExists if name is already present, so callers can give a user-friendly "already exists" message without string-matching the wrapped error.
(file *File, name string, ep Endpoint)
| 48 | // present, so callers can give a user-friendly "already exists" message |
| 49 | // without string-matching the wrapped error. |
| 50 | func Add(file *File, name string, ep Endpoint) error { |
| 51 | if file == nil { |
| 52 | return errors.New("providers: nil file") |
| 53 | } |
| 54 | if err := validateName(name); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | if file.Endpoints == nil { |
| 58 | file.Endpoints = map[string]Endpoint{} |
| 59 | } |
| 60 | if _, ok := file.Endpoints[name]; ok { |
| 61 | return fmt.Errorf("provider %q already exists: %w", name, ErrAlreadyExists) |
| 62 | } |
| 63 | file.Endpoints[name] = ep |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | // Update applies the sparse patch to the endpoint named name. Returns |
| 68 | // ErrNotFound if no such endpoint exists. |