Skip to content

Add NewPatternFromString and bundle.SetDefault; use it for volumes & dashboards - #2734

Merged
denik merged 14 commits into
mainfrom
denik/set-defaults
Apr 22, 2025
Merged

Add NewPatternFromString and bundle.SetDefault; use it for volumes & dashboards#2734
denik merged 14 commits into
mainfrom
denik/set-defaults

Conversation

@denik

@denikdenik commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

Changes

  • New dyn.NewPatternFromString to construct pattern from strings.
  • New bundle.SetDefault() for easier setting of defaults.
  • Replace configure_dashboards_defaults.go and configure_volume_defaults.go with new this new helper.

There could be slight behaviour change: we no longer ignore IsCannotTraverseNilError error. Per team discussions, null should not be set in yaml or python.

Why

  • It's more declarative, so less error-prone.
  • The string syntax for patterns is easier to read (and write): "resources.dashboards.*.parent_path" vs dyn.NewPattern(dyn.Key("resources"), dyn.Key("dashboards"), dyn.AnyKey(), dyn.Key("parent_path"))
  • We're going to migrate many defaults from terraform, so need a compact representation.

Tests

Unit tests for old mutators were converted to acceptance tests in #2733

@denik
deniktemporarily deployed to test-trigger-is April 16, 2025 13:53 — with GitHub Actions Inactive
@denik
denikforce-pushed the denik/set-defaults branch from 5613d9e to e2ba197CompareApril 16, 2025 13:55
@denik
deniktemporarily deployed to test-trigger-is April 16, 2025 13:55 — with GitHub Actions Inactive
@denik
denik changed the base branch from main to denik/dashboard-defaults-testApril 16, 2025 13:55
Comment threadbundle/set_default.go Outdated
@denikdenik changed the title Add SetDefault helper for declarative defaultsAdd SetDefault helper for declarative defaults; use it in volumes & dashboardsApr 16, 2025
denik added a commit that referenced this pull request Apr 16, 2025
)
## Changes
- Replace configure_dashboard_defaults_test.go with acceptance test
- Replace configure_volume_defaults_test.go with acceptance test
## Why
Going to refactor default machinery:
#2734
Base automatically changed from denik/dashboard-defaults-test to mainApril 16, 2025 14:14
@denik
denikforce-pushed the denik/set-defaults branch from e2ba197 to 7381948CompareApril 16, 2025 14:15
@denik
deniktemporarily deployed to test-trigger-is April 16, 2025 14:15 — with GitHub Actions Inactive
// OK, we'll set the default value.
break
case dyn.IsCannotTraverseNilError(err):
// Cannot traverse the value, skip it.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I did not copy this because we discussed that nils should be hard errors. cc @pietern

@denik
denik requested a review from kanterovApril 16, 2025 14:18
Comment threadbundle/set_default.go Outdated
Comment threadbundle/set_default.go Outdated

