Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions cmd/tools/gendynamicconfig/dynamic_config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ const (
{{- range $P := $Precedences}}
{{ if $T.IsGeneric -}}
type {{$P.Name}}TypedSetting[T any] setting[T, func({{$P.GoArgs}})]
type {{$P.Name}}TypedConstrainedDefaultSetting[T any] constrainedDefaultSetting[T, func({{$P.GoArgs}})]

// New{{$P.Name}}TypedSetting creates a setting that uses mapstructure to handle complex structured
// values. The value from dynamic config will be copied over a shallow copy of 'def', which means
// 'def' must not contain any non-nil slices, maps, or pointers.
// values. The value from dynamic config will be _merged_ over a deep copy of 'def'. Be very careful
// when using non-empty maps or slices as defaults, the result may not be what you want.
func New{{$P.Name}}TypedSetting[T any](key Key, def T, description string) {{$P.Name}}TypedSetting[T] {
s := {{$P.Name}}TypedSetting[T]{
key: key,
Expand All @@ -50,10 +51,10 @@ func New{{$P.Name}}TypedSettingWithConverter[T any](key Key, convert func(any) (
}

// New{{$P.Name}}TypedSettingWithConstrainedDefault creates a setting with a compound default value.
func New{{$P.Name}}TypedSettingWithConstrainedDefault[T any](key Key, convert func(any) (T, error), cdef []TypedConstrainedValue[T], description string) {{$P.Name}}TypedSetting[T] {
s := {{$P.Name}}TypedSetting[T]{
func New{{$P.Name}}TypedSettingWithConstrainedDefault[T any](key Key, convert func(any) (T, error), cdef []TypedConstrainedValue[T], description string) {{$P.Name}}TypedConstrainedDefaultSetting[T] {
s := {{$P.Name}}TypedConstrainedDefaultSetting[T]{
key: key,
cdef: &cdef,
cdef: cdef,
convert: convert,
description: description,
}
Expand All @@ -68,6 +69,13 @@ func (s {{$P.Name}}TypedSetting[T]) Validate(v any) error {
return err
}

func (s {{$P.Name}}TypedConstrainedDefaultSetting[T]) Key() Key { return s.key }
func (s {{$P.Name}}TypedConstrainedDefaultSetting[T]) Precedence() Precedence { return Precedence{{$P.Name}} }
func (s {{$P.Name}}TypedConstrainedDefaultSetting[T]) Validate(v any) error {
_, err := s.convert(v)
return err
}

func (s {{$P.Name}}TypedSetting[T]) WithDefault(v T) {{$P.Name}}TypedSetting[T] {
newS := s
newS.def = v
Expand All @@ -92,6 +100,22 @@ func (s {{$P.Name}}TypedSetting[T]) Get(c *Collection) TypedPropertyFnWith{{$P.N
c,
s.key,
s.def,
s.convert,
prec,
)
}
}

{{if eq $P.Name "Global" -}}
func (s {{$P.Name}}TypedConstrainedDefaultSetting[T]) Get(c *Collection) TypedPropertyFn[T] {
{{- else -}}
func (s {{$P.Name}}TypedConstrainedDefaultSetting[T]) Get(c *Collection) TypedPropertyFnWith{{$P.Name}}Filter[T] {
{{- end}}
return func({{$P.GoArgs}}) T {
prec := {{$P.Expr}}
return matchAndConvertWithConstrainedDefault(
c,
s.key,
s.cdef,
s.convert,
prec,
Expand All @@ -113,7 +137,7 @@ func (s {{$P.Name}}TypedSetting[T]) Subscribe(c *Collection) TypedSubscribableWi
return func({{$P.GoArgs}}, callback func(T)) (T, func()) {
{{- end}}
prec := {{$P.Expr}}
return subscribe(c, s.key, s.def, s.cdef, s.convert, prec, callback)
return subscribe(c, s.key, s.def, s.convert, prec, callback)
}
}

Expand All @@ -127,6 +151,10 @@ func (s {{$P.Name}}TypedSetting[T]) dispatchUpdate(c *Collection, sub any, cvs [
)
}

func (s {{$P.Name}}TypedConstrainedDefaultSetting[T]) dispatchUpdate(c *Collection, sub any, cvs []ConstrainedValue) {
// can't subscribe to constrained default settings
}

{{if eq $P.Name "Global" -}}
func GetTypedPropertyFn[T any](value T) TypedPropertyFn[T] {
{{- else -}}
Expand All @@ -138,12 +166,13 @@ func GetTypedPropertyFnFilteredBy{{$P.Name}}[T any](value T) TypedPropertyFnWith
}
{{else -}}
type {{$P.Name}}{{$T.Name}}Setting = {{$P.Name}}TypedSetting[{{$T.GoType}}]
type {{$P.Name}}{{$T.Name}}ConstrainedDefaultSetting = {{$P.Name}}TypedConstrainedDefaultSetting[{{$T.GoType}}]

func New{{$P.Name}}{{$T.Name}}Setting(key Key, def {{$T.GoType}}, description string) {{$P.Name}}{{$T.Name}}Setting {
return New{{$P.Name}}TypedSettingWithConverter[{{$T.GoType}}](key, convert{{$T.Name}}, def, description)
}

func New{{$P.Name}}{{$T.Name}}SettingWithConstrainedDefault(key Key, cdef []TypedConstrainedValue[{{$T.GoType}}], description string) {{$P.Name}}{{$T.Name}}Setting {
func New{{$P.Name}}{{$T.Name}}SettingWithConstrainedDefault(key Key, cdef []TypedConstrainedValue[{{$T.GoType}}], description string) {{$P.Name}}{{$T.Name}}ConstrainedDefaultSetting {
return New{{$P.Name}}TypedSettingWithConstrainedDefault[{{$T.GoType}}](key, convert{{$T.Name}}, cdef, description)
}

Expand All @@ -164,4 +193,4 @@ func Get{{$T.Name}}PropertyFnFilteredBy{{$P.Name}}(value {{$T.GoType}}) {{$T.Nam
{{- end}}
{{end }}{{/* if $T.IsGeneric */}}
{{- end}}{{/* range $T :=.Types */}}
{{- end}}{{/* range $P := $Precedences */}}
{{- end}}{{/* range $P := $Precedences */}}
58 changes: 0 additions & 58 deletions common/dynamicconfig/cachedvalue.go

This file was deleted.

5 changes: 5 additions & 0 deletions common/dynamicconfig/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type (
// Note that GetValue is called very often! You should not synchronously call out to an
// external system. Instead you should keep a set of all configured values, refresh it
// periodically or when notified, and only do in-memory lookups inside of GetValue.
//
// Implementations should prefer to return the same slice in response to the same key
// as long as the value hasn't changed. Value conversions are cached using weak
// pointers into the returned slice, so new slices will result in unnecessary calls to
// conversion functions.
GetValue(key Key) []ConstrainedValue
}

Expand Down
Loading
Loading