Repository navigation
Expand file tree
/
Copy pathoption.go
More file actions
82 lines (78 loc) · 2.01 KB
/
Copy pathoption.go
File metadata and controls
82 lines (78 loc) · 2.01 KB
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
package wicked
import (
"fmt"
"strings"
"time"
)
const (
WPgxOptionKeyCache = "cache"
WPgxOptionKeyInvalidate = "invalidate"
WpgxOptionKeyCountIntent = "count_intent"
WpgxOptionKeyTimeout = "timeout"
WpgxOptionKeyAllowReplica = "allow_replica"
WpgxOptionKeyEach = "each"
)
type WPgxOption struct {
Cache time.Duration
Invalidates []string
CountIntent bool
Timeout time.Duration
AllowReplica bool
Each bool
}
func parseOption(options map[string]string, queryNames map[string]bool) (rv WPgxOption, err error) {
for k, v := range options {
switch k {
case WPgxOptionKeyCache:
rv.Cache, err = time.ParseDuration(v)
if err != nil {
return
}
if rv.Cache < 1*time.Millisecond {
return rv, fmt.Errorf("cache duration too short: %s", v)
}
case WPgxOptionKeyInvalidate:
trimed := strings.Trim(v, " []")
fnNames := strings.Split(trimed, ",")
for _, rawFnName := range fnNames {
queryName := strings.TrimSpace(rawFnName)
if !queryNames[queryName] {
return rv, fmt.Errorf("Unknown to invalidate query: %s", queryName)
}
rv.Invalidates = append(rv.Invalidates, queryName)
}
case WpgxOptionKeyCountIntent:
if v == "true" {
rv.CountIntent = true
} else if v == "false" {
rv.CountIntent = false
} else {
return rv, fmt.Errorf("Unknown count_intent value: %s", v)
}
case WpgxOptionKeyTimeout:
rv.Timeout, err = time.ParseDuration(v)
if err != nil {
return
}
if rv.Timeout < 1*time.Millisecond {
return rv, fmt.Errorf("timeout duration too short: %s", v)
}
case WpgxOptionKeyEach:
if v != "true" && v != "false" {
return rv, fmt.Errorf("invalid each value: %s", v)
}
rv.Each = v == "true"
case WpgxOptionKeyAllowReplica:
if v == "true" {
rv.AllowReplica = true
} else if v == "false" {
rv.AllowReplica = false
} else {
return rv, fmt.Errorf("Unknown allow_replica value: %s", v)
}
default:
return rv, fmt.Errorf("Unknown option: %s", k)
}
}
return
}