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

Add merge.Override transform #1428

Merged
merged 2 commits into from
May 17, 2024
Merged

Conversation

kanterov
Copy link
Contributor

@kanterov kanterov commented May 13, 2024

Changes

Add merge.Override transform. It allows the override one dyn.Value with another, preserving source locations for parts of the sub-tree where nothing has changed. This is different from merging, where values are concatenated.

OverrideVisitor is visiting the changes during the override process and allows to control of what changes are allowed or update the effective value.

The primary use case is Python code updating bundle configuration.

During override, we update locations only for changed values. This allows us to keep track of locations where values were initially defined and used for error reporting. For instance, merging:

resources:               # location=left.yaml:0
  jobs:                  # location=left.yaml:1
    job_0:               # location=left.yaml:2
      name: "job_0"      # location=left.yaml:3

with

resources:               # location=right.yaml:0
  jobs:                  # location=right.yaml:1
    job_0:               # location=right.yaml:2
      name: "job_0"      # location=right.yaml:3
      description: job 0 # location=right.yaml:4
    job_1:               # location=right.yaml:5
      name: "job_1"      # location=right.yaml:5

produces

resources:               # location=left.yaml:0
  jobs:                  # location=left.yaml:1
    job_0:               # location=left.yaml:2
      name: "job_0"      # location=left.yaml:3
      description: job 0 # location=right.yaml:4
    job_1:               # location=right.yaml:5
      name: "job_1"      # location=right.yaml:5

Tests

Unit tests

@codecov-commenter
Copy link

codecov-commenter commented May 13, 2024

Codecov Report

Attention: Patch coverage is 75.26882% with 23 lines in your changes are missing coverage. Please review.

Project coverage is 55.10%. Comparing base (e22dd8a) to head (2ca1c25).
Report is 106 commits behind head on main.

Files Patch % Lines
libs/dyn/merge/override.go 75.26% 13 Missing and 10 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1428      +/-   ##
==========================================
+ Coverage   52.25%   55.10%   +2.84%     
==========================================
  Files         317      346      +29     
  Lines       18004    15785    -2219     
==========================================
- Hits         9408     8698     -710     
+ Misses       7903     6294    -1609     
- Partials      693      793     +100     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@pietern pietern left a comment

Choose a reason for hiding this comment

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

This is generic functionality so should belong somewhere in libs/dyn. It could be part of the merge package perhaps?

bundle/config/mutator/python/override.go Outdated Show resolved Hide resolved
bundle/config/mutator/python/override.go Outdated Show resolved Hide resolved
bundle/config/mutator/python/override.go Outdated Show resolved Hide resolved
}
}

return merged, modified, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

This hook is executed only for the top level value, not for inner values. Is this intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, because we want to handle top-level value from inner values differently. E.g., appending to top-level is allowed during pre-init, but appending to nested field is not allowed, because it will mutate existing resource.

bundle/config/mutator/python/override.go Outdated Show resolved Hide resolved
bundle/config/mutator/python/override.go Outdated Show resolved Hide resolved
bundle/config/mutator/python/override.go Outdated Show resolved Hide resolved
// 'VisitInsert' is called when a new value is added to mapping or sequence
// 'VisitUpdate' is called when a leaf value is updated
type OverrideVisitor struct {
VisitDelete func(valuePath dyn.Path, left dyn.Value) error
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can allow to "undelete" value here, but there is no use-case for it right now.

@pietern pietern self-requested a review May 16, 2024 08:17
Copy link
Contributor

@pietern pietern left a comment

Choose a reason for hiding this comment

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

Looks great!

Haven't reviewed the tests yet, doing that later today.

libs/dyn/merge/override.go Show resolved Hide resolved
libs/dyn/merge/override.go Outdated Show resolved Hide resolved
return dyn.InvalidValue, err
}

return dyn.NewValue(merged, left.Location()), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing call to the update visitor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is very intentional, I can add a comment. We don't know if the value was changed or not. That's why we call "update" only on leafs

libs/dyn/merge/override.go Outdated Show resolved Hide resolved
@pietern pietern self-requested a review May 16, 2024 08:29
Copy link
Contributor

@pietern pietern left a comment

Choose a reason for hiding this comment

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

Very nice!

Please update the PR description (and title perhaps) before merging.

libs/dyn/merge/override.go Outdated Show resolved Hide resolved
@kanterov kanterov changed the title Implement 'override' transformation Add merge.Override transformation May 16, 2024
@kanterov kanterov changed the title Add merge.Override transformation Add merge.Override transformat May 16, 2024
@kanterov kanterov changed the title Add merge.Override transformat Add merge.Override transform May 16, 2024
@kanterov
Copy link
Contributor Author

@pietern Thanks. I've updated the code, PR description, and title. Can you add it to the merge queue?

@pietern
Copy link
Contributor

pietern commented May 17, 2024

Yes, everything looks good!

Looking at the code coverage, could you add/modify tests to make sure we cover the error cases as well? I think the table test can be modified to run twice for every example, one like it does now, and another one where the visitor returns an error to confirm it actually bubbles up to the caller.

