crud.skel (1745B)
1 // TYPEs returns a slice of TYPE matching the filter expression filter. 2 // If no PLURAL match, error wraps ErrNoMatch. 3 // To fetch all LOWER, set filter to the empty string (""). 4 func (c *Client) TYPEs(filter string) ([]TYPE, error) { 5 objects, err := c.filterObjects("/objects/PLURAL", filter) 6 if err != nil { 7 return nil, fmt.Errorf("get PLURAL filter %s: %w", filter, err) 8 } 9 var PLURAL []TYPE 10 for _, o := range objects { 11 v, ok := o.(TYPE) 12 if !ok { 13 return nil, fmt.Errorf("get PLURAL filter %s: %T in response", filter, v) 14 } 15 PLURAL = append(PLURAL, v) 16 } 17 return PLURAL, nil 18 } 19 20 // LookupTYPE returns the TYPE identified by name. If no TYPE is found, error 21 // wraps ErrNotExist. 22 func (c *Client) LookupTYPE(name string) (TYPE, error) { 23 obj, err := c.lookupObject("/objects/PLURAL/" + url.PathEscape(name)) 24 if err != nil { 25 return TYPE{}, fmt.Errorf("lookup LOWER %s: %w", name, err) 26 } 27 v, ok := obj.(TYPE) 28 if !ok { 29 return TYPE{}, fmt.Errorf("lookup LOWER %s: result type %T is not TYPE", name, v) 30 } 31 return v, nil 32 } 33 34 // CreateTYPE creates LOWER. Some fields of LOWER must be set for successful 35 // creation; see the type definition of TYPE for details. 36 func (c *Client) CreateTYPE(LOWER TYPE) error { 37 if err := c.createObject(LOWER); err != nil { 38 return fmt.Errorf("create LOWER %s: %w", LOWER.Name, err) 39 } 40 return nil 41 } 42 43 // DeleteTYPE deletes the TYPE identified by name. If cascade is true, objects 44 // depending on the TYPE are also deleted. If no TYPE is found, error wraps 45 // ErrNotExist. 46 func (c *Client) DeleteTYPE(name string, cascade bool) error { 47 if err := c.deleteObject("/objects/PLURAL/"+url.PathEscape(name), cascade); err != nil { 48 return fmt.Errorf("delete LOWER %s: %w", name, err) 49 } 50 return nil 51 }