Skip to content

Commit feb60c4

Browse files
authored
chore: improve properties lookup
* chore: build supported property list * chore: lint fixes * chore: bump deps * chore: make supported properties a struct * chore: improve properties lookup * chore: fix test failures * chore: go mod tidy
1 parent b98cced commit feb60c4

File tree

6 files changed

+289
-228
lines changed

6 files changed

+289
-228
lines changed

context/properties.go

Lines changed: 96 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,51 @@ import (
1414
"github.com/flanksource/duty/models"
1515
cmap "github.com/orcaman/concurrent-map/v2"
1616
"github.com/patrickmn/go-cache"
17+
"github.com/samber/lo"
1718
)
1819

1920
var Local map[string]string
20-
var supportedProperties = cmap.New[string]()
21+
var supportedProperties = cmap.New[PropertyType]()
2122

2223
var propertyCache = cache.New(time.Minute*15, time.Minute*15)
2324

25+
type PropertyType struct {
26+
Key string `json:"-"`
27+
Value interface{} `json:"value,omitempty"`
28+
Default interface{} `json:"default,omitempty"`
29+
Type string `json:"type,omitempty"`
30+
}
31+
2432
func (k Context) ClearCache() {
2533
propertyCache = cache.New(time.Minute*15, time.Minute*15)
2634
}
2735