@pietern pietern added this pull request to the merge queue May 17, 2024
@pietern pietern removed this pull request from the merge queue due to a manual request May 17, 2024
@pietern pietern changed the title Add merge.Override transform Add merge.Override transform May 17, 2024
@pietern pietern added this pull request to the merge queue May 17, 2024
Merged via the queue into databricks:main with commit 04e56aa May 17, 2024
5 checks passed
pietern added a commit that referenced this pull request May 22, 2024
CLI:
 * Add line about Docker installation to README.md ([#1363](#1363)).
 * Improve token refresh flow ([#1434](#1434)).

Bundles:
 * Upgrade Terraform provider to v1.42.0 ([#1418](#1418)).
 * Upgrade Terraform provider to v1.43.0 ([#1429](#1429)).
 * Don't merge-in remote resources during deployments ([#1432](#1432)).
 * Remove dependency on `ConfigFilePath` from path translation mutator ([#1437](#1437)).
 * Add `merge.Override` transform ([#1428](#1428)).
 * Fixed panic when loading incorrectly defined jobs ([#1402](#1402)).
 * Add more tests for `merge.Override` ([#1439](#1439)).
 * Fixed seg fault when specifying environment key for tasks ([#1443](#1443)).
 * Fix conversion of zero valued scalar pointers to a dynamic value ([#1433](#1433)).

Internal:
 * Don't hide commands of services that are already hidden ([#1438](#1438)).

API Changes:
 * Renamed `lakehouse-monitors` command group to `quality-monitors`.
 * Added `apps` command group.
 * Renamed `csp-enablement` command group to `compliance-security-profile`.
 * Renamed `esm-enablement` command group to `enhanced-security-monitoring`.
 * Added `databricks vector-search-indexes scan-index` command.

OpenAPI commit 7eb5ad9a2ed3e3f1055968a2d1014ac92c06fe92 (2024-05-21)

Dependency updates:
 * Bump golang.org/x/text from 0.14.0 to 0.15.0 ([#1419](#1419)).
 * Bump golang.org/x/oauth2 from 0.19.0 to 0.20.0 ([#1421](#1421)).
 * Bump golang.org/x/term from 0.19.0 to 0.20.0 ([#1422](#1422)).
 * Bump github.com/databricks/databricks-sdk-go from 0.39.0 to 0.40.1 ([#1431](#1431)).
 * Bump github.com/fatih/color from 1.16.0 to 1.17.0 ([#1441](#1441)).
 * Bump github.com/hashicorp/terraform-json from 0.21.0 to 0.22.1 ([#1440](#1440)).
 * Bump github.com/hashicorp/terraform-exec from 0.20.0 to 0.21.0 ([#1442](#1442)).
 * Update Go SDK to v0.41.0 ([#1445](#1445)).
@pietern pietern mentioned this pull request May 22, 2024
github-merge-queue bot pushed a commit that referenced this pull request May 22, 2024
CLI:
* Add line about Docker installation to README.md
([#1363](#1363)).
* Improve token refresh flow
([#1434](#1434)).

Bundles:
* Upgrade Terraform provider to v1.42.0
([#1418](#1418)).
* Upgrade Terraform provider to v1.43.0
([#1429](#1429)).
* Don't merge-in remote resources during deployments
([#1432](#1432)).
* Remove dependency on `ConfigFilePath` from path translation mutator
([#1437](#1437)).
* Add `merge.Override` transform
([#1428](#1428)).
* Fixed panic when loading incorrectly defined jobs
([#1402](#1402)).
* Add more tests for `merge.Override`
([#1439](#1439)).
* Fixed seg fault when specifying environment key for tasks
([#1443](#1443)).
* Fix conversion of zero valued scalar pointers to a dynamic value
([#1433](#1433)).

Internal:
* Don't hide commands of services that are already hidden
([#1438](#1438)).

API Changes:
 * Renamed `lakehouse-monitors` command group to `quality-monitors`.
 * Added `apps` command group.
* Renamed `csp-enablement` command group to
`compliance-security-profile`.
* Renamed `esm-enablement` command group to
`enhanced-security-monitoring`.
 * Added `databricks vector-search-indexes scan-index` command.

OpenAPI commit 7eb5ad9a2ed3e3f1055968a2d1014ac92c06fe92 (2024-05-21)

Dependency updates:
* Bump golang.org/x/text from 0.14.0 to 0.15.0
([#1419](#1419)).
* Bump golang.org/x/oauth2 from 0.19.0 to 0.20.0
([#1421](#1421)).
* Bump golang.org/x/term from 0.19.0 to 0.20.0
([#1422](#1422)).
* Bump github.com/databricks/databricks-sdk-go from 0.39.0 to 0.40.1
([#1431](#1431)).
* Bump github.com/fatih/color from 1.16.0 to 1.17.0
([#1441](#1441)).
* Bump github.com/hashicorp/terraform-json from 0.21.0 to 0.22.1
([#1440](#1440)).
* Bump github.com/hashicorp/terraform-exec from 0.20.0 to 0.21.0
([#1442](#1442)).
* Update Go SDK to v0.41.0
([#1445](#1445)).
Sign up for free to 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.

3 participants