-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_variables.go
726 lines (655 loc) · 20.6 KB
/
system_variables.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
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"time"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"github.com/bufbuild/protocompile"
"github.com/cloudspannerecosystem/memefish/ast"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoregistry"
"github.com/samber/lo"
"spheric.cloud/xiter"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"cloud.google.com/go/spanner"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
)
type systemVariables struct {
RPCPriority sppb.RequestOptions_Priority
ReadOnlyStaleness *spanner.TimestampBound
ReadTimestamp time.Time
OptimizerVersion string
OptimizerStatisticsPackage string
CommitResponse *sppb.CommitResponse
CommitTimestamp time.Time
CLIFormat DisplayMode
Project, Instance, Database string
Verbose bool
Prompt string
Prompt2 string
HistoryFile string
Role string
Endpoint string
DirectedRead *sppb.DirectedReadOptions
ProtoDescriptorFile []string
BuildStatementMode parseMode
LogGrpc bool
QueryMode *sppb.ExecuteSqlRequest_QueryMode
LintPlan bool
TransactionTag string
RequestTag string
UsePager bool
DatabaseDialect databasepb.DatabaseDialect
MarkdownCodeblock bool
// it is internal variable and hidden from system variable statements
ProtoDescriptor *descriptorpb.FileDescriptorSet
Insecure bool
Debug bool
Params map[string]ast.Node
// link to session
CurrentSession *Session
ReadOnly bool
VertexAIProject string
AutoWrap bool
EchoExecutedDDL bool
EnableHighlight bool
EchoInput bool
// TODO: Expose as CLI_*
EnableProgressBar bool
ImpersonateServiceAccount string
EnableADCPlus bool
MultilineProtoText bool
}
var errIgnored = errors.New("ignored")
type setter = func(this *systemVariables, name, value string) error
type adder = func(this *systemVariables, name, value string) error
type getter = func(this *systemVariables, name string) (map[string]string, error)
type accessor struct {
Setter setter
Getter getter
Adder adder
}
func (sv *systemVariables) InstancePath() string {
return instancePath(sv.Project, sv.Instance)
}
func (sv *systemVariables) DatabasePath() string {
return databasePath(sv.Project, sv.Instance, sv.Database)
}
func (sv *systemVariables) ProjectPath() string {
return projectPath(sv.Project)
}
func (sv *systemVariables) Set(name string, value string) error {
upperName := strings.ToUpper(name)
a, ok := accessorMap[upperName]
if !ok {
return fmt.Errorf("unknown variable name: %v", name)
}
if a.Setter == nil {
return fmt.Errorf("setter unimplemented: %v", name)
}
return a.Setter(sv, upperName, value)
}
func (sv *systemVariables) Add(name string, value string) error {
upperName := strings.ToUpper(name)
a, ok := accessorMap[upperName]
if !ok {
return fmt.Errorf("unknown variable name: %v", name)
}
if a.Adder == nil {
return fmt.Errorf("adder unimplemented: %v", name)
}
return a.Adder(sv, upperName, value)
}
func (sv *systemVariables) Get(name string) (map[string]string, error) {
upperName := strings.ToUpper(name)
a, ok := accessorMap[upperName]
if !ok {
return nil, fmt.Errorf("unknown variable name: %v", name)
}
if a.Getter == nil {
return nil, fmt.Errorf("getter unimplemented: %v", name)
}
value, err := a.Getter(sv, name)
if err != nil && !errors.Is(err, errIgnored) {
return nil, err
}
return value, nil
}
func unquoteString(s string) string {
return strings.Trim(s, `"'`)
}
func sliceOf[V any](vs ...V) []V {
return vs
}
func singletonMap[K comparable, V any](k K, v V) map[K]V {
return map[K]V{k: v}
}
func parseTimeString(s string) (time.Time, error) {
return time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", s)
}
var accessorMap = map[string]accessor{
"READONLY": {
Setter: func(this *systemVariables, name, value string) error {
if this.CurrentSession != nil && (this.CurrentSession.InReadOnlyTransaction() || this.CurrentSession.InReadWriteTransaction()) {
return errors.New("can't change READONLY when there is a active transaction")
}
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
this.ReadOnly = b
return nil
},
Getter: boolGetter(func(sysVars *systemVariables) *bool { return &sysVars.ReadOnly }),
},
"AUTOCOMMIT": {},
"RETRY_ABORTS_INTERNALLY": {},
"AUTOCOMMIT_DML_MODE": {},
"STATEMENT_TIMEOUT": {},
"READ_ONLY_STALENESS": {
func(this *systemVariables, name, value string) error {
staleness, err := parseTimestampBound(unquoteString(value))
if err != nil {
return err
}
this.ReadOnlyStaleness = &staleness
return nil
},
func(this *systemVariables, name string) (map[string]string, error) {
if this.ReadOnlyStaleness == nil {
return nil, errIgnored
}
s := this.ReadOnlyStaleness.String()
stalenessRe := regexp.MustCompile(`^\(([^:]+)(?:: (.+))?\)$`)
matches := stalenessRe.FindStringSubmatch(s)
if matches == nil {
return singletonMap(name, s), nil
}
switch matches[1] {
case "strong":
return singletonMap(name, "STRONG"), nil
case "exactStaleness":
return singletonMap(name, fmt.Sprintf("EXACT_STALENESS %v", matches[2])), nil
case "maxStaleness":
return singletonMap(name, fmt.Sprintf("MAX_STALENESS %v", matches[2])), nil
// TODO: re-format timestamp as RFC3339
case "readTimestamp":
ts, err := parseTimeString(matches[2])
if err != nil {
return nil, err
}
return singletonMap(name, fmt.Sprintf("READ_TIMESTAMP %v", ts.Format(time.RFC3339Nano))), nil
case "minReadTimestamp":
ts, err := parseTimeString(matches[2])
if err != nil {
return nil, err
}
return singletonMap(name, fmt.Sprintf("MIN_READ_TIMESTAMP %v", ts.Format(time.RFC3339Nano))), nil
default:
return singletonMap(name, s), nil
}
},
nil,
},
"OPTIMIZER_VERSION": stringAccessor(func(sysVars *systemVariables) *string {
return &sysVars.OptimizerVersion
}),
"OPTIMIZER_STATISTICS_PACKAGE": stringAccessor(func(sysVars *systemVariables) *string {
return &sysVars.OptimizerStatisticsPackage
}),
"RETURN_COMMIT_STATS": {},
"RPC_PRIORITY": {
Setter: func(this *systemVariables, name, value string) error {
s := unquoteString(value)
p, err := parsePriority(s)
if err != nil {
return err
}
this.RPCPriority = p
return nil
},
Getter: func(this *systemVariables, name string) (map[string]string, error) {
return singletonMap(name, strings.TrimPrefix(this.RPCPriority.String(), "PRIORITY_")), nil
},
},
"TRANSACTION_TAG": {
Setter: func(this *systemVariables, name, value string) error {
if this.CurrentSession == nil {
return errors.New("invalid state: current session is not populated")
}
if !this.CurrentSession.InPendingTransaction() {
return errors.New("there is no pending transaction")
}
this.CurrentSession.tc.tag = unquoteString(value)
return nil
},
Getter: func(this *systemVariables, name string) (map[string]string, error) {
if this.CurrentSession == nil || this.CurrentSession.tc == nil || this.CurrentSession.tc.tag == "" {
return singletonMap(name, ""), errIgnored
}
return singletonMap(name, this.CurrentSession.tc.tag), nil
},
},
"STATEMENT_TAG": {
Setter: func(this *systemVariables, name, value string) error {
this.RequestTag = unquoteString(value)
return nil
},
Getter: func(this *systemVariables, name string) (map[string]string, error) {
if this.RequestTag == "" {
return nil, errIgnored
}
return singletonMap(name, this.RequestTag), nil
},
},
"READ_TIMESTAMP": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
if this.ReadTimestamp.IsZero() {
return nil, errIgnored
}
return singletonMap(name, this.ReadTimestamp.Format(time.RFC3339Nano)), nil
},
},
"COMMIT_TIMESTAMP": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
if this.CommitTimestamp.IsZero() {
return nil, errIgnored
}
s := this.CommitTimestamp.Format(time.RFC3339Nano)
return singletonMap(name, s), nil
},
},
"COMMIT_RESPONSE": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
if this.CommitResponse == nil {
return nil, errIgnored
}
mutationCount := this.CommitResponse.GetCommitStats().GetMutationCount()
return map[string]string{
"COMMIT_TIMESTAMP": this.CommitTimestamp.Format(time.RFC3339Nano),
"MUTATION_COUNT": strconv.FormatInt(mutationCount, 10),
}, nil
},
},
"CLI_FORMAT": {
Setter: func(this *systemVariables, name, value string) error {
switch strings.ToUpper(unquoteString(value)) {
case "TABLE":
this.CLIFormat = DisplayModeTable
case "TABLE_COMMENT":
this.CLIFormat = DisplayModeTableComment
case "TABLE_DETAIL_COMMENT":
this.CLIFormat = DisplayModeTableDetailComment
case "VERTICAL":
this.CLIFormat = DisplayModeVertical
case "TAB":
this.CLIFormat = DisplayModeTab
}
return nil
},
Getter: func(this *systemVariables, name string) (map[string]string, error) {
var formatStr string
switch this.CLIFormat {
case DisplayModeTable:
formatStr = "TABLE"
case DisplayModeVertical:
formatStr = "VERTICAL"
case DisplayModeTab:
formatStr = "TAB"
}
return singletonMap(name, formatStr), nil
},
},
"CLI_VERBOSE": {
Getter: boolGetter(func(sysVars *systemVariables) *bool { return &sysVars.Verbose }),
Setter: func(this *systemVariables, name, value string) error {
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
this.Verbose = b
return nil
},
},
"CLI_DATABASE_DIALECT": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
return singletonMap(name, this.DatabaseDialect.String()), nil
},
Setter: func(this *systemVariables, name, value string) error {
n, ok := databasepb.DatabaseDialect_value[strings.ToUpper(unquoteString(value))]
if !ok {
return fmt.Errorf("invalid value: %v", value)
}
this.DatabaseDialect = databasepb.DatabaseDialect(n)
return nil
},
},
"CLI_ECHO_EXECUTED_DDL": {
Getter: boolGetter(func(sysVars *systemVariables) *bool { return &sysVars.EchoExecutedDDL }),
Setter: func(this *systemVariables, name, value string) error {
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
this.EchoExecutedDDL = b
return nil
},
},
"CLI_ROLE": {
Getter: stringGetter(func(sysVars *systemVariables) *string { return &sysVars.Role }),
},
"CLI_ECHO_INPUT": boolAccessor(func(sysVars *systemVariables) *bool { return &sysVars.EchoInput }),
"CLI_ENDPOINT": {
Getter: stringGetter(func(sysVars *systemVariables) *string { return &sysVars.Endpoint }),
},
"CLI_DIRECT_READ": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
return singletonMap(name, xiter.Join(xiter.Map(
slices.Values(this.DirectedRead.GetIncludeReplicas().GetReplicaSelections()),
func(rs *sppb.DirectedReadOptions_ReplicaSelection) string {
return fmt.Sprintf("%v:%v", rs.GetLocation(), rs.GetType())
},
), ",")), nil
},
},
"CLI_HISTORY_FILE": {
Getter: stringGetter(
func(sysVars *systemVariables) *string { return &sysVars.HistoryFile },
),
},
"CLI_PROMPT": stringAccessor(func(sysVars *systemVariables) *string { return &sysVars.Prompt }),
"CLI_PROMPT2": stringAccessor(func(sysVars *systemVariables) *string { return &sysVars.Prompt2 }),
"CLI_PROJECT": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
return singletonMap(name, this.Project), nil
},
},
"CLI_INSTANCE": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
return singletonMap(name, this.Instance), nil
},
},
"CLI_DATABASE": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
return singletonMap(name, this.Database), nil
},
},
"CLI_PROTO_DESCRIPTOR_FILE": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
return singletonMap(name, strings.Join(this.ProtoDescriptorFile, ",")), nil
},
Setter: func(this *systemVariables, name, value string) error {
filenames := strings.Split(unquoteString(value), ",")
if len(filenames) == 0 {
return nil
}
var fileDescriptorSet *descriptorpb.FileDescriptorSet
for _, filename := range filenames {
fds, err := readFileDescriptorProtoFromFile(filename)
if err != nil {
return err
}
fileDescriptorSet = mergeFDS(fileDescriptorSet, fds)
}
this.ProtoDescriptorFile = filenames
this.ProtoDescriptor = fileDescriptorSet
return nil
},
Adder: func(this *systemVariables, name, value string) error {
filename := unquoteString(value)
fds, err := readFileDescriptorProtoFromFile(filename)
if err != nil {
return err
}
if !slices.Contains(this.ProtoDescriptorFile, filename) {
this.ProtoDescriptorFile = slices.Concat(this.ProtoDescriptorFile, sliceOf(filename))
this.ProtoDescriptor = &descriptorpb.FileDescriptorSet{File: slices.Concat(this.ProtoDescriptor.GetFile(), fds.GetFile())}
} else {
this.ProtoDescriptor = mergeFDS(this.ProtoDescriptor, fds)
}
return nil
},
},
"CLI_PARSE_MODE": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
if this.BuildStatementMode == parseModeUnspecified {
return nil, errIgnored
}
return singletonMap(name, string(this.BuildStatementMode)), nil
},
Setter: func(this *systemVariables, name, value string) error {
s := strings.ToUpper(unquoteString(value))
switch s {
case string(parseModeFallback),
string(parseMemefishOnly),
string(parseModeNoMemefish),
string(parseModeUnspecified):
this.BuildStatementMode = parseMode(s)
return nil
default:
return fmt.Errorf("invalid value: %v", s)
}
},
},
"CLI_INSECURE": {
Getter: boolGetter(func(sysVars *systemVariables) *bool { return &sysVars.Insecure }),
},
"CLI_DEBUG": {
Getter: boolGetter(func(sysVars *systemVariables) *bool { return lo.Ternary(sysVars.Debug, lo.ToPtr(sysVars.Debug), nil) }),
Setter: boolSetter(func(sysVars *systemVariables) *bool { return &sysVars.Debug }),
},
"CLI_LOG_GRPC": {
Getter: boolGetter(func(sysVars *systemVariables) *bool {
return lo.Ternary(sysVars.LogGrpc, &sysVars.LogGrpc, nil)
}),
},
"CLI_LINT_PLAN": {
Getter: boolGetter(func(sysVars *systemVariables) *bool {
return lo.Ternary(sysVars.LintPlan, lo.ToPtr(sysVars.LintPlan), nil)
}),
Setter: boolSetter(func(sysVars *systemVariables) *bool { return &sysVars.LintPlan }),
},
"CLI_USE_PAGER": boolAccessor(func(variables *systemVariables) *bool {
return &variables.UsePager
}),
"CLI_AUTOWRAP": boolAccessor(func(variables *systemVariables) *bool {
return &variables.AutoWrap
}),
"CLI_ENABLE_HIGHLIGHT": boolAccessor(func(variables *systemVariables) *bool {
return &variables.EnableHighlight
}),
"CLI_PROTOTEXT_MULTILINE": boolAccessor(func(variables *systemVariables) *bool {
return &variables.MultilineProtoText
}),
"CLI_MARKDOWN_CODEBLOCK": boolAccessor(func(variables *systemVariables) *bool {
return &variables.MarkdownCodeblock
}),
"CLI_QUERY_MODE": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
if this.QueryMode == nil {
return nil, errIgnored
}
return singletonMap(name, this.QueryMode.String()), nil
},
Setter: func(this *systemVariables, name, value string) error {
s := unquoteString(value)
mode, ok := sppb.ExecuteSqlRequest_QueryMode_value[strings.ToUpper(s)]
if !ok {
return fmt.Errorf("invalid value: %v", s)
}
this.QueryMode = sppb.ExecuteSqlRequest_QueryMode(mode).Enum()
return nil
},
},
"CLI_VERSION": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
return singletonMap(name, getVersion()), nil
},
},
}
func mergeFDS(left, right *descriptorpb.FileDescriptorSet) *descriptorpb.FileDescriptorSet {
result := slices.Clone(left.GetFile())
for _, fd := range right.GetFile() {
idx := slices.IndexFunc(result, func(descriptorProto *descriptorpb.FileDescriptorProto) bool {
return descriptorProto.GetPackage() == fd.GetPackage() && descriptorProto.GetName() == fd.GetName()
})
if idx != -1 {
result[idx] = fd
} else {
result = append(result, fd)
}
}
return &descriptorpb.FileDescriptorSet{File: result}
}
var httpResolver = protocompile.ResolverFunc(httpResolveFunc)
var httpOrHTTPSRe = regexp.MustCompile("^https?://")
func httpResolveFunc(path string) (protocompile.SearchResult, error) {
if !httpOrHTTPSRe.MatchString(path) {
return protocompile.SearchResult{}, protoregistry.NotFound
}
resp, err := http.Get(path)
if err != nil {
return protocompile.SearchResult{}, err
}
defer resp.Body.Close()
var buf bytes.Buffer
_, err = io.Copy(&buf, resp.Body)
if err != nil {
return protocompile.SearchResult{}, err
}
return protocompile.SearchResult{Source: &buf}, nil
}
var resolver = protocompile.CompositeResolver{&protocompile.SourceResolver{}, httpResolver}
func readFileDescriptorProtoFromFile(filename string) (*descriptorpb.FileDescriptorSet, error) {
if filepath.Ext(filename) == ".proto" {
compiler := protocompile.Compiler{
Resolver: protocompile.WithStandardImports(resolver),
}
files, err := compiler.Compile(context.Background(), filename)
if err != nil {
return nil, err
}
return &descriptorpb.FileDescriptorSet{
File: sliceOf(protodesc.ToFileDescriptorProto(files.FindFileByPath(filename))),
}, nil
}
var b []byte
var err error
if httpOrHTTPSRe.MatchString(filename) {
resp, err := http.Get(filename)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
} else {
b, err = os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error on read proto descriptor-file %v: %w", filename, err)
}
}
var fds descriptorpb.FileDescriptorSet
err = proto.Unmarshal(b, &fds)
if err != nil {
return nil, fmt.Errorf("error on unmarshal proto descriptor-file %v: %w", filename, err)
}
return &fds, nil
}
func stringGetter(f func(sysVars *systemVariables) *string) getter {
return func(this *systemVariables, name string) (map[string]string, error) {
ref := f(this)
if ref == nil {
return nil, errIgnored
}
return singletonMap(name, *ref), nil
}
}
func boolGetter(f func(sysVars *systemVariables) *bool) getter {
return func(this *systemVariables, name string) (map[string]string, error) {
ref := f(this)
if ref == nil {
return nil, errIgnored
}
return singletonMap(name, strings.ToUpper(strconv.FormatBool(*ref))), nil
}
}
func boolSetter(f func(sysVars *systemVariables) *bool) setter {
return func(this *systemVariables, name string, value string) error {
ref := f(this)
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
*ref = b
return nil
}
}
func stringSetter(f func(sysVars *systemVariables) *string) setter {
return func(this *systemVariables, name, value string) error {
ref := f(this)
s := unquoteString(value)
*ref = s
return nil
}
}
func boolAccessor(f func(variables *systemVariables) *bool) accessor {
return accessor{
Setter: boolSetter(f),
Getter: boolGetter(f),
}
}
func stringAccessor(f func(variables *systemVariables) *string) accessor {
return accessor{
Setter: stringSetter(f),
Getter: stringGetter(f),
}
}
func parseTimestampBound(s string) (spanner.TimestampBound, error) {
first, second, _ := strings.Cut(s, " ")
// only for error result
nilStaleness := spanner.StrongRead()
switch strings.ToUpper(first) {
case "STRONG":
return spanner.StrongRead(), nil
case "MIN_READ_TIMESTAMP":
ts, err := time.Parse(time.RFC3339Nano, second)
if err != nil {
return nilStaleness, err
}
return spanner.MinReadTimestamp(ts), nil
case "READ_TIMESTAMP":
ts, err := time.Parse(time.RFC3339Nano, second)
if err != nil {
return nilStaleness, err
}
return spanner.ReadTimestamp(ts), nil
case "MAX_STALENESS":
ts, err := time.ParseDuration(second)
if err != nil {
return nilStaleness, err
}
return spanner.MaxStaleness(ts), nil
case "EXACT_STALENESS":
ts, err := time.ParseDuration(second)
if err != nil {
return nilStaleness, err
}
return spanner.ExactStaleness(ts), nil
default:
return nilStaleness, fmt.Errorf("unknown staleness: %v", first)
}
}