-
Notifications
You must be signed in to change notification settings - Fork 15
/
create.go
135 lines (113 loc) · 2.82 KB
/
create.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package parse
import (
"encoding/json"
"errors"
"net/url"
"reflect"
)
type createT struct {
v interface{}
shouldUseMasterKey bool
currentSession *sessionT
isUser bool
username string
password string
}
func (c *createT) method() string {
return "POST"
}
func (c *createT) endpoint() (string, error) {
p := getEndpointBase(c.v)
u := url.URL{}
u.Scheme = ParseScheme
u.Host = parseHost
u.Path = p
return u.String(), nil
}
func (c *createT) body() (string, error) {
payload := map[string]interface{}{}
if c.isUser {
payload["username"] = c.username
payload["password"] = c.password
}
rv := reflect.ValueOf(c.v)
rvi := reflect.Indirect(rv)
rt := rvi.Type()
fields := getFields(rt)
for _, f := range fields {
var name string
var fv reflect.Value
if n, o := parseTag(f.Tag.Get("parse")); n == "-" || n == "objectId" || f.Name == "Id" || f.Type == reflect.TypeOf(Base{}) {
continue
} else if fv = rvi.FieldByName(f.Name); !fv.IsValid() || o == "omitempty" && isEmptyValue(fv) {
continue
} else {
name = n
}
var fname string
if name != "" {
fname = name
} else {
fname = firstToLower(f.Name)
}
if canBeNil(fv) && fv.IsNil() {
payload[fname] = nil
} else {
payload[fname] = encodeForRequest(fv.Interface())
}
}
b, err := json.Marshal(payload)
if err != nil {
return "", err
}
return string(b), nil
}
func (c *createT) useMasterKey() bool {
return c.shouldUseMasterKey
}
func (c *createT) session() *sessionT {
return c.currentSession
}
func (c *createT) contentType() string {
return "application/json"
}
// Save a new instance of the type pointed to by v to the Parse database. If
// useMasteKey=true, the Master Key will be used for the creation request. On a
// successful request, the CreatedAt field will be set on v.
//
// Note: v should be a pointer to a struct whose name represents a Parse class,
// or that implements the ClassName method
func Create(v interface{}, useMasterKey bool) error {
return create(v, useMasterKey, nil)
}
func Signup(username string, password string, user interface{}) error {
cr := &createT{
v: user,
shouldUseMasterKey: false,
currentSession: nil,
isUser: true,
username: username,
password: password,
}
if b, err := defaultClient.doRequest(cr); err != nil {
return err
} else {
return handleResponse(b, user)
}
}
func create(v interface{}, useMasterKey bool, currentSession *sessionT) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("v must be a non-nil pointer")
}
cr := &createT{
v: v,
shouldUseMasterKey: useMasterKey,
currentSession: currentSession,
}
if b, err := defaultClient.doRequest(cr); err != nil {
return err
} else {
return handleResponse(b, v)
}
}