Skip to content

Commit

Permalink
Merge pull request #38 from nextstrain/update-transform-field-names
Browse files Browse the repository at this point in the history
`transform-field-names`: Skip field if `old_field_name` == `new_field_name`
  • Loading branch information
joverlee521 authored May 28, 2024
2 parents 5b74edc + 1f9a382 commit 0f55d15
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
26 changes: 26 additions & 0 deletions tests/transform-field-names/transform-field-names.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 should be no-op.

$ echo '{"strain": "A"}' \
> | $TESTDIR/../../transform-field-names \
> --field-map "strain=strain"
{"strain":"A"}

If the `--field-map` overwrites an existing field, then skip renaming and
print a loud warning.

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

The `--field-map` may overwrite an existing field if using `--force` flag.

$ echo '{"strain": "A", "isolate": "B"}' \
> | $TESTDIR/../../transform-field-names \
> --field-map "isolate=strain" \
> --force
{"strain":"B"}
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 0f55d15

Please sign in to comment.