-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Dynamic config conversion improvements #7052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
33d8eec
2d6ae44
f16d3d6
db02895
0a0ca55
df91d43
f67fc7a
a8d5b75
0bbc7fd
fb25934
54da20e
46c63e7
e67c1e3
bad8380
333be62
02b0f22
5dc95af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,10 +155,11 @@ func generateType(w io.Writer, tp *settingType, prec *settingPrecedence) { | |
| writeTemplatedCode(w, ` | ||
| {{ 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, | ||
|
|
@@ -183,10 +184,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, | ||
| } | ||
|
|
@@ -201,6 +202,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 | ||
|
|
@@ -225,6 +233,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, | ||
|
|
@@ -246,7 +270,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) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -260,6 +284,16 @@ 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) { | ||
| dispatchUpdate( | ||
| c, | ||
| s.key, | ||
| s.convert, | ||
| sub.(*subscription[T]), | ||
| cvs, | ||
| ) | ||
| } | ||
|
|
||
| {{if eq .P.Name "Global" -}} | ||
| func GetTypedPropertyFn[T any](value T) TypedPropertyFn[T] { | ||
| {{- else -}} | ||
|
|
@@ -271,12 +305,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) | ||
| } | ||
|
|
||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if we can runtime assert this and prevent unwanted defaults. It's really easy for devs to miss docstring comments and reviewers will likely not be aware of this detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree we should. I can add a task for it.