Skip to content
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

Channel default value by changing default logic #206

Merged
merged 1 commit into from
Jul 16, 2024

Conversation

oxzi
Copy link
Member

@oxzi oxzi commented May 29, 2024

In a nutshell, the newly introduced plugin.PopulateDefaults function
populates all fields of a Plugin-implementing struct with those fields
from Info.ConfigAttributes where ConfigOption.Default is set. Thus, a
single function call before parsing the user-submitted configuration
within the Plugin.SetConfig method sets default values.

As a result of the discussion between the Go and the Web team,
summarized in #205, Web does not store key-value pairs to be omitted.

Prior, an already JSON-encoded version of the ConfigOption slice was
present in plugin.Info. Thus, this data wasn't easily available anymore.
As the new code now needs to access this field, it was changed and a
custom sql driver.Valuer was implemented for a slice type.

While working on this, all ConfigOptions were put in the same order as
the struct fields.

Requires Icinga/icinga-notifications-web#230.

Closes #205.

@oxzi oxzi requested review from julianbrost and yhabteab May 29, 2024 10:10
@cla-bot cla-bot bot added the cla/signed CLA is signed by all contributors of a PR label May 29, 2024
@oxzi oxzi force-pushed the channel-cfg-default-i205 branch from d7873da to fcf8531 Compare May 29, 2024 10:15
@oxzi oxzi marked this pull request as ready for review May 29, 2024 10:17
pkg/plugin/plugin.go Outdated Show resolved Hide resolved
pkg/plugin/plugin.go Outdated Show resolved Hide resolved
pkg/plugin/plugin.go Outdated Show resolved Hide resolved
pkg/plugin/plugin_test.go Outdated Show resolved Hide resolved
cmd/channel/email/main_test.go Outdated Show resolved Hide resolved
pkg/plugin/plugin.go Outdated Show resolved Hide resolved
@oxzi oxzi force-pushed the channel-cfg-default-i205 branch from fcf8531 to 7ff9196 Compare May 31, 2024 11:07
@oxzi oxzi requested a review from yhabteab May 31, 2024 11:07
yhabteab
yhabteab previously approved these changes May 31, 2024
Copy link
Member

@yhabteab yhabteab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LFTM!

@oxzi
Copy link
Member Author

oxzi commented Jun 3, 2024

Rebased after switch to icinga-go-library.

yhabteab
yhabteab previously approved these changes Jun 4, 2024
@julianbrost
Copy link
Collaborator

To avoid storing each default value twice, it's location was moved up to a "default" struct tag. By utilizing the already included defaults library and adding a small wrapper for plugin.Info, those defaults are now being set to the plugin.ConfigAttributes at runtime.

Have you considered doing it the other way around, i.e. applying the defaults from ConfigAttributes to the struct?

I think that this can be done in a much simpler way, without any custom use of reflection even: Iterate over ConfigAttributes and populate a map[string]any with the defaults (basically, you're already doing this, but it's more difficult to extract the information from the field tags). Then JSON-encode that map, in other words: generate a config like it would be written to the database when it would contain exactly the defaults. Then simply unmarshal it into the config struct before unmarshaling the config from the database.

@oxzi oxzi force-pushed the channel-cfg-default-i205 branch 2 times, most recently from 0dbbfd9 to 0ce82f4 Compare June 13, 2024 10:10
@oxzi
Copy link
Member Author

oxzi commented Jun 13, 2024

I liked @julianbrost's suggestion and worked on it. The new design reduces the change's size and is, imo, more readable. Please take another look.

@oxzi oxzi requested a review from yhabteab June 13, 2024 10:12
julianbrost
julianbrost previously approved these changes Jun 13, 2024
Copy link
Collaborator

@julianbrost julianbrost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code change looks fine (and quite a lot nicer than the previous version IMHO), actually using default values by default sounds sane, did not break anything in my test setup including after saving the config from web again, so looks fine to me. Nonetheless, @yhabteab please also have another look at this as you were involved in the discussions mentioned in #205.

@yhabteab
Copy link
Member

As a result of the discussion between the Go daemon team and the web
team, summarized in #205, web does not store or stores default values as
JSON "null" values. The Go JSON unmarshaller does not overwrite existing
field values for JSON nulls.

I'm a bit confused then, given that unmarshaling a JSON null value into a GO scalar type has no effect, how does the user clear optional default values then? Web does store null values to signify that a user does not want to use a default value of that field (if any). {"sender_name":"Foo","sender_mail":null,"host":"localhost","port":"25","encryption":"none"}

  • a null value results in an empty value for optional key value pairs.

Or to be more specific, how do you deal with the second point of the referenced issue?

@oxzi
Copy link
Member Author

oxzi commented Jul 15, 2024

For the record, as this changes the channel logic a bit and introduces another helper function, it should either be included in #236 if this got merged first or be an update in this PR otherwise.

@oxzi oxzi force-pushed the channel-cfg-default-i205 branch from 0ce82f4 to 8b8bd63 Compare July 15, 2024 12:36
@oxzi oxzi marked this pull request as draft July 15, 2024 12:36
In a nutshell, the newly introduced plugin.PopulateDefaults function
populates all fields of a Plugin-implementing struct with those fields
from Info.ConfigAttributes where ConfigOption.Default is set. Thus, a
single function call before parsing the user-submitted configuration
within the Plugin.SetConfig method sets default values.

As a result of the discussion between the Go and the Web team,
summarized in #205, Web does not store key-value pairs to be omitted.

Prior, an already JSON-encoded version of the ConfigOption slice was
present in plugin.Info. Thus, this data wasn't easily available anymore.
As the new code now needs to access this field, it was changed and a
custom sql driver.Valuer was implemented for a slice type.

While working on this, all ConfigOptions were put in the same order as
the struct fields.

Requires <Icinga/icinga-notifications-web#230>.

Closes #205.
@oxzi oxzi force-pushed the channel-cfg-default-i205 branch from 8b8bd63 to 53448cb Compare July 15, 2024 14:52
@oxzi oxzi marked this pull request as ready for review July 15, 2024 14:52
@julianbrost julianbrost merged commit 3989f71 into main Jul 16, 2024
12 checks passed
@julianbrost julianbrost deleted the channel-cfg-default-i205 branch July 16, 2024 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla/signed CLA is signed by all contributors of a PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix default value behavior for channel plugins
3 participants