Skip to content

Commit

Permalink
chore: improve properties lookup
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
moshloop authored Jul 17, 2024
1 parent b98cced commit feb60c4
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 228 deletions.
131 changes: 96 additions & 35 deletions context/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,51 @@ import (
"github.com/flanksource/duty/models"
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/patrickmn/go-cache"
"github.com/samber/lo"
)

var Local map[string]string
var supportedProperties = cmap.New[string]()
var supportedProperties = cmap.New[PropertyType]()

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

type PropertyType struct {
Key string `json:"-"`
Value interface{} `json:"value,omitempty"`
Default interface{} `json:"default,omitempty"`
Type string `json:"type,omitempty"`
}

func (k Context) ClearCache() {
propertyCache = cache.New(time.Minute*15, time.Minute*15)
}

func nilSafe(v interface{}) string {
if v == nil {
return ""
func nilSafe(values ...interface{}) string {
for _, v := range values {
if v != nil && v != "" {
switch t := v.(type) {
case *bool:
return fmt.Sprintf("%v", *t)
default:
return fmt.Sprintf("%v", v)
}
}
}
return fmt.Sprintf("%v", v)
return ""
}
func newProp(key, def string, val interface{}) {
if loaded := supportedProperties.SetIfAbsent(key, fmt.Sprintf("%s", val)); loaded {
if val == nil {
logger.Tracef("property: %s=%v", key, console.Grayf(nilSafe(def)))
} else {
logger.Debugf("property: %s=%v (default %v)", key, console.Greenf("%s", val), nilSafe(def))

func newProp(prop PropertyType) {
if loaded := supportedProperties.SetIfAbsent(prop.Key, prop); loaded {
if prop.Value != nil && prop.Default != prop.Value {
logger.Debugf("Property overridden %s=%v (default=%v)", prop.Key, console.Greenf(nilSafe(prop.Value)), nilSafe(prop.Default))
}
}
}

func (p Properties) SupportedProperties() map[string]string {
m := make(map[string]string)
func (p Properties) SupportedProperties() map[string]PropertyType {
m := make(map[string]PropertyType)
for t := range supportedProperties.IterBuffered() {
m[t.Key] = nilSafe(t.Val)
m[t.Key] = t.Val
}
return m
}
Expand All @@ -53,63 +67,110 @@ type Properties map[string]string

// Returns true if the property is true|enabled|on, if there is no property it defaults to true
func (p Properties) On(def bool, keys ...string) bool {
var v *bool
for _, key := range keys {
k, ok := p[key]
if ok {
v := k == "true" || k == "enabled" || k == "on"
newProp(key, fmt.Sprintf("%v", def), v)
return v
prop := PropertyType{
Type: "bool",
Key: key,
Default: def,
}
newProp(key, fmt.Sprintf("%v", def), nil)
if v == nil {
k, ok := p[key]
if ok {
v = lo.ToPtr(k == "true" || k == "enabled" || k == "on")
prop.Value = v
}
}
newProp(prop)
}
if v != nil {
return *v
}
return def
}

func (p Properties) Duration(key string, def time.Duration) time.Duration {
if d, ok := p[key]; !ok {
newProp(key, fmt.Sprintf("%v", def), nil)
newProp(PropertyType{
Type: "duration",
Key: key,
Default: def,
})
return def
} else if dur, err := time.ParseDuration(d); err != nil {
newProp(PropertyType{
Type: "duration",
Key: key,
Default: def,
Value: d,
})
logger.Warnf("property[%s] invalid duration %s", key, d)
return def
} else {
newProp(key, fmt.Sprintf("%v", def), dur)
newProp(PropertyType{
Type: "duration",
Key: key,
Default: def,
Value: dur,
})
return dur
}
}

func (p Properties) Int(key string, def int) int {
if d, ok := p[key]; !ok {
newProp(key, fmt.Sprintf("%v", def), nil)
return def
} else if i, err := strconv.Atoi(d); err != nil {
logger.Warnf("property[%s] invalid int %s", key, d)
return def
} else {
newProp(key, fmt.Sprintf("%v", def), i)
return i
prop := PropertyType{
Type: "int",
Key: key,
Default: def,
}

if v, ok := p[key]; ok {
prop.Value = v
if i, err := strconv.Atoi(v); err != nil {
logger.Warnf("property[%s] invalid int %s", key, v)
} else {
prop.Value = i
newProp(prop)
return i
}
}
newProp(prop)
return def

}

func (p Properties) String(key string, def string) string {
prop := PropertyType{
Type: "string",
Key: key,
Default: def,
}
if d, ok := p[key]; ok {
newProp(key, fmt.Sprintf("%v", def), d)
prop.Value = d
newProp(prop)
return d
}
newProp(key, fmt.Sprintf("%v", def), nil)
newProp(prop)
return def

}

// Returns true if the property is false|disabled|off, if there is no property it defaults to true
func (p Properties) Off(key string, def bool) bool {

prop := PropertyType{
Type: "bool",
Key: key,
Default: def,
}
k, ok := p[key]
if !ok {
newProp(key, fmt.Sprintf("%v", def), nil)
newProp(prop)
return def
}
v := k == "false" || k == "disabled" || k == "off"
newProp(key, fmt.Sprintf("%v", def), v)
prop.Value = v
newProp(prop)
return v
}

Expand Down
63 changes: 30 additions & 33 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
module github.com/flanksource/duty

go 1.22.0
go 1.22.2

require (
ariga.io/atlas v0.14.2
cloud.google.com/go/cloudsqlconn v1.5.1
github.com/RaveNoX/go-jsonmerge v1.0.0
github.com/WinterYukky/gorm-extra-clause-plugin v0.2.0
github.com/asecurityteam/rolling v2.0.4+incompatible
github.com/eko/gocache/lib/v4 v4.1.5
github.com/eko/gocache/store/go_cache/v4 v4.2.1
github.com/eko/gocache/lib/v4 v4.1.6
github.com/eko/gocache/store/go_cache/v4 v4.2.2
github.com/exaring/otelpgx v0.5.2
github.com/fergusstrange/embedded-postgres v1.25.0
github.com/flanksource/commons v1.24.2
github.com/flanksource/gomplate/v3 v3.24.2
github.com/flanksource/gomplate/v3 v3.24.11
github.com/flanksource/kommons v0.31.4
github.com/flanksource/postq v0.1.3
github.com/google/cel-go v0.18.2
github.com/google/cel-go v0.20.1
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/hashicorp/hcl/v2 v2.18.1
github.com/hashicorp/hcl/v2 v2.21.0
github.com/hexops/gotextdiff v1.0.3
github.com/invopop/jsonschema v0.12.0
github.com/itchyny/gojq v0.12.14
github.com/itchyny/gojq v0.12.16
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/jackc/pgx/v5 v5.5.4
github.com/jackc/pgx/v5 v5.6.0
github.com/json-iterator/go v1.1.12
github.com/labstack/echo/v4 v4.11.4
github.com/labstack/echo/v4 v4.12.0
github.com/liamylian/jsontime/v2 v2.0.0
github.com/lib/pq v1.10.9
github.com/ohler55/ojg v1.20.3
github.com/onsi/ginkgo/v2 v2.17.2
github.com/onsi/gomega v1.33.0
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/client_golang v1.19.1
github.com/robfig/cron/v3 v3.0.1
github.com/rodaine/table v1.1.0
github.com/samber/lo v1.44.0
github.com/samber/lo v1.46.0
github.com/sethvargo/go-retry v0.2.4
github.com/spf13/pflag v1.0.5
github.com/timberio/go-datemath v0.1.0
Expand All @@ -50,11 +50,11 @@ require (
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.21.0
go.opentelemetry.io/otel/sdk v1.22.0
go.opentelemetry.io/otel/trace v1.24.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/exp v0.0.0-20240716175740-e3f259677ff7
golang.org/x/sync v0.7.0
google.golang.org/grpc v1.64.0
gorm.io/driver/postgres v1.5.3
gorm.io/gorm v1.25.5
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.11
k8s.io/api v0.28.2
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
Expand All @@ -64,8 +64,7 @@ require (

require (
cloud.google.com/go v0.112.1 // indirect
cloud.google.com/go/compute v1.25.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/iam v1.1.6 // indirect
cloud.google.com/go/storage v1.38.0 // indirect
github.com/AlekSi/pointer v1.1.0 // indirect
Expand Down Expand Up @@ -106,7 +105,7 @@ require (
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
github.com/google/pprof v0.0.0-20240711041743-f6c9dda6c6da // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
Expand All @@ -118,13 +117,13 @@ require (
github.com/hairyhenderson/toml v0.4.2-0.20210923231440-40456b8e66cf // indirect
github.com/hairyhenderson/yaml v0.0.0-20220618171115-2d35fca545ce // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.4 // indirect
github.com/hashicorp/go-getter v1.7.5 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/henvic/httpretty v0.1.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
Expand All @@ -136,12 +135,10 @@ require (
github.com/klauspost/compress v1.17.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
Expand All @@ -155,9 +152,9 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/robertkrimen/otto v0.2.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
Expand Down Expand Up @@ -189,20 +186,20 @@ require (
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/api v0.169.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/flanksource/yaml.v3 v3.2.3 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
Expand Down
Loading

0 comments on commit feb60c4

Please sign in to comment.