forked from forcedotcom/go-soql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshaller.go
1192 lines (1096 loc) · 41.5 KB
/
marshaller.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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package soql
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
const (
openBrace = "("
closeBrace = ")"
orCondition = " OR "
andCondition = " AND "
singleQuote = "'"
safeSingleQuote = "\\'"
doubleQuote = "\""
safeDoubleQuote = "\\\""
backslash = "\\"
safeBackslash = "\\\\"
newLine = "\n"
safeNewLine = "\\n"
carriageReturn = "\r"
safeCarriageReturn = "\\r"
tab = "\t"
safeTab = "\\t"
bell = "\b"
safeBell = "\\b"
formFeed = "\f"
safeFormFeed = "\\f"
underscore = "_"
safeUnderscore = "\\_"
percentSign = "%"
safePercentSign = "\\%"
comma = ","
notOperator = "NOT "
openLike = " LIKE '%"
closeLike = "%'"
inOperator = " IN "
notInOperator = " NOT IN "
equalsOperator = " = "
period = "."
null = "null"
notEqualsOperator = " != "
greaterThanOperator = " > "
greaterThanOrEqualsToOperator = " >= "
lessThanOperator = " < "
lessThanOrEqualsToOperator = " <= "
greaterNextNDaysOperator = " > NEXT_N_DAYS:"
greaterOrEqualNextNDaysOperator = " >= NEXT_N_DAYS:"
equalsNextNDaysOperator = " = NEXT_N_DAYS:"
lessNextNDaysOperator = " < NEXT_N_DAYS:"
lessOrEqualNextNDaysOperator = " <= NEXT_N_DAYS:"
greaterLastNDaysOperator = " > LAST_N_DAYS:"
greaterOrEqualLastNDaysOperator = " >= LAST_N_DAYS:"
equalsLastNDaysOperator = " = LAST_N_DAYS:"
lessLastNDaysOperator = " < LAST_N_DAYS:"
lessOrEqualLastNDaysOperator = " <= LAST_N_DAYS:"
selectKeyword = "SELECT "
whereKeyword = " WHERE "
fromKeyword = " FROM "
orderByKeyword = " ORDER BY "
limitKeyword = " LIMIT "
offsetKeyword = " OFFSET "
ascKeyword = " ASC"
descKeyword = " DESC"
// DateTimeFormat is the golang reference time in the soql dateTime fields format
DateTimeFormat = "2006-01-02T15:04:05.000-0700"
// SoqlTag is the main tag name to be used to mark a struct field to be considered for soql marshaling
SoqlTag = "soql"
// SelectClause is the tag to be used when marking the struct to be considered for select clause
SelectClause = "selectClause"
// TableName is the parameter to be used to specify the name of the underlying SOQL table. It should be used
// with SelectClause
TableName = "tableName"
// SelectColumn is the tag to be used for selecting a column in select clause
SelectColumn = "selectColumn"
// SelectChild is the tag to be used when selecting from child tables
SelectChild = "selectChild"
// FieldName is the parameter to be used to specify the name of the field in underlying SOQL object
FieldName = "fieldName"
// WhereClause is the tag to be used when marking the struct to be considered for where clause
WhereClause = "whereClause"
// Joiner is the parameter to be used to specify the joiner to use between properties within a where clause
Joiner = "joiner"
// Format is the parameter to be used to specify string formatting, it is only valid for time.Time fields
Format = "format"
// OrderByClause is the tag to be used when marking the string slice to be considered for order by clause
OrderByClause = "orderByClause"
// LimitClause is the tag to be used when marking the int to be considered for limit clause
LimitClause = "limitClause"
// OffsetClause is the tag to be used when marking the int to be considered for offset clause
OffsetClause = "offsetClause"
// LikeOperator is the tag to be used for "like" operator in where clause
LikeOperator = "likeOperator"
// NotLikeOperator is the tag to be used for "not like" operator in where clause
NotLikeOperator = "notLikeOperator"
// InOperator is the tag to be used for "in" operator in where clause
InOperator = "inOperator"
// NotInOperator is the tag to be used for "not in" operator in where clause
NotInOperator = "notInOperator"
// EqualsOperator is the tag to be used for "=" operator in where clause
EqualsOperator = "equalsOperator"
// NotEqualsOperator is the tag to be used for "!=" operator in where clause
NotEqualsOperator = "notEqualsOperator"
// NullOperator is the tag to be used for " = null " or "!= null" operator in where clause
NullOperator = "nullOperator"
// GreaterThanOperator is the tag to be used for ">" operator in where clause
GreaterThanOperator = "greaterThanOperator"
// GreaterThanOrEqualsToOperator is the tag to be used for ">=" operator in where clause
GreaterThanOrEqualsToOperator = "greaterThanOrEqualsToOperator"
// LessThanOperator is the tag to be used for "<" operator in where clause
LessThanOperator = "lessThanOperator"
// LessThanOrEqualsToOperator is the tag to be used for "<=" operator in where clause
LessThanOrEqualsToOperator = "lessThanOrEqualsToOperator"
// GreaterNextNDaysOperator is the tag to be used for "> NEXT_N_DAYS:n" operator in where clause
GreaterNextNDaysOperator = "greaterNextNDaysOperator"
// GreaterOrEqualNextNDaysOperator is the tag to be used for ">= NEXT_N_DAYS:n" operator in where clause
GreaterOrEqualNextNDaysOperator = "greaterOrEqualNextNDaysOperator"
// EqualsNextNDaysOperator is the tag to be used for "= NEXT_N_DAYS:n" operator in where clause
EqualsNextNDaysOperator = "equalsNextNDaysOperator"
// LessNextNDaysOperator is the tag to be used for "< NEXT_N_DAYS:n" operator in where clause
LessNextNDaysOperator = "lessNextNDaysOperator"
// LessOrEqualNextNDaysOperator is the tag to be used for "<= NEXT_N_DAYS:n" operator in where clause
LessOrEqualNextNDaysOperator = "lessOrEqualNextNDaysOperator"
// GreaterLastNDaysOperator is the tag to be used for "> LAST_N_DAYS:n" operator in where clause
GreaterLastNDaysOperator = "greaterLastNDaysOperator"
// GreaterOrEqualLastNDaysOperator is the tag to be used for ">= LAST_N_DAYS:n" operator in where clause
GreaterOrEqualLastNDaysOperator = "greaterOrEqualLastNDaysOperator"
// EqualsLastNDaysOperator is the tag to be used for "= LAST_N_DAYS:n" operator in where clause
EqualsLastNDaysOperator = "equalsLastNDaysOperator"
// LessLastNDaysOperator is the tag to be used for "< LAST_N_DAYS:n" operator in where clause
LessLastNDaysOperator = "lessLastNDaysOperator"
// LessOrEqualLastNDaysOperator is the tag to be used for "<= LAST_N_DAYS:n" operator in where clause
LessOrEqualLastNDaysOperator = "lessOrEqualLastNDaysOperator"
// Subquery is the tag to be used for a subquery in a where clause
Subquery = "subquery"
)
var clauseBuilderMap = map[string]func(v interface{}, fieldName string, tags map[string]string) (string, error){
LikeOperator: buildLikeClause,
NotLikeOperator: buildNotLikeClause,
InOperator: buildInClause,
NotInOperator: buildNotInClause,
EqualsOperator: buildEqualsClause,
NullOperator: buildNullClause,
NotEqualsOperator: buildNotEqualsClause,
GreaterThanOperator: buildGreaterThanClause,
GreaterThanOrEqualsToOperator: buildGreaterThanOrEqualsToClause,
LessThanOperator: buildLessThanClause,
LessThanOrEqualsToOperator: buildLessThanOrEqualsToClause,
GreaterNextNDaysOperator: buildGreaterNextNDaysOperator,
GreaterOrEqualNextNDaysOperator: buildGreaterOrEqualNextNDaysOperator,
EqualsNextNDaysOperator: buildEqualsNextNDaysOperator,
LessNextNDaysOperator: buildLessNextNDaysOperator,
LessOrEqualNextNDaysOperator: buildLessOrEqualNextNDaysOperator,
GreaterLastNDaysOperator: buildGreaterLastNDaysOperator,
GreaterOrEqualLastNDaysOperator: buildGreaterOrEqualLastNDaysOperator,
EqualsLastNDaysOperator: buildEqualsLastNDaysOperator,
LessLastNDaysOperator: buildLessLastNDaysOperator,
LessOrEqualLastNDaysOperator: buildLessOrEqualLastNDaysOperator,
}
var (
// ErrInvalidTag error is returned when invalid key is used in soql tag
ErrInvalidTag = errors.New("ErrInvalidTag")
// ErrNilValue error is returned when nil pointer is passed as argument
ErrNilValue = errors.New("ErrNilValue")
// ErrMultipleSelectClause error is returned when there are multiple selectClause in struct
ErrMultipleSelectClause = errors.New("ErrMultipleSelectClause")
// ErrNoSelectClause error is returned when there are No selectClause in struct
ErrNoSelectClause = errors.New("ErrNoSelectClause")
// ErrMultipleWhereClause error is returned when there are multiple whereClause in struct
ErrMultipleWhereClause = errors.New("ErrMultipleWhereClause")
// ErrInvalidOrderByClause error is returned when field with orderByClause tag is invalid
ErrInvalidOrderByClause = errors.New("ErrInvalidOrderByClause")
// ErrMultipleOrderByClause error is returned when there are multiple orderByClause in struct
ErrMultipleOrderByClause = errors.New("ErrMultipleOrderByClause")
// ErrInvalidSelectColumnOrderByClause error is returned when the selectColumn
// associated with the order by clause is invalid
ErrInvalidSelectColumnOrderByClause = errors.New("ErrInvalidSelectColumnOrderByClause")
// ErrInvalidLimitClause error is returned when field with limitClause tag is invalid
ErrInvalidLimitClause = errors.New("ErrInvalidLimitClause")
// ErrMultipleLimitClause error is returned when there are multiple limitClause in struct
ErrMultipleLimitClause = errors.New("ErrMultipleLimitClause")
// ErrInvalidOffsetClause error is returned when field with offsetClause tag is invalid
ErrInvalidOffsetClause = errors.New("ErrInvalidOffsetClause")
// ErrMultipleOffsetClause error is returned when there are multiple offsetClause in struct
ErrMultipleOffsetClause = errors.New("ErrMultipleOffsetClause")
)
// Order is the struct for defining the order by clause on a per column basis
// A slice of this struct tagged with the orderByClause tag in a soql struct
// specifies the columns from the selectClause struct to be included in the
// order by clause and their sort order
type Order struct {
// Field contains the name of the field of the selectClause struct to be
// included in the order by clause
Field string
// IsDesc indicates whether the ordering is DESC (true) or ASC (false)
IsDesc bool
}
// https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_quotedstringescapes.htm
var sanitizeCharacters = []string{
singleQuote, safeSingleQuote,
doubleQuote, safeDoubleQuote,
backslash, safeBackslash,
newLine, safeNewLine,
carriageReturn, safeCarriageReturn,
tab, safeTab,
bell, safeBell,
formFeed, safeFormFeed,
}
var sanitizeReplacer = strings.NewReplacer(sanitizeCharacters...)
var sanitizeLikeCharacters = append(
sanitizeCharacters,
underscore, safeUnderscore,
percentSign, safePercentSign,
)
var sanitizeLikeReplacer = strings.NewReplacer(sanitizeLikeCharacters...)
func buildLikeClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructLikeClause(v, fieldName, false)
}
func buildNotLikeClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructLikeClause(v, fieldName, true)
}
func constructLikeClause(v interface{}, fieldName string, exclude bool) (string, error) {
var buff strings.Builder
patterns, ok := v.([]string)
if !ok {
return buff.String(), ErrInvalidTag
}
if len(patterns) > 1 {
buff.WriteString(openBrace)
}
for indx, pattern := range patterns {
if indx > 0 {
if exclude {
buff.WriteString(andCondition)
} else {
buff.WriteString(orCondition)
}
}
if exclude {
buff.WriteString(openBrace)
buff.WriteString(notOperator)
}
buff.WriteString(fieldName)
buff.WriteString(openLike)
buff.WriteString(sanitizeLikeReplacer.Replace(pattern))
buff.WriteString(closeLike)
if exclude {
buff.WriteString(closeBrace)
}
}
if len(patterns) > 1 {
buff.WriteString(closeBrace)
}
return buff.String(), nil
}
func buildInClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructContainsClause(v, fieldName, inOperator, tags)
}
func buildNotInClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructContainsClause(v, fieldName, notInOperator, tags)
}
func constructContainsClause(v interface{}, fieldName string, operator string, tags map[string]string) (string, error) {
var buff strings.Builder
var items []string
useSingleQuotes := false
switch u := v.(type) {
case []string:
useSingleQuotes = true
items = u
case []int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16, []uint32, []uint64, []float32, []float64, []bool:
items = strings.Fields(strings.Trim(fmt.Sprint(u), "[]"))
case []time.Time:
for _, item := range u {
items = append(items, item.Format(getDateFormat(tags)))
}
default:
return buff.String(), ErrInvalidTag
}
if len(items) > 0 {
buff.WriteString(fieldName)
buff.WriteString(operator)
buff.WriteString(openBrace)
}
for indx, item := range items {
if indx > 0 {
buff.WriteString(comma)
}
if useSingleQuotes {
buff.WriteString(singleQuote)
buff.WriteString(sanitizeReplacer.Replace(item))
buff.WriteString(singleQuote)
} else {
buff.WriteString(item)
}
}
if len(items) > 0 {
buff.WriteString(closeBrace)
}
return buff.String(), nil
}
func buildNotEqualsClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructComparisonClause(v, fieldName, notEqualsOperator, tags)
}
func buildEqualsClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructComparisonClause(v, fieldName, equalsOperator, tags)
}
func buildGreaterThanClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructComparisonClause(v, fieldName, greaterThanOperator, tags)
}
func buildGreaterThanOrEqualsToClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructComparisonClause(v, fieldName, greaterThanOrEqualsToOperator, tags)
}
func buildLessThanClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructComparisonClause(v, fieldName, lessThanOperator, tags)
}
func buildLessThanOrEqualsToClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructComparisonClause(v, fieldName, lessThanOrEqualsToOperator, tags)
}
func constructComparisonClause(v interface{}, fieldName, operator string, tags map[string]string) (string, error) {
var buff strings.Builder
var value string
useSingleQuotes := false
switch u := v.(type) {
case string:
useSingleQuotes = true
value = u
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool:
value = fmt.Sprint(u)
case time.Time:
value = u.Format(getDateFormat(tags))
case *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float32, *float64, *bool:
if !reflect.ValueOf(u).IsNil() {
value = fmt.Sprint(reflect.Indirect(reflect.ValueOf(u)))
}
case *time.Time:
if !reflect.ValueOf(u).IsNil() {
value = reflect.Indirect(reflect.ValueOf(u)).Interface().(time.Time).Format(getDateFormat(tags))
}
default:
return buff.String(), ErrInvalidTag
}
if value != "" {
buff.WriteString(fieldName)
buff.WriteString(operator)
if useSingleQuotes {
buff.WriteString(singleQuote)
buff.WriteString(sanitizeReplacer.Replace(value))
buff.WriteString(singleQuote)
} else {
buff.WriteString(value)
}
}
return buff.String(), nil
}
func buildGreaterNextNDaysOperator(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructDateLiteralsClause(v, fieldName, greaterNextNDaysOperator)
}
func buildGreaterOrEqualNextNDaysOperator(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructDateLiteralsClause(v, fieldName, greaterOrEqualNextNDaysOperator)
}
func buildEqualsNextNDaysOperator(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructDateLiteralsClause(v, fieldName, equalsNextNDaysOperator)
}
func buildLessNextNDaysOperator(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructDateLiteralsClause(v, fieldName, lessNextNDaysOperator)
}
func buildLessOrEqualNextNDaysOperator(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructComparisonClause(v, fieldName, lessOrEqualNextNDaysOperator, tags)
}
func buildGreaterLastNDaysOperator(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructDateLiteralsClause(v, fieldName, greaterLastNDaysOperator)
}
func buildGreaterOrEqualLastNDaysOperator(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructDateLiteralsClause(v, fieldName, greaterOrEqualLastNDaysOperator)
}
func buildEqualsLastNDaysOperator(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructDateLiteralsClause(v, fieldName, equalsLastNDaysOperator)
}
func buildLessLastNDaysOperator(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructDateLiteralsClause(v, fieldName, lessLastNDaysOperator)
}
func buildLessOrEqualLastNDaysOperator(v interface{}, fieldName string, tags map[string]string) (string, error) {
return constructDateLiteralsClause(v, fieldName, lessOrEqualLastNDaysOperator)
}
func constructDateLiteralsClause(v interface{}, fieldName string, operator string) (string, error) {
var buff strings.Builder
var value string
switch u := v.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
value = fmt.Sprint(u)
case *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64:
if !reflect.ValueOf(u).IsNil() {
value = fmt.Sprint(reflect.Indirect(reflect.ValueOf(u)))
}
default:
return buff.String(), ErrInvalidTag
}
if value != "" {
buff.WriteString(fieldName)
buff.WriteString(operator)
buff.WriteString(value)
}
return buff.String(), nil
}
func buildNullClause(v interface{}, fieldName string, tags map[string]string) (string, error) {
reflectedValue, _, err := getReflectedValueAndType(v)
if err == ErrNilValue {
// Not an error case because nil value for *bool is valid
return "", nil
}
val := reflectedValue.Interface()
allowNull, ok := val.(bool)
if !ok {
return "", ErrInvalidTag
}
if allowNull {
return fieldName + equalsOperator + null, nil
}
return fieldName + notEqualsOperator + null, nil
}
func getReflectedValueAndType(v interface{}) (reflect.Value, reflect.Type, error) {
var reflectedValue reflect.Value
var reflectedType reflect.Type
if reflect.ValueOf(v).Kind() == reflect.Ptr {
if reflect.ValueOf(v).IsNil() {
return reflect.Value{}, nil, ErrNilValue
}
reflectedValue = reflect.Indirect(reflect.ValueOf(v))
} else {
reflectedValue = reflect.ValueOf(v)
}
reflectedType = reflectedValue.Type()
return reflectedValue, reflectedType, nil
}
// mapSelectColumns maps the selectColumn field name in the soql tag to their
// corresponding field name in the struct needed by marshalOrderByClause
func mapSelectColumns(mappings map[string]string, parent string, gusParent string, v interface{}) error {
reflectedValue, reflectedType, err := getReflectedValueAndType(v)
if err != nil {
return ErrInvalidSelectColumnOrderByClause
}
for i := 0; i < reflectedType.NumField(); i++ {
field := reflectedType.Field(i)
tag := field.Tag.Get(SoqlTag)
if tag == "" {
continue
}
// skip all fields that are not tagged as selectColumn
if getClauseKey(tag) != SelectColumn {
continue
}
fieldName := field.Name
gusFieldName := getFieldName(tag, field.Name)
// inside a nested struct, prepend parent to create full field names
if parent != "" {
fieldName = parent + period + fieldName
gusFieldName = gusParent + period + gusFieldName
}
fieldValue := reflectedValue.Field(i)
// the mapping for a struct field should be added regardless, to cover
// the case of a struct field not being a nested field (e.g. time.Time)
mappings[fieldName] = gusFieldName
if fieldValue.Kind() == reflect.Struct {
err := mapSelectColumns(mappings, fieldName, gusFieldName, fieldValue.Interface())
if err != nil {
return err
}
}
}
return nil
}
// v is the Order slice for specifying the columns and sort order
// s is the struct value containing fields with the selectColumn tag
func marshalOrderByClause(v interface{}, tableName string, s interface{}) (string, error) {
reflectedValue, reflectedType, err := getReflectedValueAndType(v)
if err != nil {
return "", err
}
if reflectedType.Kind() != reflect.Slice {
return "", ErrInvalidOrderByClause
}
if reflectedType.Elem() != reflect.TypeOf(Order{}) {
return "", ErrInvalidOrderByClause
}
sReflectedValue, sReflectedType, err := getReflectedValueAndType(s)
if err != nil {
return "", err
}
if sReflectedType.Kind() != reflect.Struct {
return "", ErrInvalidSelectColumnOrderByClause
}
columnMappings := make(map[string]string)
err = mapSelectColumns(columnMappings, "", "", sReflectedValue.Interface())
if err != nil {
return "", err
}
if len(columnMappings) == 0 {
return "", ErrInvalidSelectColumnOrderByClause
}
var buff strings.Builder
previousConditionExists := false
for i := 0; i < reflectedValue.Len(); i++ {
order := reflectedValue.Index(i).Interface().(Order)
fieldName := order.Field
if strings.TrimSpace(fieldName) == "" {
return "", ErrInvalidOrderByClause
}
columnName, ok := columnMappings[fieldName]
if !ok {
return "", ErrInvalidOrderByClause
}
if tableName != "" {
columnName = tableName + period + columnName
}
orderString := ascKeyword
if order.IsDesc {
orderString = descKeyword
}
partialClause := columnName + orderString
if previousConditionExists {
buff.WriteString(comma)
}
buff.WriteString(partialClause)
previousConditionExists = true
}
return buff.String(), nil
}
// v is the limit value provided
func marshalLimitClause(v interface{}) (string, error) {
s, err := marshalIntValue(v)
if err != nil {
return "", ErrInvalidLimitClause
}
return s, nil
}
// v is the offset value provided
func marshalOffsetClause(v interface{}) (string, error) {
s, err := marshalIntValue(v)
if err != nil {
return "", ErrInvalidOffsetClause
}
return s, nil
}
func marshalIntValue(v interface{}) (string, error) {
vPtr, ok := v.(*int)
if !ok {
return "", errors.New("invalid type")
}
if vPtr == nil {
return "", nil
}
vInt := *vPtr
if vInt < 0 {
return "", errors.New("invalid value")
}
vString := strconv.Itoa(vInt)
return vString, nil
}
// MarshalOrderByClause returns a string representing the SOQL order by clause.
// Parameter v is a slice of the Order struct indicating the fields from
// parameter s, which is the value of the select column struct, that should be
// included in the clause and their respective ordering (i.e. ASC or DESC).
// Consider the following struct containing the fields with selectColumn tags:
// type SelectColumns struct {
// HostName string `soql:"selectColumn,fieldName=Host_Name__c"`
// RoleName string `soql:"selectColumn,fieldName=Role__r.Name"`
// NumOfCPUCores int `soql:"selectColumn,fieldName=Num_of_CPU_Cores__c"`
// }
// s := SelectColumns{}
// And with an Order slice as follows:
// o := []Order{ Order{Field: "HostName", IsDesc: true},
// Order{Field: "NumOfCPUCores", IsDesc: false} }
// By calling MarshalOrderByClause() like the following:
// orderByClause, err := MarshalOrderByClause(o, s)
// if err != nil {
// log.Warn("Error in marshaling order by clause")
// }
// fmt.Println(orderByClause)
// This will print the orderByClause as:
// Host_Name__c DESC,Num_of_CPU_Cores__c ASC
// For nested structs, specify the field name in the Order slice using a
// <parent>.<child> notation
// For example,
func MarshalOrderByClause(v interface{}, s interface{}) (string, error) {
return marshalOrderByClause(v, "", s)
}
func marshalWhereClause(v interface{}, tableName, joiner string) (string, error) {
var buff strings.Builder
reflectedValue, reflectedType, err := getReflectedValueAndType(v)
if err != nil {
return "", err
}
previousConditionExists := false
for i := 0; i < reflectedValue.NumField(); i++ {
field := reflectedValue.Field(i)
fieldType := reflectedType.Field(i)
clauseTag := fieldType.Tag.Get(SoqlTag)
if clauseTag == "" {
continue
}
clauseKey := getClauseKey(clauseTag)
var partialClause string
if clauseKey == Subquery {
if field.Kind() != reflect.Struct && field.Kind() != reflect.Ptr {
return "", ErrInvalidTag
}
if field.Kind() == reflect.Ptr {
if reflect.ValueOf(field.Interface()).IsNil() {
continue
}
}
joiner, err := getJoiner(clauseTag)
if err != nil {
return "", err
}
if joiner == inOperator || joiner == notInOperator {
fieldName := getFieldName(clauseTag, "")
if fieldName == "" {
return "", ErrInvalidTag
}
partialJoinQuery, err := Marshal(field.Interface())
if err != nil {
return "", err
}
var queryBuff strings.Builder
queryBuff.WriteString(fieldName)
queryBuff.WriteString(joiner)
queryBuff.WriteString(openBrace)
queryBuff.WriteString(partialJoinQuery)
queryBuff.WriteString(closeBrace)
partialClause = queryBuff.String()
} else {
partialClause, err = marshalWhereClause(field.Interface(), tableName, joiner)
if err != nil {
return "", err
}
partialClause = openBrace + partialClause + closeBrace
}
} else {
fieldName := getFieldName(clauseTag, fieldType.Name)
if fieldName == "" {
return "", ErrInvalidTag
}
fn, ok := clauseBuilderMap[clauseKey]
if !ok {
return "", ErrInvalidTag
}
columnName := fieldName
if tableName != "" {
columnName = tableName + period + fieldName
}
partialClause, err = fn(field.Interface(), columnName, getTagParameterMap(clauseTag))
if err != nil {
return "", err
}
}
if partialClause != "" {
if previousConditionExists {
buff.WriteString(joiner)
}
buff.WriteString(partialClause)
previousConditionExists = true
}
}
return buff.String(), nil
}
// MarshalWhereClause returns the string with all conditions that applies for SOQL where clause.
// As part of soql tag, you will need to specify the operator (one of the operators listed below) and
// then specify the name of the field using fieldName parameter.
// Following operators are currently supported:
// 1. LIKE: Like operator. E.g. Host_Name__c LIKE '%-db%'. Use likeOperator in as soql tag
// 2. NOT LIKE: Not like operator. E.g. (NOT Host_Name__c LIKE '%-db%'). Use notLikeOperator in soql tag
// 3. EQUALS (=): Equals operator. E.g. Asset_Type_Asset_Type__c = 'SERVER'. Use equalsOperator in soql tag
// 4. IN: In operator. E.g. Role__r.Name IN ('db','dbmgmt'). Use inOperator in soql tag
// 5. NULL ( = null ): Null operator. E.g. Last_Discovered_Date__c = null. Use nullOperator in soql tag
// 6. NOT NULL: Not null operator. E.g. Last_Discovered_Date__c != null. Use nullOperator in soql tag
// 7. GREATER THAN: Greater than operator. E.g. Last_Discovered_Date__c > 2006-01-02T15:04:05.000-0700. Use greaterThanOperator in soql tag
// 8. GREATER THAN OR EQUALS TO: Greater than or equals to operator. E.g. Num_of_CPU_Cores__c >= 16. Use greaterThanOrEqualsToOperator in soql tag
// 9. LESS THAN: Less than operator. E.g. Last_Discovered_Date__c < 2006-01-02T15:04:05.000-0700. Use lessThanOperator in soql tag
// 10. LESS THAN OR EQUALS TO: Less than or equals to operator. E.g. Num_of_CPU_Cores__c <= 16. Use lessThanOrEqualsToOperator in soql tag
// Consider following go struct
// type TestQueryCriteria struct {
// IncludeNamePattern []string `soql:"likeOperator,fieldName=Host_Name__c"`
// Roles []string `soql:"inOperator,fieldName=Role__r.Name"`
// ExcludeNamePattern []string `soql:"notLikeOperator,fieldName=Host_Name__c"`
// AssetType string `soql:"equalsOperator,fieldName=Tech_Asset__r.Asset_Type_Asset_Type__c"`
// AllowNullLastDiscoveredDate *bool `soql:"nullOperator,fieldName=Last_Discovered_Date__c"`
// NumOfCPUCores int `soql:"greaterThanOperator,fieldName=Num_of_CPU_Cores__c"`
// }
// allowNull := false
// t := TestQueryCriteria{
// AssetType: "SERVER",
// IncludeNamePattern: []string{"-db", "-dbmgmt"},
// Roles: []string{"db", "dbmgmt"},
// ExcludeNamePattern: []string{"-core", "-drp"},
// AllowNullLastDiscoveredDate: &allowNull,
// NumOfCPUCores: 16,
// }
// whereClause, err := MarshalWhereClause(t)
// if err != nil {
// log.Warn("Error in marshaling where clause")
// }
// fmt.Println(whereClause)
// This will print whereClause as:
// (Host_Name__c LIKE '%-db%' OR Host_Name__c LIKE '%-dbmgmt%') AND Role__r.Name IN ('db','dbmgmt') AND ((NOT Host_Name__c LIKE '%-core%') AND (NOT Host_Name__c LIKE '%-drp%')) AND Tech_Asset__r.Asset_Type_Asset_Type__c = 'SERVER' AND Last_Discovered_Date__c != null AND Num_of_CPU_Cores__c > 16
func MarshalWhereClause(v interface{}) (string, error) {
return marshalWhereClause(v, "", andCondition)
}
func getClauseKey(clauseTag string) string {
tagItems := strings.Split(clauseTag, ",")
return tagItems[0]
}
func getJoiner(clauseTag string) (string, error) {
tag := getTagValue(clauseTag, Joiner, "")
switch strings.ToLower(tag) {
case "or":
return orCondition, nil
case "in":
return inOperator, nil
case "not in":
return notInOperator, nil
case "and", "":
return andCondition, nil
default:
return "", ErrInvalidTag
}
}
func parseTagString(tagString string) (string, string) {
delimInd := strings.Index(tagString, "=")
if delimInd == -1 {
return tagString, ""
}
return tagString[:delimInd], tagString[delimInd+1:]
}
func getTagParameterMap(clauseTag string) map[string]string {
tagMap := make(map[string]string)
tagItems := strings.Split(clauseTag, ",")
for _, tagItem := range tagItems {
tagKey, tagValue := parseTagString(tagItem)
if tagKey == "" {
continue
}
tagMap[tagKey] = tagValue
}
return tagMap
}
func getDateFormat(tags map[string]string) string {
if customFormat, ok := tags[Format]; ok {
return customFormat
}
return DateTimeFormat
}
func getTagValue(clauseTag, key, defaultValue string) string {
tagItems := strings.Split(clauseTag, ",")
for _, tagItem := range tagItems {
tagKey, tagValue := parseTagString(tagItem)
if tagKey == key {
return tagValue
}
}
return defaultValue
}
func getFieldName(clauseTag, defaultFieldName string) string {
return getTagValue(clauseTag, FieldName, defaultFieldName)
}
func getTableName(clauseTag, defaultTableName string) string {
return getTagValue(clauseTag, TableName, defaultTableName)
}
// MarshalSelectClause returns fields to be included in select clause. Child to parent and parent to child
// relationship is also supported.
// Using selectColumn and fieldName in soql tag lets you specify that the field should be included as part of
// select clause. It lets you specify the name of the field as it is named in SOQL object.
// type NonNestedStruct struct {
// Name string `soql:"selectColumn,fieldName=Name"`
// SomeValue string `soql:"selectColumn,fieldName=SomeValue__c"`
// }
// str, err := MarshalSelectClause(NonNestedStruct{}, "")
// if err != nil {
// log.Warn("Error in marshaling select clause")
// }
// fmt.Println(str)
// This will print selectClause as:
// Name,SomeValue__c
//
// Second argument to this function is the relationship name, typically used for parent relationships.
// So call to this function with relatonship name will result in marshaling as follows:
// str, err := MarshalSelectClause(NonNestedStruct{}, "NonNestedStruct__r")
// if err != nil {
// log.Warn("Error in marshaling select clause")
// }
// fmt.Println(str)
// This will print selectClause as:
// NonNestedStruct__r.Name,NonNestedStruct__r.SomeValue__c
//
// You can specify parent relationships as nested structs and specify the relationship name as field name.
// type NestedStruct struct {
// ID string `soql:"selectColumn,fieldName=Id"`
// Name string `soql:"selectColumn,fieldName=Name__c"`
// NonNestedStruct NonNestedStruct `soql:"selectColumn,fieldName=NonNestedStruct__r"`
// }
// str, err := MarshalSelectClause(NestedStruct{}, "")
// if err != nil {
// log.Warn("Error in marshaling select clause")
// }
// fmt.Println(str)
// This will print selectClause as:
// Id,Name__c,NonNestedStruct__r.Name,NonNestedStruct__r.SomeValue__c
//
// To specify child relationships in struct you will need to use selectChild tag as follows:
// type ParentStruct struct {
// ID string `soql:"selectColumn,fieldName=Id"`
// Name string `soql:"selectColumn,fieldName=Name__c"`
// ChildStruct TestChildStruct `soql:"selectChild,fieldName=Application_Versions__r"`
// }
// type TestChildStruct struct {
// SelectClause ChildStruct `soql:"selectClause,tableName=SM_Application_Versions__c"`
// }
// type ChildStruct struct {
// Version string `soql:"selectColumn,fieldName=Version__c"`
// }
// str, err := MarshalSelectClause(ParentStruct{}, "")
// if err != nil {
// log.Warn("Error in marshaling select clause")
// }
// fmt.Println(str)
// This will print selectClause as:
// Id,Name__c,(SELCT SM_Application_Versions__c.Version__c FROM Application_Versions__r)
func MarshalSelectClause(v interface{}, relationShipName string) (string, error) {
var buff strings.Builder
prefix := relationShipName
if prefix != "" {
prefix += period
}
val, t, err := getReflectedValueAndType(v)
if err != nil {
return "", err
}
if t.Kind() == reflect.Struct {
totalFields := t.NumField()
for i := 0; i < totalFields; i++ {
field := t.Field(i)
clauseTag := field.Tag.Get(SoqlTag)
if clauseTag == "" {
continue
}
clauseKey := getClauseKey(clauseTag)
isChildRelation := false
switch clauseKey {
case SelectColumn:
isChildRelation = false
case SelectChild:
isChildRelation = true
default:
return "", ErrInvalidTag
}
fieldName := getFieldName(clauseTag, field.Name)
if fieldName == "" {
return "", ErrInvalidTag
}
if isChildRelation {
subStr, err := marshal(val.Field(i), field.Type, prefix+fieldName)
if err != nil {
return "", err
}
buff.WriteString(subStr)
} else {
if field.Type.Kind() == reflect.Struct {
v := reflect.New(field.Type)
subStr, err := MarshalSelectClause(v.Elem().Interface(), prefix+fieldName)
if err != nil {
return "", err
}
buff.WriteString(subStr)
} else {
buff.WriteString(prefix)
buff.WriteString(fieldName)
}
}
buff.WriteString(comma)
}
} else {
return "", ErrInvalidTag
}
return strings.TrimRight(buff.String(), comma), nil
}
func marshal(reflectedValue reflect.Value, reflectedType reflect.Type, childRelationName string) (string, error) {
var buff strings.Builder
if reflectedType.Kind() == reflect.Struct {
totalFields := reflectedType.NumField()
if totalFields == 0 {
// Empty struct
return "", nil
}
soqlTagPresent := false
selectClausePresent := false
whereClausePresent := false
orderByClausePresent := false