forked from kylemcc/parse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
70 lines (58 loc) · 1.63 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package parse
import (
"fmt"
"path"
"reflect"
)
// Delete the instance of the type represented by v from the Parse database. If
// useMasteKey=true, the Master Key will be used for the deletion request.
func (c *Client) Delete(v interface{}, useMasterKey bool) error {
return c._delete(v, useMasterKey, "")
}
func (c *Client) _delete(v interface{}, useMasterKey bool, sessionToken string) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return fmt.Errorf("parse: expected a non-nil pointer got %v", rv.Kind())
}
_, err := c.doRequest(&deleteRequest{
inst: v,
shouldUseMasterKey: useMasterKey,
st: sessionToken,
})
return err
}
type deleteRequest struct {
inst interface{}
shouldUseMasterKey bool
st string
}
func (d *deleteRequest) method() string {
return "DELETE"
}
func (d *deleteRequest) endpoint() (string, error) {
var id string
rv := reflect.ValueOf(d.inst)
rvi := reflect.Indirect(rv)
if f := rvi.FieldByName("Id"); f.IsValid() {
if s, ok := f.Interface().(string); ok {
id = s
} else {
return "", fmt.Errorf("parse: Id field should be a string, received type %s", f.Type())
}
} else {
return "", fmt.Errorf("parse: can not delete value - type has no Id field")
}
return path.Join(getEndpointBase(d.inst), id), nil
}
func (d *deleteRequest) body() (string, error) {
return "", nil
}
func (d *deleteRequest) useMasterKey() bool {
return d.shouldUseMasterKey
}
func (d *deleteRequest) sessionToken() string {
return d.st
}
func (d *deleteRequest) contentType() string {
return "application/x-www-form-urlencoded"
}