-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlb_util.go
1056 lines (881 loc) · 22.2 KB
/
sqlb_util.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
package sqlb
import (
"database/sql"
"database/sql/driver"
"fmt"
r "reflect"
"regexp"
"strconv"
"strings"
"sync"
"time"
"unsafe"
)
const (
ordinalParamPrefix = '$'
namedParamPrefix = ':'
doubleColonPrefix = `::`
commentLinePrefix = `--`
commentBlockPrefix = `/*`
commentBlockSuffix = `*/`
quoteSingle = '\''
quoteDouble = '"'
quoteGrave = '`'
byteLen = 1
expectedStructNestingDepth = 8
)
var (
typeTime = r.TypeOf((*time.Time)(nil)).Elem()
typeBytes = r.TypeOf((*[]byte)(nil)).Elem()
sqlScannerRtype = r.TypeOf((*sql.Scanner)(nil)).Elem()
charsetDigitDec = new(charset).addStr(`0123456789`)
charsetIdentStart = new(charset).addStr(`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_`)
charsetIdent = new(charset).addSet(charsetIdentStart).addSet(charsetDigitDec)
charsetSpace = new(charset).addStr(" \t\v")
charsetNewline = new(charset).addStr("\r\n")
charsetWhitespace = new(charset).addSet(charsetSpace).addSet(charsetNewline)
charsetDelimStart = new(charset).addSet(charsetWhitespace).addStr(`([{.`)
charsetDelimEnd = new(charset).addSet(charsetWhitespace).addStr(`,}])`)
)
type charset [256]bool
func (self *charset) has(val byte) bool {
return self != nil && self[val]
}
func (self *charset) addStr(vals string) *charset {
for _, val := range vals {
self[val] = true
}
return self
}
func (self *charset) addSet(vals *charset) *charset {
for ind, val := range vals {
if val {
self[ind] = true
}
}
return self
}
type structNestedDbField struct {
Field r.StructField
DbPath []string
}
type structPath struct {
Name string
FieldIndex []int
MethodIndex int
}
type structFieldValue struct {
Field r.StructField
Value r.Value
}
func cacheOf[Key, Val any](fun func(Key) Val) *cache[Key, Val] {
return &cache[Key, Val]{Func: fun}
}
type cache[Key, Val any] struct {
sync.Map
Func func(Key) Val
}
// Susceptible to "thundering herd". An improvement from no caching, but still
// not ideal.
func (self *cache[Key, Val]) Get(key Key) Val {
iface, ok := self.Load(key)
if ok {
return iface.(Val)
}
val := self.Func(key)
self.Store(key, val)
return val
}
func leadingNewlineSize(val string) int {
if len(val) >= 2 && val[0] == '\r' && val[1] == '\n' {
return 2
}
if len(val) >= 1 && (val[0] == '\r' || val[0] == '\n') {
return 1
}
return 0
}
/*
Allocation-free conversion. Reinterprets a byte slice as a string. Borrowed from
the standard library. Reasonably safe. Should not be used when the underlying
byte array is volatile, for example when it's part of a scratch buffer during
SQL scanning.
*/
func bytesToMutableString(bytes []byte) string {
return *(*string)(unsafe.Pointer(&bytes))
}
/*
Allocation-free conversion. Returns a byte slice backed by the provided string.
Mutations are reflected in the source string, unless it's backed by constant
storage, in which case they trigger a segfault. Reslicing is ok. Should be safe
as long as the resulting bytes are not mutated.
*/
func stringToBytesUnsafe(val string) []byte {
type sliceHeader struct {
_ uintptr
len int
cap int
}
slice := *(*sliceHeader)(unsafe.Pointer(&val))
slice.cap = slice.len
return *(*[]byte)(unsafe.Pointer(&slice))
}
func isScannableRtype(typ r.Type) bool {
typ = typeDeref(typ)
return typ != nil && (typ == typeTime || r.PtrTo(typ).Implements(sqlScannerRtype))
}
// WTB more specific name.
func isStructType(typ r.Type) bool {
return typ != nil && typ.Kind() == r.Struct && !isScannableRtype(typ)
}
func maybeAppendSpace(val []byte) []byte {
if hasDelimSuffix(bytesToMutableString(val)) {
return val
}
return append(val, ` `...)
}
func appendMaybeSpaced(text []byte, suffix string) []byte {
if !hasDelimSuffix(bytesToMutableString(text)) && !hasDelimPrefix(suffix) {
text = append(text, ` `...)
}
text = append(text, suffix...)
return text
}
func hasDelimPrefix(text string) bool {
return len(text) <= 0 || charsetDelimEnd.has(text[0])
}
func hasDelimSuffix(text string) bool {
return len(text) <= 0 || charsetDelimStart.has(text[len(text)-1])
}
var ordReg = regexp.MustCompile(
`^\s*((?:\w+\.)*\w+)(?i)(?:\s+(asc|desc))?(?:\s+nulls\s+(first|last))?\s*$`,
)
func try(err error) {
if err != nil {
panic(err)
}
}
func try1[A any](val A, err error) A {
try(err)
return val
}
// Must be deferred.
func rec(ptr *error) {
val := recover()
if val == nil {
return
}
err, _ := val.(error)
if err != nil {
*ptr = err
return
}
panic(val)
}
/*
Questionable. Could be avoided by using `is [not] distinct from` which works for
both nulls and non-nulls, but at the time of writing, that operator doesn't
work on indexes in PG, resulting in atrocious performance.
*/
func norm(val any) any {
val = normNil(val)
if val == nil {
return nil
}
nullable, _ := val.(Nullable)
if nullable != nil {
if nullable.IsNull() {
return nil
}
return val
}
valuer, _ := val.(driver.Valuer)
if valuer != nil {
return try1(valuer.Value())
}
return val
}
func normNil(val any) any {
if isNil(val) {
return nil
}
return val
}
func counter(val int) []struct{} { return make([]struct{}, val) }
// Generics when?
func resliceStrings(val *[]string, length int) { *val = (*val)[:length] }
// Generics when?
func resliceInts(val *[]int, length int) { *val = (*val)[:length] }
// Generics when?
func copyStrings(val []string) []string {
if val == nil {
return nil
}
out := make([]string, len(val))
copy(out, val)
return out
}
// Generics when?
func copyInts(src []int) []int {
if src == nil {
return nil
}
out := make([]int, len(src))
copy(out, src)
return out
}
func trimPrefixByte(val string, prefix byte) (string, error) {
if !(len(val) >= byteLen && val[0] == prefix) {
return ``, errf(`expected %q to begin with %q`, val, rune(prefix))
}
return val[byteLen:], nil
}
func exprAppend[A Expr](expr A, text []byte) []byte {
text, _ = expr.AppendExpr(text, nil)
return text
}
func exprString[A Expr](expr A) string {
return bytesToMutableString(exprAppend(expr, nil))
}
// Copied from `github.com/mitranim/gax` and tested there.
func growBytes(prev []byte, size int) []byte {
len, cap := len(prev), cap(prev)
if cap-len >= size {
return prev
}
next := make([]byte, len, 2*cap+size)
copy(next, prev)
return next
}
// Same as `growBytes`. WTB generics.
func growInterfaces(prev []any, size int) []any {
len, cap := len(prev), cap(prev)
if cap-len >= size {
return prev
}
next := make([]any, len, 2*cap+size)
copy(next, prev)
return next
}
// Same as `growBytes`. WTB generics.
func growExprs(prev []Expr, size int) []Expr {
len, cap := len(prev), cap(prev)
if cap-len >= size {
return prev
}
next := make([]Expr, len, 2*cap+size)
copy(next, prev)
return next
}
var argTrackerPool = sync.Pool{New: newArgTracker}
func newArgTracker() any { return new(argTracker) }
func getArgTracker() *argTracker {
return argTrackerPool.Get().(*argTracker)
}
/**
Should be pooled via `sync.Pool`. All fields should be allocated lazily on
demand, but only once. Pre-binding the methods is a one-time expense which
allows to avoid repeated allocs that would be caused by passing any
key-validating functions to the "ranger" interfaces. Because "range" methods
are dynamically-dispatched, Go can't perform escape analysis, and must assume
that any inputs will escape.
*/
type argTracker struct {
Ordinal map[OrdinalParam]OrdinalParam
Named map[NamedParam]OrdinalParam
ValidateOrdinal func(int)
ValidateNamed func(string)
}
func (self *argTracker) GotOrdinal(key OrdinalParam) (OrdinalParam, bool) {
val, ok := self.Ordinal[key]
return val, ok
}
func (self *argTracker) GotNamed(key NamedParam) (OrdinalParam, bool) {
val, ok := self.Named[key]
return val, ok
}
func (self *argTracker) SetOrdinal(key OrdinalParam, val OrdinalParam) {
if self.Ordinal == nil {
self.Ordinal = make(map[OrdinalParam]OrdinalParam, 16)
}
self.Ordinal[key] = val
}
func (self *argTracker) SetNamed(key NamedParam, val OrdinalParam) {
if self.Named == nil {
self.Named = make(map[NamedParam]OrdinalParam, 16)
}
self.Named[key] = val
}
func (self *argTracker) validateOrdinal(key int) {
param := OrdinalParam(key).FromIndex()
_, ok := self.Ordinal[param]
if !ok {
panic(errUnusedOrdinal(param))
}
}
func (self *argTracker) validateNamed(key string) {
param := NamedParam(key)
_, ok := self.Named[param]
if !ok {
panic(errUnusedNamed(param))
}
}
func (self *argTracker) validate(dict ArgDict) {
impl0, _ := dict.(OrdinalRanger)
if impl0 != nil {
if self.ValidateOrdinal == nil {
self.ValidateOrdinal = self.validateOrdinal
}
impl0.RangeOrdinal(self.ValidateOrdinal)
}
impl1, _ := dict.(NamedRanger)
if impl1 != nil {
if self.ValidateNamed == nil {
self.ValidateNamed = self.validateNamed
}
impl1.RangeNamed(self.ValidateNamed)
}
}
func (self *argTracker) put() {
for key := range self.Ordinal {
delete(self.Ordinal, key)
}
for key := range self.Named {
delete(self.Named, key)
}
argTrackerPool.Put(self)
}
func strDir(val string) Dir {
if strings.EqualFold(val, `asc`) {
return DirAsc
}
if strings.EqualFold(val, `desc`) {
return DirDesc
}
return DirNone
}
func strNulls(val string) Nulls {
if strings.EqualFold(val, `first`) {
return NullsFirst
}
if strings.EqualFold(val, `last`) {
return NullsLast
}
return NullsNone
}
func countNonEmptyStrings(vals []string) (count int) {
for _, val := range vals {
if val != `` {
count++
}
}
return
}
func validateIdent(val string) {
if strings.ContainsRune(val, quoteDouble) {
panic(ErrInvalidInput{Err{
`encoding ident`,
errf(`unexpected %q in SQL identifier %q`, rune(quoteDouble), val),
}})
}
}
var prepCache = cacheOf(func(src string) Prep {
prep := Prep{Source: src}
prep.Parse()
return prep
})
var colsCache = cacheOf(func(typ r.Type) string {
typ = typeElem(typ)
if isStructType(typ) {
return structCols(typ)
}
return `*`
})
var colsDeepCache = cacheOf(func(typ r.Type) string {
typ = typeElem(typ)
if isStructType(typ) {
return structColsDeep(typ)
}
return `*`
})
func loadStructDbFields(typ r.Type) []r.StructField {
return structDbFieldsCache.Get(typeElem(typ))
}
var structDbFieldsCache = cacheOf(func(typ r.Type) []r.StructField {
// No `make` because `typ.NumField()` doesn't give us the full count.
var out []r.StructField
typ = typeElem(typ)
if typ == nil {
return out
}
reqStructType(`scanning DB-related struct fields`, typ)
path := make([]int, 0, expectedStructNestingDepth)
for ind := range counter(typ.NumField()) {
appendStructDbFields(&out, &path, typ, ind)
}
return out
})
func loadStructPaths(typ r.Type) []structPath {
return structPathsCache.Get(typeElem(typ))
}
var structPathsCache = cacheOf(func(typ r.Type) []structPath {
var out []structPath
typ = typeElem(typ)
if typ == nil {
return out
}
reqStructType(`scanning struct field and method paths`, typ)
path := make([]int, 0, expectedStructNestingDepth)
for ind := range counter(typ.NumField()) {
appendStructFieldPaths(&out, &path, typ, ind)
}
for ind := range counter(typ.NumMethod()) {
meth := typ.Method(ind)
if isPublic(meth.PkgPath) {
out = append(out, structPath{Name: meth.Name, MethodIndex: ind})
}
}
return out
})
func loadStructPathMap(typ r.Type) map[string]structPath {
return structPathMapCache.Get(typeElem(typ))
}
var structPathMapCache = cacheOf(func(typ r.Type) map[string]structPath {
paths := loadStructPaths(typ)
out := make(map[string]structPath, len(paths))
for _, val := range paths {
out[val.Name] = val
}
return out
})
func loadStructJsonPathToNestedDbFieldMap(typ r.Type) map[string]structNestedDbField {
return structJsonPathToNestedDbFieldMapCache.Get(typeElem(typ))
}
var structJsonPathToNestedDbFieldMapCache = cacheOf(func(typ r.Type) map[string]structNestedDbField {
typ = typeElem(typ)
if typ == nil {
return nil
}
reqStructType(`generating JSON-DB path mapping from struct type`, typ)
buf := map[string]structNestedDbField{}
jsonPath := make([]string, 0, expectedStructNestingDepth)
dbPath := make([]string, 0, expectedStructNestingDepth)
for ind := range counter(typ.NumField()) {
addJsonPathsToDbPaths(buf, &jsonPath, &dbPath, typ.Field(ind))
}
return buf
})
func loadStructJsonPathToDbPathFieldValueMap(typ r.Type) map[string]structFieldValue {
return structJsonPathToDbPathFieldValueMapCache.Get(typeElem(typ))
}
var structJsonPathToDbPathFieldValueMapCache = cacheOf(func(typ r.Type) map[string]structFieldValue {
src := loadStructJsonPathToNestedDbFieldMap(typ)
out := make(map[string]structFieldValue, len(src))
for key, val := range src {
out[key] = structFieldValue{val.Field, r.ValueOf(val.DbPath)}
}
return out
})
func appendStructDbFields(buf *[]r.StructField, path *[]int, typ r.Type, index int) {
field := typ.Field(index)
if !isPublic(field.PkgPath) {
return
}
defer resliceInts(path, len(*path))
*path = append(*path, index)
tag, ok := field.Tag.Lookup(TagNameDb)
if ok {
if tagIdent(tag) != `` {
field.Index = copyInts(*path)
*buf = append(*buf, field)
}
return
}
typ = typeDeref(field.Type)
if field.Anonymous && typ.Kind() == r.Struct {
for ind := range counter(typ.NumField()) {
appendStructDbFields(buf, path, typ, ind)
}
}
}
func appendStructFieldPaths(buf *[]structPath, path *[]int, typ r.Type, index int) {
field := typ.Field(index)
if !isPublic(field.PkgPath) {
return
}
defer resliceInts(path, len(*path))
*path = append(*path, index)
*buf = append(*buf, structPath{Name: field.Name, FieldIndex: copyInts(*path)})
typ = typeDeref(field.Type)
if field.Anonymous && typ.Kind() == r.Struct {
for ind := range counter(typ.NumField()) {
appendStructFieldPaths(buf, path, typ, ind)
}
}
}
func makeIter(val any) (out iter) {
out.init(val)
return
}
/*
Allows clearer code. Seems to incur no measurable overhead compared to
equivalent inline code. However, be aware that converting a stack-allocated
source value to `any` tends to involve copying.
*/
type iter struct {
field r.StructField
value r.Value
index int
count int
root r.Value
fields []r.StructField
filter Filter
}
func (self *iter) init(src any) {
if src == nil {
return
}
sparse, _ := src.(Sparse)
if sparse != nil {
self.root = valueOf(sparse.Get())
self.filter = sparse
} else {
self.root = valueOf(src)
}
if self.root.IsValid() {
self.fields = loadStructDbFields(self.root.Type())
}
}
//nolint:unused
func (self *iter) reinit() {
self.index = 0
self.count = 0
}
func (self *iter) next() bool {
fil := self.filter
for self.index < len(self.fields) {
field := self.fields[self.index]
if fil != nil && !fil.AllowField(field) {
self.index++
continue
}
self.field = field
self.value = self.root.FieldByIndex(field.Index)
self.count++
self.index++
return true
}
return false
}
func (self *iter) empty() bool { return self.count <= 0 }
func (self *iter) first() bool { return self.count == 1 }
/*
Returns true if the iterator would visit at least one field/value, otherwise
returns false. Requires `.init`.
*/
func (self *iter) has() bool {
fil := self.filter
if fil == nil {
return len(self.fields) > 0
}
for _, field := range self.fields {
if fil.AllowField(field) {
return true
}
}
return false
}
func typeElem(typ r.Type) r.Type {
for typ != nil && (typ.Kind() == r.Ptr || typ.Kind() == r.Slice) {
typ = typ.Elem()
}
return typ
}
func valueDeref(val r.Value) r.Value {
for val.Kind() == r.Ptr {
if val.IsNil() {
return r.Value{}
}
val = val.Elem()
}
return val
}
func typeElemOf(typ any) r.Type {
return typeElem(r.TypeOf(typ))
}
func typeOf(typ any) r.Type {
return typeDeref(r.TypeOf(typ))
}
func valueOf(val any) r.Value {
return valueDeref(r.ValueOf(val))
}
func kindOf(val any) r.Kind {
typ := typeOf(val)
if typ != nil {
return typ.Kind()
}
return r.Invalid
}
func isStructTypeEmpty(typ r.Type) bool {
typ = typeDeref(typ)
return typ == nil || typ.Kind() != r.Struct || typ.NumField() <= 0
}
func reqGetter(val, method r.Type, name string) {
inputs := method.NumIn()
if inputs != 0 {
panic(ErrInternal{Err{
`evaluating method`,
errf(
`can't evaluate %q of %v: expected 0 parameters, found %v parameters`,
name, val, inputs,
),
}})
}
outputs := method.NumOut()
if outputs != 1 {
panic(ErrInternal{Err{
`evaluating method`,
errf(
`can't evaluate %q of %v: expected 1 return parameter, found %v return parameters`,
name, val, outputs,
),
}})
}
}
func reqStructType(while string, typ r.Type) {
if typ.Kind() != r.Struct {
panic(errExpectedStruct(while, typ.String()))
}
}
func typeName(typ r.Type) string {
typ = typeDeref(typ)
if typ == nil {
return `nil`
}
return typ.String()
}
func typeNameOf[A any](val A) string { return typeName(r.TypeOf(val)) }
func isNil(val any) bool {
return val == nil || isValueNil(r.ValueOf(val))
}
func isValueNil(val r.Value) bool {
return !val.IsValid() || isNilable(val.Kind()) && val.IsNil()
}
func isNilable(kind r.Kind) bool {
switch kind {
case r.Chan, r.Func, r.Interface, r.Map, r.Ptr, r.Slice:
return true
default:
return false
}
}
func isPublic(pkgPath string) bool { return pkgPath == `` }
func typeDeref(typ r.Type) r.Type {
if typ == nil {
return nil
}
return typeDerefCache.Get(typ)
}
var typeDerefCache = cacheOf(func(typ r.Type) r.Type {
for typ != nil {
if typ.Kind() == r.Ptr {
typ = typ.Elem()
continue
}
if typ.Kind() == r.Struct && typ.NumField() > 0 {
field := typ.Field(0)
if field.Tag.Get(`role`) == `ref` {
typ = field.Type
continue
}
}
break
}
return typ
})
/*
TODO: consider validating that the name doesn't contain double quotes. We might
return an error, or panic.
*/
func tagIdent(tag string) string {
index := strings.IndexRune(tag, ',')
if index >= 0 {
return tagIdent(tag[:index])
}
if tag == "-" {
return ""
}
return tag
}
func structCols(typ r.Type) string {
reqStructType(`generating struct columns string from struct type`, typ)
var buf []byte
for ind, field := range loadStructDbFields(typ) {
if ind > 0 {
buf = append(buf, `, `...)
}
buf = Ident(FieldDbName(field)).AppendTo(buf)
}
return bytesToMutableString(buf)
}
func structColsDeep(typ r.Type) string {
reqStructType(`generating deep struct columns string from struct type`, typ)
var buf []byte
var path []string
for ind := range counter(typ.NumField()) {
appendFieldCols(&buf, &path, typ.Field(ind))
}
return bytesToMutableString(buf)
}
func appendFieldCols(buf *[]byte, path *[]string, field r.StructField) {
if !isPublic(field.PkgPath) {
return
}
typ := typeDeref(field.Type)
tag, ok := field.Tag.Lookup(TagNameDb)
dbName := tagIdent(tag)
if dbName == `` {
if !ok {
if field.Anonymous && typ.Kind() == r.Struct {
for ind := range counter(typ.NumField()) {
appendFieldCols(buf, path, typ.Field(ind))
}
}
}
return
}
defer resliceStrings(path, len(*path))
*path = append(*path, dbName)
if isStructType(typ) {
for ind := range counter(typ.NumField()) {
appendFieldCols(buf, path, typ.Field(ind))
}
return
}
text := *buf
if len(text) > 0 {
text = append(text, `, `...)
}
text = AliasedPath(*path).AppendTo(text)
*buf = text
}
func addJsonPathsToDbPaths(
buf map[string]structNestedDbField, jsonPath *[]string, dbPath *[]string, field r.StructField,
) {
if !isPublic(field.PkgPath) {
return
}
typ := typeDeref(field.Type)
jsonName := FieldJsonName(field)
tag, ok := field.Tag.Lookup(TagNameDb)
dbName := tagIdent(tag)
if dbName == `` {
if !ok {
if field.Anonymous && typ.Kind() == r.Struct {
for ind := range counter(typ.NumField()) {
addJsonPathsToDbPaths(buf, jsonPath, dbPath, typ.Field(ind))
}
}
}
return
}
defer resliceStrings(jsonPath, len(*jsonPath))
*jsonPath = append(*jsonPath, jsonName)
defer resliceStrings(dbPath, len(*dbPath))
*dbPath = append(*dbPath, dbName)
buf[strings.Join(*jsonPath, `.`)] = structNestedDbField{
Field: field,
DbPath: copyStrings(*dbPath),
}
if isStructType(typ) {
for ind := range counter(typ.NumField()) {
addJsonPathsToDbPaths(buf, jsonPath, dbPath, typ.Field(ind))
}
}
}
func trimWhitespaceAndComments(val Token) Token {
switch val.Type {
case TokenTypeWhitespace:
return Token{` `, TokenTypeWhitespace}
case TokenTypeCommentLine, TokenTypeCommentBlock:
return Token{}
default:
return val
}
}
func isJsonDict(val []byte) bool { return headByte(val) == '{' }
func isJsonList(val []byte) bool { return headByte(val) == '[' }
func isJsonString(val []byte) bool { return headByte(val) == '"' }
func headByte(val []byte) byte {
if len(val) > 0 {
return val[0]
}
return 0
}
func appendIntWith(text []byte, delim string, val int64) []byte {
if val == 0 {
return text
}
text = maybeAppendSpace(text)
text = append(text, delim...)
text = maybeAppendSpace(text)
text = strconv.AppendInt(text, val, 10)
return text
}
func appendPrefixSub(
text []byte, args []any, prefix string, val any,
) (
[]byte, []any,
) {
if val == nil {
return text, args
}
bui := Bui{text, args}
bui.Str(prefix)
bui.SubAny(val)
return bui.Get()