-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
886 lines (772 loc) · 21.4 KB
/
types.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
package parse
import (
"encoding/gob"
"encoding/json"
"fmt"
"math"
"net/url"
"path"
"reflect"
"time"
)
func init() {
gob.Register(&aclT{})
}
var registeredTypes = map[string]reflect.Type{}
// An interface for custom Parse types. Contains a single method:
//
// ClassName() - returns a string containing the class name as it appears in your
// Parse database.
//
// Implement this interface if your class name does not match your struct
// name. If this class is not implemented, the name of the struct will
// be used when interacting with the Parse API
type iClassName interface {
ClassName() string
}
// An interface for custom Parse types to override the endpoint used for querying.
//
// Contains a single method:
//
// Endpoint() - returns the endpoint to use when querying the Parse REST API.
//
// If this method is not implented, the endpoint is constructed as follows:
//
// /classes/{ClassName} - where {ClassName} is the name of the struct or the value returned by the ClassName
// method if implemented
type iParseEp interface {
Endpoint() string
}
// A base type containing fields common to all Parse types
//
// Embed this struct in custom types to avoid having to declare
// these fields everywhere.
type Base struct {
Id string `parse:"objectId"`
CreatedAt time.Time `parse:"-"`
UpdatedAt time.Time `parse:"-"`
ACL ACL `parse:"ACL,omitempty"`
Extra map[string]interface{} `parse:"-"`
}
type AnonymousAuthData struct {
Id string `json:"id"`
}
type TwitterAuthData struct {
Id string `json:"id"`
ScreenName string `json:"screen_name" parse:"screen_name"`
ConsumerKey string `json:"consumer_key" parse:"consumer_key"`
ConsumerSecret string `json:"consumer_secret" parse:"consumer_secret"`
AuthToken string `json:"auth_token" parse:"auth_token"`
AuthTokenSecret string `json:"auth_token_secret" parse:"auth_token_secret"`
}
type FacebookAuthData struct {
Id string
AccessToken string `parse:"access_token"`
ExpirationDate time.Time `parse:"expiration_date"`
}
func (a *FacebookAuthData) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Id string `json:"id"`
AccessToken string `json:"access_token" parse:"access_token"`
ExpirationDate string `json:"expiration_date"`
}{
a.Id, a.AccessToken, a.ExpirationDate.Format("2006-01-02T15:04:05.000Z"),
})
}
func (a *FacebookAuthData) UnmarshalJSON(b []byte) (err error) {
data := struct {
Id string `json:"id"`
AccessToken string `json:"access_token" parse:"access_token"`
ExpirationDate string `json:"expiration_date"`
}{}
if err := json.Unmarshal(b, &data); err != nil {
return err
}
a.Id = data.Id
a.AccessToken = data.AccessToken
a.ExpirationDate, err = time.Parse("2006-01-02T15:04:05.000Z", data.ExpirationDate)
return err
}
type AuthData struct {
Twitter *TwitterAuthData `json:"twitter,omitempty"`
Facebook *FacebookAuthData `json:"facebook,omitempty"`
Anonymous *AnonymousAuthData `json:"anonymous,omitempty"`
}
// Represents the built-in Parse "User" class. Embed this type in a custom
// type containing any custom fields. When fetching user objects, any retrieved
// fields with no matching struct field will be stored in User.Extra (map[string]interface{})
type User struct {
Base
Username string
Email string
EmailVerified bool `json:"-" parse:"-"`
}
func (u *User) ClassName() string {
return "_User"
}
func (u *User) Endpoint() string {
return "users"
}
// Represents the built-in Parse "Installation" class. Embed this type in a custom
// type containing any custom fields. When fetching user objects, any retrieved
// fields with no matching struct field will be stored in User.Extra (map[string]interface{})
type Installation struct {
Base
Badge int `parse:",omitempty"`
Channels []string `parse:",omitempty"`
TimeZone string
DeviceType string
PushType string `parse:",omitempty"`
GCMSenderId string `parse:",omitempty"`
InstallationId string
DeviceToken string `parse:",omitempty"`
ChannelUris []string `parse:",omitempty"`
AppName string
AppVersion string
ParseVersion string
AppIdentifier string
}
func (i *Installation) ClassName() string {
return "_Installation"
}
func (i *Installation) Endpoint() string {
return "installations"
}
type ACL interface {
// Returns whether public read access is enabled on this ACL
PublicReadAccess() bool
// Returns whether public write access is enabled on this ACL
PublicWriteAccess() bool
// Returns whether read access is enabled on this ACL for the
// given role
RoleReadAccess(role string) bool
// Returns whether write access is enabled on this ACL for the
// given role
RoleWriteAccess(role string) bool
// Returns whether read access is enabled on this ACL for the
// given user
ReadAccess(userId string) bool
// Returns whether write access is enabled on this ACL for the
// given user
WriteAccess(userId string) bool
// Allow the object to which this ACL is attached be read
// by anyone
SetPublicReadAccess(allowed bool) ACL
// Allow the object to which this ACL is attached to be
// updated by anyone
SetPublicWriteAccess(allowed bool) ACL
// Allow the object to which this ACL is attached to be
// read by the provided role
SetRoleReadAccess(role string, allowed bool) ACL
// Allow the object to which this ACL is attached to be
// updated by the provided role
SetRoleWriteAccess(role string, allowed bool) ACL
// Allow the object to which this ACL is attached to be
// read by the provided user
SetReadAccess(userId string, allowed bool) ACL
// Allow the object to which this ACL is attached to be
// updated by the provided user
SetWriteAccess(userId string, allowed bool) ACL
}
type aclT struct {
publicReadAccess bool
publicWriteAccess bool
write map[string]bool
read map[string]bool
}
func NewACL() ACL {
return &aclT{
write: map[string]bool{},
read: map[string]bool{},
}
}
func (a *aclT) PublicReadAccess() bool {
return a.publicReadAccess
}
func (a *aclT) PublicWriteAccess() bool {
return a.publicWriteAccess
}
func (a *aclT) RoleReadAccess(role string) bool {
if tmp, ok := a.read["role:"+role]; ok {
return tmp
}
return false
}
func (a *aclT) RoleWriteAccess(role string) bool {
if tmp, ok := a.write["role:"+role]; ok {
return tmp
}
return false
}
func (a *aclT) ReadAccess(userId string) bool {
if tmp, ok := a.read[userId]; ok {
return tmp
}
return false
}
func (a *aclT) WriteAccess(userId string) bool {
if tmp, ok := a.write[userId]; ok {
return tmp
}
return false
}
func (a *aclT) SetPublicReadAccess(allowed bool) ACL {
a.publicReadAccess = allowed
return a
}
func (a *aclT) SetPublicWriteAccess(allowed bool) ACL {
a.publicWriteAccess = allowed
return a
}
func (a *aclT) SetReadAccess(userId string, allowed bool) ACL {
a.read[userId] = allowed
return a
}
func (a *aclT) SetWriteAccess(userId string, allowed bool) ACL {
a.write[userId] = allowed
return a
}
func (a *aclT) SetRoleReadAccess(role string, allowed bool) ACL {
a.read["role:"+role] = allowed
return a
}
func (a *aclT) SetRoleWriteAccess(role string, allowed bool) ACL {
a.write["role:"+role] = allowed
return a
}
func (a *aclT) GobEncode() ([]byte, error) {
return json.Marshal(a)
}
func (a *aclT) GobDecode(b []byte) error {
return json.Unmarshal(b, &a)
}
func (a *aclT) MarshalJSON() ([]byte, error) {
m := map[string]map[string]bool{}
for k, v := range a.read {
if v {
m[k] = map[string]bool{
"read": v,
}
}
}
for k, v := range a.write {
if v {
if p, ok := m[k]; ok {
p["write"] = v
} else {
m[k] = map[string]bool{
"write": v,
}
}
}
}
if a.publicReadAccess {
m["*"] = map[string]bool{
"read": true,
}
}
if a.publicWriteAccess {
if p, ok := m["*"]; !ok {
m["*"] = map[string]bool{
"write": true,
}
} else {
p["write"] = true
}
}
return json.Marshal(m)
}
func (a *aclT) UnmarshalJSON(b []byte) error {
m := map[string]map[string]bool{}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
if a.read == nil {
a.read = map[string]bool{}
}
if a.write == nil {
a.write = map[string]bool{}
}
for k, v := range m {
if k == "*" {
if w, ok := v["write"]; w && ok {
a.publicWriteAccess = true
}
if r, ok := v["read"]; r && ok {
a.publicReadAccess = true
}
} else {
if w, ok := v["write"]; w && ok {
a.write[k] = true
}
if r, ok := v["read"]; r && ok {
a.read[k] = true
}
}
}
return nil
}
// Represents the Parse GeoPoint type
type GeoPoint struct {
Latitude float64
Longitude float64
}
func (g GeoPoint) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string `json:"__type"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}{
"GeoPoint",
g.Latitude,
g.Longitude,
})
}
func (g *GeoPoint) UnmarshalJSON(b []byte) error {
s := struct {
Type string `json:"__type"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}{}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
if s.Type != "GeoPoint" {
return fmt.Errorf("cannot unmarshal type %s to type GeoPoint", s.Type)
}
g.Latitude = s.Latitude
g.Longitude = s.Longitude
return nil
}
// Returns this distance from this GeoPoint to another in radians
func (g GeoPoint) RadiansTo(point GeoPoint) float64 {
d2r := math.Pi / 180.0
lat1Rad := g.Latitude * d2r
long1Rad := g.Longitude * d2r
lat2Rad := point.Latitude * d2r
long2Rad := point.Longitude * d2r
sinDeltaLatDiv2 := math.Sin((lat1Rad - lat2Rad) / 2)
sinDeltaLongDiv2 := math.Sin((long1Rad - long2Rad) / 2)
// Square of half the straight line chord distance between both points.
var a = sinDeltaLatDiv2*sinDeltaLatDiv2 + math.Cos(lat1Rad)*math.Cos(lat2Rad)*sinDeltaLongDiv2*sinDeltaLongDiv2
a = math.Min(1.0, a)
return 2 * math.Asin(math.Sqrt(a))
}
// Returns this distance from this GeoPoint to another in kilometers
func (g GeoPoint) KilometersTo(point GeoPoint) float64 {
return g.RadiansTo(point) * 6371.0
}
// Returns this distance from this GeoPoint to another in miles
func (g GeoPoint) MilesTo(point GeoPoint) float64 {
return g.RadiansTo(point) * 3958.8
}
// Represents the Parse File type
type File struct {
Name string `json:"name"`
Url string `json:"url"`
}
func (f *File) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Name string `json:"name"`
Url string `json:"url"`
Type string `json:"__type"`
}{
f.Name, f.Url, "File",
})
}
// Represents a Parse Pointer type. When querying, creating, or updating
// objects, any struct types will be automatically converted to and from Pointer
// types as required. Direct use of this type should not be necessary
type Pointer struct {
Id string
ClassName string
}
func (p Pointer) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string `json:"__type"`
ClassName string `json:"className"`
Id string `json:"objectId"`
}{
"Pointer",
p.ClassName,
p.Id,
})
}
// Represents the Parse Date type. Values of type time.Time will
// automatically converted to a Date type when constructing queries
// or creating objects. The inverse is true for retrieving objects.
// Direct use of this type should not be necessary
type Date time.Time
func (d Date) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string `json:"__type"`
Iso string `json:"iso"`
}{
"Date",
time.Time(d).In(time.UTC).Format("2006-01-02T15:04:05.000Z"),
})
}
func (d *Date) UnmarshalJSON(b []byte) error {
s := struct {
Type string `json:"__type"`
Iso string `json:"iso"`
}{}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
if s.Type != "Date" {
return fmt.Errorf("cannot unmarshal type %s to type Date", s.Type)
}
t, err := time.Parse("2006-01-02T15:04:05.000Z", s.Iso)
if err != nil {
return err
}
*d = Date(t)
return nil
}
func getClassName(v interface{}) string {
if tmp, ok := v.(iClassName); ok {
return tmp.ClassName()
} else {
t := reflect.TypeOf(v)
return t.Elem().Name()
}
}
func getEndpointBase(v interface{}) string {
var p string
var inst interface{}
rt := reflect.TypeOf(v)
rt = rt.Elem()
if rt.Kind() == reflect.Slice || rt.Kind() == reflect.Array {
rte := rt.Elem()
var rv reflect.Value
if rte.Kind() == reflect.Ptr {
rv = reflect.New(rte.Elem())
} else {
rv = reflect.New(rte)
}
inst = rv.Interface()
} else {
inst = v
}
if iv, ok := inst.(iParseEp); ok {
p = iv.Endpoint()
} else {
cname := getClassName(inst)
p = path.Join("classes", cname)
}
p = path.Join(defaultClient.config.Version, p)
return p
}
type Config map[string]interface{}
// Retrieves the value associated with the given key, and,
// if present, converts the value to a string and returns
// it. If the value is not present, or is not a string
// value, an empty string is returned
func (c Config) String(key string) string {
if v, ok := c[key]; ok {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
// Retrieves the value associated with the given key, and,
// if present, converts the value to a byte slice and returns
// it. If the value is not present, or is not a string
// value, an empty byte slice is returned
func (c Config) Bytes(key string) []byte {
if v, ok := c[key]; ok {
if s, ok := v.(string); ok {
return []byte(s)
}
}
return make([]byte, 0, 0)
}
// Retrieves the value associated with the given key, and,
// if present, converts the value to a bool and returns
// it. If the value is not present, or is not a bool
// value, false is returned
func (c Config) Bool(key string) bool {
if v, ok := c[key]; ok {
if b, ok := v.(bool); ok {
return b
}
}
return false
}
// Retrieves the value associated with the given key, and,
// if present, converts the value to an int and returns
// it. If the value is not present, or is not a numeric
// value, 0 is returned
func (c Config) Int(key string) int {
if v, ok := c[key]; ok {
// since we're unmarshaling into an interface{} value, all
// numbers will be float64 values
if f, ok := v.(float64); ok {
return int(f)
}
}
return 0
}
// Retrieves the value associated with the given key, and,
// if present, converts the value to an int64 and returns
// it. If the value is not present, or is not a numeric
// value, 0 is returned
func (c Config) Int64(key string) int64 {
if v, ok := c[key]; ok {
// since we're unmarshaling into an interface{} value, all
// numbers will be float64 values
if f, ok := v.(float64); ok {
return int64(f)
}
}
return 0
}
// Retrieves the value associated with the given key, and,
// if present, converts the value to an float64 and returns
// it. If the value is not present, or is not a numeric
// value, 0 is returned
func (c Config) Float(key string) float64 {
if v, ok := c[key]; ok {
// since we're unmarshaling into an interface{} value, all
// numbers will be float64 values
if f, ok := v.(float64); ok {
return f
}
}
return 0
}
// Retrieves the value associated with the given key, and,
// if present, converts the value to a slice of interface{}
// values and returns it. If the value is not present, or
// is not an array value, nil is returned
func (c Config) Values(key string) []interface{} {
if v, ok := c[key]; ok {
if s, ok := v.([]interface{}); ok {
return s
}
}
return nil
}
// Retrieves the value associated with the given key, and,
// if present, converts the value to a slice of string
// values and returns it. If the value is not present, or
// is not an array value, nil is returned
func (c Config) Strings(key string) []string {
if v, ok := c[key]; ok {
if is, ok := v.([]interface{}); ok {
ss := []string{}
for _, i := range is {
if s, ok := i.(string); ok {
ss = append(ss, s)
}
}
if len(ss) == len(is) {
return ss
}
}
}
return nil
}
// Retrieves the value associated with the given key, and,
// if present, converts the value to a slice of int values
// and returns it. If the value is not present, or is not
// an array value, nil is returned
func (c Config) Ints(key string) []int {
if v, ok := c[key]; ok {
if ifs, ok := v.([]interface{}); ok {
ints := []int{}
for _, i := range ifs {
if f, ok := i.(float64); ok {
ints = append(ints, int(f))
}
}
if len(ints) == len(ifs) {
return ints
}
}
}
return nil
}
// Retrieves the value associated with the given key, and,
// if present, converts the value to a slice of int64 values
// and returns it. If the value is not present, or is not
// an array value, nil is returned
func (c Config) Int64s(key string) []int64 {
if v, ok := c[key]; ok {
if ifs, ok := v.([]interface{}); ok {
ints := []int64{}
for _, i := range ifs {
if f, ok := i.(float64); ok {
ints = append(ints, int64(f))
}
}
if len(ints) == len(ifs) {
return ints
}
}
}
return nil
}
// Retrieves the value associated with the given key, and,
// if present, converts the value to a slice of float64 values
// and returns it. If the value is not present, or is not
// an array value, nil is returned
func (c Config) Floats(key string) []float64 {
if v, ok := c[key]; ok {
if is, ok := v.([]interface{}); ok {
fs := []float64{}
for _, i := range is {
if f, ok := i.(float64); ok {
fs = append(fs, f)
}
}
if len(fs) == len(is) {
return fs
}
}
}
return nil
}
// Retrieves the value associated with the given key, and, if present,
// converts value to a Config type (map[string]interface{}) and returns
// it. If the value is not present, or is not a JSON object, nil is
// returned
func (c Config) Map(key string) Config {
if v, ok := c[key]; ok {
if s, ok := v.(map[string]interface{}); ok {
return Config(s)
}
}
return nil
}
type configRequestT struct{}
func (c *configRequestT) method() string {
return "GET"
}
func (c *configRequestT) endpoint() (string, error) {
u := url.URL{}
u.Scheme = defaultClient.config.Schema
u.Host = defaultClient.config.Host
u.Path = path.Join(defaultClient.config.PathPrefix, defaultClient.config.Version, "config")
return u.String(), nil
}
func (c *configRequestT) body() (string, error) {
return "", nil
}
func (c *configRequestT) useMasterKey() bool {
return false
}
func (c *configRequestT) session() *sessionT {
return nil
}
func (c *configRequestT) contentType() string {
return ""
}
func GetConfig() (Config, error) {
b, err := defaultClient.doRequest(&configRequestT{})
if err != nil {
return nil, err
}
c := struct {
Params Config `json:"params"`
}{}
if err := json.Unmarshal(b, &c); err != nil {
return nil, err
}
return c.Params, nil
}
// Register a type so that it can be handled when populating struct values.
//
// The provided value will be registered under the name provided by the ClassName method
// if it is implemented, otherwise by the name of the type. When handling Parse responses,
// any object value with __type "Object" or "Pointer" and className matching the type provided
// will be unmarshaled into pointer to the provided type.
//
// This is useful in at least one instance: If you have an array or object field on a
// Parse class that contains pointers to or instances of Objects of arbitrary types
// that cannot be represented by a single type on your struct definition, but you would
// still like to be able to populate your struct with these values.
//
// In order to accomplish this, the field in question on your struct definition
// should either be of type interface{}, or another interface type that all possible
// types implement.
//
// Accepts a value t, representing the type to be registered. The value
// t should be either a struct value, or a pointer to a struct. Otherwise,
// an error will be returned.
func RegisterType(t interface{}) error {
rv := reflect.ValueOf(t)
rvi := reflect.Indirect(rv)
if rvi.Kind() != reflect.Struct {
return fmt.Errorf("expected struct or pointer to struct, got: %v", rv.Kind())
}
className := getClassName(t)
registeredTypes[className] = rvi.Type()
return nil
}
// Transform the given value into the proper representation for Marshaling as part
// of a request
//
// E.g. A struct is turned into a Pointer type, a time.Time is turned into a Date
func encodeForRequest(v interface{}) interface{} {
if v == nil {
return nil
}
rv := reflect.ValueOf(v)
rvi := reflect.Indirect(rv)
rt := rvi.Type()
if rt.Kind() == reflect.Struct {
switch v.(type) {
case time.Time, *time.Time, Date, *Date:
switch v.(type) {
case time.Time:
return Date(v.(time.Time))
case *time.Time:
return Date(*v.(*time.Time))
case Date, *Date:
return v
case string:
return map[string]interface{}{
"__type": "Date",
"iso": v.(string),
}
}
case Pointer, *Pointer:
return v
case GeoPoint, *GeoPoint:
return v
case ACL, *ACL:
return v
case AuthData, *AuthData:
b, _ := json.Marshal(v)
return string(b)
default:
var cname string
if tmp, ok := reflect.Zero(rvi.Type()).Interface().(iClassName); ok {
cname = tmp.ClassName()
} else if tmp, ok := reflect.New(rvi.Type()).Interface().(iClassName); ok {
cname = tmp.ClassName()
} else {
cname = rt.Name()
}
if idf := rvi.FieldByName("Id"); idf.IsValid() {
id := idf.Interface().(string)
return Pointer{
Id: id,
ClassName: cname,
}
}
}
} else if rt.Kind() == reflect.Slice {
vals := make([]interface{}, 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals = append(vals, encodeForRequest(rv.Index(i).Interface()))
}
return vals
}
return v
}