-
Notifications
You must be signed in to change notification settings - Fork 57
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
PythonMutator: support omitempty in PyDABs #1513
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b1d8b59
PythonMutator: support omitempty in PyDABs
kanterov 7a3e961
Use err to undo deletes
kanterov 370b286
Fix tests
kanterov 838c116
fmt
kanterov 214577b
Update comment
kanterov f067029
fmt
kanterov d58f374
more fmt
kanterov 0ffa1b6
Remove redundant assignment
kanterov f3b352e
Merge branch 'main' into python-mutator-ommitempty
kanterov dd4583e
Address more comments
kanterov 03994b6
Update libs/dyn/merge/override.go
kanterov e575c97
Use switch
kanterov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -313,6 +313,10 @@ func createLoadOverrideVisitor(ctx context.Context) merge.OverrideVisitor { | |
|
||
return merge.OverrideVisitor{ | ||
VisitDelete: func(valuePath dyn.Path, left dyn.Value) error { | ||
if isOmitemptyDelete(left) { | ||
return merge.ErrOverrideUndoDelete | ||
} | ||
|
||
return fmt.Errorf("unexpected change at %q (delete)", valuePath.String()) | ||
}, | ||
VisitInsert: func(valuePath dyn.Path, right dyn.Value) (dyn.Value, error) { | ||
|
@@ -346,6 +350,10 @@ func createInitOverrideVisitor(ctx context.Context) merge.OverrideVisitor { | |
|
||
return merge.OverrideVisitor{ | ||
VisitDelete: func(valuePath dyn.Path, left dyn.Value) error { | ||
if isOmitemptyDelete(left) { | ||
return merge.ErrOverrideUndoDelete | ||
} | ||
|
||
if !valuePath.HasPrefix(jobsPath) { | ||
return fmt.Errorf("unexpected change at %q (delete)", valuePath.String()) | ||
} | ||
|
@@ -382,6 +390,27 @@ func createInitOverrideVisitor(ctx context.Context) merge.OverrideVisitor { | |
} | ||
} | ||
|
||
func isOmitemptyDelete(left dyn.Value) bool { | ||
// PyDABs can omit empty sequences/mappings in output, because we don't track them as optional, | ||
// there is no semantic difference between empty and missing, so we keep them as they were before | ||
// PyDABs deleted them. | ||
|
||
if left.Kind() == dyn.KindMap && left.MustMap().Len() == 0 { | ||
return true | ||
} | ||
|
||
if left.Kind() == dyn.KindSequence && len(left.MustSequence()) == 0 { | ||
return true | ||
} | ||
|
||
// map/sequence can be nil, for instance, bad YAML like: `foo:<eof>` | ||
if left.Kind() == dyn.KindNil { | ||
return true | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A switch/case would be cleaner here, IMO. |
||
return false | ||
} | ||
|
||
// interpreterPath returns platform-specific path to Python interpreter in the virtual environment. | ||
func interpreterPath(venvPath string) string { | ||
if runtime.GOOS == "windows" { | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
omitempty
is specific to the JSON serializer. Here we check if the incoming value is an empty collection.Maybe
isEmptyCollection
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was intentional because we want to reserve this method to specific behavior with omit empty and add a comment explaining that. For instance, we intentionally don't handle objects where all fields are empty.