-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass copy of
dyn.Path
to callback function (#1747)
## Changes Some call sites hold on to the `dyn.Path` provided to them by the callback. It must therefore never be mutated after the callback returns, or these mutations leak out into unknown scope. This change means it is no longer possible for this failure mode to happen. ## Tests Unit test.
- Loading branch information
Showing
7 changed files
with
45 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dyn_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/databricks/cli/libs/dyn" | ||
assert "github.com/databricks/cli/libs/dyn/dynassert" | ||
) | ||
|
||
func TestVisitCallbackPathCopy(t *testing.T) { | ||
vin := dyn.V(map[string]dyn.Value{ | ||
"foo": dyn.V(42), | ||
"bar": dyn.V(43), | ||
}) | ||
|
||
var paths []dyn.Path | ||
|
||
// The callback should receive a copy of the path. | ||
// If the same underlying value is used, all collected paths will be the same. | ||
// This test uses `MapByPattern` to collect all paths in the map. | ||
// Visit itself doesn't have public functions and we exclusively use black-box testing for this package. | ||
_, _ = dyn.MapByPattern(vin, dyn.NewPattern(dyn.AnyKey()), func(p dyn.Path, v dyn.Value) (dyn.Value, error) { | ||
paths = append(paths, p) | ||
return v, nil | ||
}) | ||
|
||
// Verify that the paths retained their original values. | ||
var strings []string | ||
for _, p := range paths { | ||
strings = append(strings, p.String()) | ||
} | ||
assert.ElementsMatch(t, strings, []string{ | ||
"foo", | ||
"bar", | ||
}) | ||
} |