28-
func nilSafe(v interface{}) string {
29-
if v == nil {
30-
return ""
36+
func nilSafe(values ...interface{}) string {
37+
for _, v := range values {
38+
if v != nil && v != "" {
39+
switch t := v.(type) {
40+
case *bool:
41+
return fmt.Sprintf("%v", *t)
42+
default:
43+
return fmt.Sprintf("%v", v)
44+
}
45+
}
3146
}
32-
return fmt.Sprintf("%v", v)
47+
return ""
3348
}
34-
func newProp(key, def string, val interface{}) {
35-
if loaded := supportedProperties.SetIfAbsent(key, fmt.Sprintf("%s", val)); loaded {
36-
if val == nil {
37-
logger.Tracef("property: %s=%v", key, console.Grayf(nilSafe(def)))
38-
} else {
39-
logger.Debugf("property: %s=%v (default %v)", key, console.Greenf("%s", val), nilSafe(def))
49+
50+
func newProp(prop PropertyType) {
51+
if loaded := supportedProperties.SetIfAbsent(prop.Key, prop); loaded {
52+
if prop.Value != nil && prop.Default != prop.Value {
53+
logger.Debugf("Property overridden %s=%v (default=%v)", prop.Key, console.Greenf(nilSafe(prop.Value)), nilSafe(prop.Default))
4054
}
4155
}
4256
}
4357

44-
func (p Properties) SupportedProperties() map[string]string {
45-
m := make(map[string]string)
58+
func (p Properties) SupportedProperties() map[string]PropertyType {
59+
m := make(map[string]PropertyType)
4660
for t := range supportedProperties.IterBuffered() {
47-
m[t.Key] = nilSafe(t.Val)
61+
m[t.Key] = t.Val
4862
}
4963
return m
5064
}
@@ -53,63 +67,110 @@ type Properties map[string]string
5367

5468
// Returns true if the property is true|enabled|on, if there is no property it defaults to true
5569
func (p Properties) On(def bool, keys ...string) bool {
70+
var v *bool
5671
for _, key := range keys {
57-
k, ok := p[key]
58-
if ok {
59-
v := k == "true" || k == "enabled" || k == "on"
60-
newProp(key, fmt.Sprintf("%v", def), v)
61-
return v
72+
prop := PropertyType{
73+
Type: "bool",
74+
Key: key,
75+
Default: def,
6276
}
63-
newProp(key, fmt.Sprintf("%v", def), nil)
77+
if v == nil {
78+
k, ok := p[key]
79+
if ok {
80+
v = lo.ToPtr(k == "true" || k == "enabled" || k == "on")
81+
prop.Value = v
82+
}
83+
}
84+
newProp(prop)
85+
}
86+
if v != nil {
87+
return *v
6488
}
6589
return def
6690
}
6791

6892
func (p Properties) Duration(key string, def time.Duration) time.Duration {
6993
if d, ok := p[key]; !ok {
70-
newProp(key, fmt.Sprintf("%v", def), nil)
94+
newProp(PropertyType{
95+
Type: "duration",
96+
Key: key,
97+
Default: def,
98+
})
7199
return def
72100
} else if dur, err := time.ParseDuration(d); err != nil {
101+
newProp(PropertyType{
102+
Type: "duration",
103+
Key: key,
104+
Default: def,
105+
Value: d,
106+
})
73107
logger.Warnf("property[%s] invalid duration %s", key, d)
74108
return def
75109
} else {
76-
newProp(key, fmt.Sprintf("%v", def), dur)
110+
newProp(PropertyType{
111+
Type: "duration",
112+
Key: key,
113+
Default: def,
114+
Value: dur,
115+
})
77116
return dur
78117
}
79118
}
80119

81120
func (p Properties) Int(key string, def int) int {
82-
if d, ok := p[key]; !ok {
83-
newProp(key, fmt.Sprintf("%v", def), nil)
84-
return def
85-
} else if i, err := strconv.Atoi(d); err != nil {
86-
logger.Warnf("property[%s] invalid int %s", key, d)
87-
return def
88-
} else {
89-
newProp(key, fmt.Sprintf("%v", def), i)
90-
return i
121+
prop := PropertyType{
122+
Type: "int",
123+
Key: key,
124+
Default: def,
125+
}
126+
127+
if v, ok := p[key]; ok {
128+
prop.Value = v
129+
if i, err := strconv.Atoi(v); err != nil {
130+
logger.Warnf("property[%s] invalid int %s", key, v)
131+
} else {
132+
prop.Value = i
133+
newProp(prop)
134+
return i
135+
}
91136
}
137+
newProp(prop)
138+
return def
139+
92140
}
93141

94142
func (p Properties) String(key string, def string) string {
143+
prop := PropertyType{
144+
Type: "string",
145+
Key: key,
146+
Default: def,
147+
}
95148
if d, ok := p[key]; ok {
96-
newProp(key, fmt.Sprintf("%v", def), d)
149+
prop.Value = d
150+
newProp(prop)
97151
return d
98152
}
99-
newProp(key, fmt.Sprintf("%v", def), nil)
153+
newProp(prop)
100154
return def
101155

102156
}
103157

104158
// Returns true if the property is false|disabled|off, if there is no property it defaults to true
105159
func (p Properties) Off(key string, def bool) bool {
160+
161+
prop := PropertyType{
162+
Type: "bool",
163+
Key: key,
164+
Default: def,
165+
}
106166
k, ok := p[key]
107167
if !ok {
108-
newProp(key, fmt.Sprintf("%v", def), nil)
168+
newProp(prop)
109169
return def
110170
}
111171
v := k == "false" || k == "disabled" || k == "off"
112-
newProp(key, fmt.Sprintf("%v", def), v)
172+
prop.Value = v
173+
newProp(prop)
113174
return v
114175
}
115176

go.mod

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
module github.com/flanksource/duty
22

3-
go 1.22.0
3+
go 1.22.2
44

55
require (
66
ariga.io/atlas v0.14.2
77
cloud.google.com/go/cloudsqlconn v1.5.1
88
github.com/RaveNoX/go-jsonmerge v1.0.0
99
github.com/WinterYukky/gorm-extra-clause-plugin v0.2.0
1010
github.com/asecurityteam/rolling v2.0.4+incompatible
11-
github.com/eko/gocache/lib/v4 v4.1.5
12-
github.com/eko/gocache/store/go_cache/v4 v4.2.1
11+
github.com/eko/gocache/lib/v4 v4.1.6
12+
github.com/eko/gocache/store/go_cache/v4 v4.2.2
1313
github.com/exaring/otelpgx v0.5.2
1414
github.com/fergusstrange/embedded-postgres v1.25.0
1515
github.com/flanksource/commons v1.24.2
16-
github.com/flanksource/gomplate/v3 v3.24.2
16+
github.com/flanksource/gomplate/v3 v3.24.11
1717
github.com/flanksource/kommons v0.31.4
1818
github.com/flanksource/postq v0.1.3
19-
github.com/google/cel-go v0.18.2
19+
github.com/google/cel-go v0.20.1
2020
github.com/google/go-cmp v0.6.0
2121
github.com/google/uuid v1.6.0
22-
github.com/hashicorp/hcl/v2 v2.18.1
22+
github.com/hashicorp/hcl/v2 v2.21.0
2323
github.com/hexops/gotextdiff v1.0.3
2424
github.com/invopop/jsonschema v0.12.0
25-
github.com/itchyny/gojq v0.12.14
25+
github.com/itchyny/gojq v0.12.16
2626
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
27-
github.com/jackc/pgx/v5 v5.5.4
27+
github.com/jackc/pgx/v5 v5.6.0
2828
github.com/json-iterator/go v1.1.12
29-
github.com/labstack/echo/v4 v4.11.4
29+
github.com/labstack/echo/v4 v4.12.0
3030
github.com/liamylian/jsontime/v2 v2.0.0
3131
github.com/lib/pq v1.10.9
3232
github.com/ohler55/ojg v1.20.3
3333
github.com/onsi/ginkgo/v2 v2.17.2
3434
github.com/onsi/gomega v1.33.0
3535
github.com/orcaman/concurrent-map/v2 v2.0.1
3636
github.com/patrickmn/go-cache v2.1.0+incompatible
37-
github.com/prometheus/client_golang v1.14.0
37+
github.com/prometheus/client_golang v1.19.1
3838
github.com/robfig/cron/v3 v3.0.1
3939
github.com/rodaine/table v1.1.0
40-
github.com/samber/lo v1.44.0
40+
github.com/samber/lo v1.46.0
4141
github.com/sethvargo/go-retry v0.2.4
4242
github.com/spf13/pflag v1.0.5
4343
github.com/timberio/go-datemath v0.1.0
@@ -50,11 +50,11 @@ require (
5050
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.21.0
5151
go.opentelemetry.io/otel/sdk v1.22.0
5252
go.opentelemetry.io/otel/trace v1.24.0
53-
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
53+
golang.org/x/exp v0.0.0-20240716175740-e3f259677ff7
5454
golang.org/x/sync v0.7.0
5555
google.golang.org/grpc v1.64.0
56-
gorm.io/driver/postgres v1.5.3
57-
gorm.io/gorm v1.25.5
56+
gorm.io/driver/postgres v1.5.9
57+
gorm.io/gorm v1.25.11
5858
k8s.io/api v0.28.2
5959
k8s.io/apimachinery v0.28.2
6060
k8s.io/client-go v0.28.2
@@ -64,8 +64,7 @@ require (
6464

6565
require (
6666
cloud.google.com/go v0.112.1 // indirect
67-
cloud.google.com/go/compute v1.25.1 // indirect
68-
cloud.google.com/go/compute/metadata v0.2.3 // indirect
67+
cloud.google.com/go/compute/metadata v0.3.0 // indirect
6968
cloud.google.com/go/iam v1.1.6 // indirect
7069
cloud.google.com/go/storage v1.38.0 // indirect
7170
github.com/AlekSi/pointer v1.1.0 // indirect
@@ -106,7 +105,7 @@ require (
106105
github.com/google/btree v1.0.1 // indirect
107106
github.com/google/gnostic-models v0.6.8 // indirect
108107
github.com/google/gofuzz v1.2.0 // indirect
109-
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
108+
github.com/google/pprof v0.0.0-20240711041743-f6c9dda6c6da // indirect
110109
github.com/google/s2a-go v0.1.7 // indirect
111110
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
112111
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
@@ -118,13 +117,13 @@ require (
118117
github.com/hairyhenderson/toml v0.4.2-0.20210923231440-40456b8e66cf // indirect
119118
github.com/hairyhenderson/yaml v0.0.0-20220618171115-2d35fca545ce // indirect
120119
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
121-
github.com/hashicorp/go-getter v1.7.4 // indirect
120+
github.com/hashicorp/go-getter v1.7.5 // indirect
122121
github.com/hashicorp/go-safetemp v1.0.0 // indirect
123-
github.com/hashicorp/go-version v1.6.0 // indirect
122+
github.com/hashicorp/go-version v1.7.0 // indirect
124123
github.com/henvic/httpretty v0.1.2 // indirect
125124
github.com/imdario/mergo v0.3.13 // indirect
126125
github.com/inconshreveable/mousetrap v1.1.0 // indirect
127-
github.com/itchyny/timefmt-go v0.1.5 // indirect
126+
github.com/itchyny/timefmt-go v0.1.6 // indirect
128127
github.com/jackc/pgpassfile v1.0.0 // indirect
129128
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
130129
github.com/jackc/puddle/v2 v2.2.1 // indirect
@@ -136,12 +135,10 @@ require (
136135
github.com/klauspost/compress v1.17.4 // indirect
137136
github.com/kr/pretty v0.3.1 // indirect
138137
github.com/kr/text v0.2.0 // indirect
139-
github.com/kylelemons/godebug v1.1.0 // indirect
140138
github.com/labstack/gommon v0.4.2 // indirect
141139
github.com/mailru/easyjson v0.7.7 // indirect
142140
github.com/mattn/go-colorable v0.1.13 // indirect
143141
github.com/mattn/go-isatty v0.0.20 // indirect
144-
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
145142
github.com/mitchellh/go-homedir v1.1.0 // indirect
146143
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
147144
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
@@ -155,9 +152,9 @@ require (
155152
github.com/opencontainers/go-digest v1.0.0 // indirect
156153
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
157154
github.com/pkg/errors v0.9.1 // indirect
158-
github.com/prometheus/client_model v0.3.0 // indirect
159-
github.com/prometheus/common v0.37.0 // indirect
160-
github.com/prometheus/procfs v0.8.0 // indirect
155+
github.com/prometheus/client_model v0.6.1 // indirect
156+
github.com/prometheus/common v0.55.0 // indirect
157+
github.com/prometheus/procfs v0.15.1 // indirect
161158
github.com/robertkrimen/otto v0.2.1 // indirect
162159
github.com/rogpeppe/go-internal v1.12.0 // indirect
163160
github.com/sergi/go-diff v1.3.1 // indirect
@@ -189,20 +186,20 @@ require (
189186
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
190187
go.uber.org/multierr v1.11.0 // indirect
191188
go.uber.org/zap v1.26.0 // indirect
192-
golang.org/x/crypto v0.23.0 // indirect
193-
golang.org/x/net v0.25.0 // indirect
194-
golang.org/x/oauth2 v0.18.0 // indirect
195-
golang.org/x/sys v0.20.0 // indirect
196-
golang.org/x/term v0.20.0 // indirect
189+
golang.org/x/crypto v0.25.0 // indirect
190+
golang.org/x/mod v0.19.0 // indirect
191+
golang.org/x/net v0.27.0 // indirect
192+
golang.org/x/oauth2 v0.21.0 // indirect
193+
golang.org/x/sys v0.22.0 // indirect
194+
golang.org/x/term v0.22.0 // indirect
197195
golang.org/x/text v0.16.0 // indirect
198196
golang.org/x/time v0.5.0 // indirect
199-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
197+
golang.org/x/tools v0.23.0 // indirect
200198
google.golang.org/api v0.169.0 // indirect
201-
google.golang.org/appengine v1.6.8 // indirect
202199
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
203200
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect
204201
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
205-
google.golang.org/protobuf v1.33.0 // indirect
202+
google.golang.org/protobuf v1.34.2 // indirect
206203
gopkg.in/flanksource/yaml.v3 v3.2.3 // indirect
207204
gopkg.in/inf.v0 v0.9.1 // indirect
208205
gopkg.in/sourcemap.v1 v1.0.5 // indirect

0 commit comments

Comments
 (0)