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

CustomDiffConfig and CustomCheckConfig #187

Open
blampe opened this issue Jan 25, 2024 · 1 comment
Open

CustomDiffConfig and CustomCheckConfig #187

blampe opened this issue Jan 25, 2024 · 1 comment
Labels
impact/usability Something that impacts users' ability to use the product easily and intuitively kind/enhancement Improvements or new features

Comments

@blampe
Copy link
Contributor

blampe commented Jan 25, 2024

We should allow the user to define custom config diff logic as well as CustomCheckConfig.

For diff, something like the below might work but is awkward. The resource controller defines Configure so it would be most natural for it to also handle DiffConfig. (This feels related to #159 because the controller doesn't currently have any type information about its configuration.)

diff --git a/infer/configuration.go b/infer/configuration.go
index 6fd15ac..6249f3f 100644
--- a/infer/configuration.go
+++ b/infer/configuration.go
@@ -62,6 +62,10 @@ type CustomConfigure interface {
        Configure(ctx p.Context) error
 }

+type CustomDiffConfig[T any] interface {
+       DiffConfig(ctx p.Context, olds T, news T) (p.DiffResponse, error)
+}
+
 type config[T any] struct{ t *T }

 func (*config[T]) underlyingType() reflect.Type {
@@ -144,6 +148,23 @@ func (c *config[T]) checkConfig(ctx p.Context, req p.CheckRequest) (p.CheckRespo

 func (c *config[T]) diffConfig(ctx p.Context, req p.DiffRequest) (p.DiffResponse, error) {
        c.ensure()
+
+       for _, ignoredChange := range req.IgnoreChanges {
+               req.News[ignoredChange] = req.Olds[ignoredChange]
+       }
+
+       var olds, news T
+       if _, err := ende.DecodeConfig(req.Olds, &olds); err != nil {
+               return p.DiffResponse{}, err
+       }
+       if _, err := ende.DecodeConfig(req.News, &news); err != nil {
+               return p.DiffResponse{}, err
+       }
+
+       if differ, ok := Config[T]().(CustomDiffConfig[T]); ok {
+               return differ.DiffConfig(ctx, olds, news)
+       }
+
        return diff[T, T, T](ctx, req, c.t, func(string) bool { return true })
 }
@blampe blampe added impact/usability Something that impacts users' ability to use the product easily and intuitively kind/enhancement Improvements or new features needs-triage Needs attention from the triage team labels Jan 25, 2024
@iwahbe
Copy link
Member

iwahbe commented Jan 25, 2024

Both of these are possible by implementing infer.CustomCheck and infer.CustomDiff on the config struct.

@mjeffryes mjeffryes removed the needs-triage Needs attention from the triage team label Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
impact/usability Something that impacts users' ability to use the product easily and intuitively kind/enhancement Improvements or new features
Projects
None yet
Development

No branches or pull requests

3 participants