func SetDefault(ctx context.Context, b *Bundle, value any, pattern ...string) diag.Diagnostics {
if len(pattern) <= 2 {
return diag.FromErr(fmt.Errorf("Internal error: invalid pattern, not enough components: %#v", pattern))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suggest to remove Internal error part

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This is to signal that the error is not with the user input.

Comment threadbundle/set_default.go Outdated
Comment threadbundle/set_default.go
Comment threadbundle/set_default.go Outdated
Comment threadbundle/set_default.go
@denik
deniktemporarily deployed to test-trigger-is April 16, 2025 15:50 — with GitHub Actions Inactive
@denikdenik changed the title Add SetDefault helper for declarative defaults; use it in volumes & dashboardsAdd NewPatternFromString and bundle.SetDefault; use it for volumes & dashboardsApr 16, 2025
@denik
deniktemporarily deployed to test-trigger-is April 16, 2025 15:56 — with GitHub Actions Inactive
@denik
denik requested a review from pieternApril 16, 2025 16:02

@pieternpietern left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For future PRs, would be nice to split the dyn changes to keep it self-contained.

Comment threadlibs/dyn/path_string_test.go
Comment threadlibs/dyn/pattern.go Outdated
Comment threadlibs/dyn/pattern_string.go Outdated
Comment threadlibs/dyn/pattern_string.go
Comment threadbundle/set_default.go
_, err := dyn.GetByPath(v, m.key)
switch {
case dyn.IsNoSuchKeyError(err):
return dyn.SetByPath(v, m.key, dyn.V(m.value))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In some cases this can still return a NoSuchKeyError.

If the Get fails with this error on a lower-level key, then setting it can still fail.

Consider foo.*.bar.qux. If foo.*.bar doesn't exist, we get the NoSuchKeyError. But the call to SetByPath also fails because it assumes the path leading up to the final component exists.

This is probably OK with the current patterns. But it's a risk, and we should have a hermetic definition of the behavior to future-proof it.

I now see the path is guaranteed to have a single component. Then it always works.

@pieternpietern left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I like the simplification here, but wonder if we need to keep the defaults inlined or split them out to a separate mutator with default in the name so it is easy to locate.

@denik
deniktemporarily deployed to test-trigger-is April 17, 2025 14:33 — with GitHub Actions Inactive
@denik
deniktemporarily deployed to test-trigger-is April 17, 2025 14:41 — with GitHub Actions Inactive
@denik
deniktemporarily deployed to test-trigger-is April 17, 2025 14:45 — with GitHub Actions Inactive
@denik

Copy link
Copy Markdown
ContributorAuthor

I like the simplification here, but wonder if we need to keep the defaults inlined or split them out to a separate mutator with default in the name so it is easy to locate.

Well what I'm trying to achieve there with this PR and comments PR before it is to being able to read the phase or a big chunk of a phase and understand what's happening. Having extra jump does not help with that.

That said, we can revisit this if it grows.

- // Reads (dynamic): resources.dashboards.* (checks for existing parent_path and embed_credentials)
- // Updates (dynamic): resources.dashboards.*.parent_path (sets to workspace.resource_path if not set)
- // Updates (dynamic): resources.dashboards.*.embed_credentials (sets to false if not set)
- // Reads (dynamic): resources.volumes.* (checks for existing volume_type)
- // Updates (dynamic): resources.volumes.*.volume_type (sets to "MANAGED" if not set)
+ {"resources.dashboards.*.parent_path", b.Config.Workspace.ResourcePath},
+ {"resources.dashboards.*.embed_credentials", false},
+ {"resources.volumes.*.volume_type", "MANAGED"},
+	}

@denik
denik requested a review from pieternApril 17, 2025 14:55
@denik
deniktemporarily deployed to test-trigger-is April 17, 2025 14:55 — with GitHub Actions Inactive
@denik
denik requested a review from andrewnesterApril 17, 2025 14:55
@denik
deniktemporarily deployed to test-trigger-is April 22, 2025 09:32 — with GitHub Actions Inactive
Comment threadbundle/config/mutator/resourcemutator/resource_mutator.go
@denik
deniktemporarily deployed to test-trigger-is April 22, 2025 14:17 — with GitHub Actions Inactive
@denik
denik enabled auto-merge April 22, 2025 14:17
@denik
denik requested a review from pieternApril 22, 2025 14:17
@denik
denik added this pull request to the merge queueApr 22, 2025
Merged via the queue into main with commit 412a1e5Apr 22, 2025
@denik
denik deleted the denik/set-defaults branch April 22, 2025 14:47
denik added a commit that referenced this pull request May 20, 2026
)
## Changes
- Replace configure_dashboard_defaults_test.go with acceptance test
- Replace configure_volume_defaults_test.go with acceptance test
## Why
Going to refactor default machinery:
#2734
denik added a commit that referenced this pull request May 20, 2026
…dashboards (#2734)
## Changes
- New dyn.NewPatternFromString to construct pattern from strings.
- New bundle.SetDefault() for easier setting of defaults.
- Replace configure_dashboards_defaults.go and
configure_volume_defaults.go with new this new helper.
There could be slight behaviour change: we no longer ignore
IsCannotTraverseNilError error. Per team discussions, null should not be
set in yaml or python.
## Why
- It's more declarative, so less error-prone.
- The string syntax for patterns is easier to read (and write):
`"resources.dashboards.*.parent_path"` vs
`dyn.NewPattern(dyn.Key("resources"), dyn.Key("dashboards"),
dyn.AnyKey(), dyn.Key("parent_path"))`
- We're going to migrate many defaults from terraform, so need a compact
representation.
## Tests
Unit tests for old mutators were converted to acceptance tests in
#2733
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@denik@pietern@kanterov@andrewnester