Skip to content

Commit

Permalink
transform-field-names: Skip field if old name is the same as new name
Browse files Browse the repository at this point in the history
Resolves #37
  • Loading branch information
joverlee521 committed May 28, 2024
1 parent 10c7376 commit 1f9a382
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 1 addition & 2 deletions tests/transform-field-names/transform-field-names.t
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
Verify behavior of `transform-field-names`

If the `--field-map` includes a old field name that is the same as the new field
name, this prints a loud warning.
name, this should be no-op.

$ echo '{"strain": "A"}' \
> | $TESTDIR/../../transform-field-names \
> --field-map "strain=strain"
WARNING: skipping rename of strain because record already has a field named strain.
{"strain":"A"}

If the `--field-map` overwrites an existing field, then skip renaming and
Expand Down
9 changes: 7 additions & 2 deletions transform-field-names
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ if __name__ == '__main__':
parser.add_argument("--field-map", nargs="+",
help="Fields names in the NDJSON record mapped to new field names, " +
"formatted as '{old_field_name}={new_field_name}'. " +
"If the old field does not exist in record, the new field will be added with an empty string value." +
"If the new field already exists in record, then the renaming of the old field will be skipped.")
"If the old field does not exist in record, the new field will be added with an empty string value. " +
"If the new field already exists in record, then the renaming of the old field will be skipped. " +
"Skips the field if the old field name is the same as the new field name (case-sensitive).")
parser.add_argument("--force", action="store_true",
help="Force renaming of old field even if the new field already exists. " +
"Please keep in mind this will overwrite the value of the new field.")
Expand All @@ -27,6 +28,10 @@ if __name__ == '__main__':
field_map = {}
for field in args.field_map:
old_name, new_name = field.split('=')

if old_name == new_name:
continue

field_map[old_name] = new_name

for record in stdin:
Expand Down

0 comments on commit 1f9a382

Please sign in to comment.