mahmoud / glom

☄️ Python's nested data operator (and CLI), for all your declarative restructuring needs. Got data? Glom it! ☄️
https://glom.readthedocs.io
Other
1.88k stars 61 forks source link

Add --scalar flag to CLI #280

Open mahmoud opened 8 months ago

mahmoud commented 8 months ago

I was using glom in a script this week, specifically to extract single values from API calls. I noticed I kept having to trim the quotes and whitespace off of the JSON output, because even when it's a single value, it's quoted and followed by a newline.

So this feature strips the quotes and whitespace when passed a new flag: --scalar. This is only done when the glom result is not a collection. If it is, it falls back to normal json output.

Some items to consider:

  1. Name?
    • I have SQLAlchemy 2.0 on the brain and scalar is everywhere. But also boltons has an is_scalar function that maps exactly to this, so even if it's jargon-y, I think it's accurate.
  2. Should it be an error if the output is not a single value?
    • I think for my use case the ergonomics would be worse.
  3. Right now, output is always json. Would it be better to do --output "json"|"scalar"?
    • Kinda don't want to get into the business of emitting yaml, etc.?

None of these are blockers, I am good to merge as is.

codecov[bot] commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (2e0c552) 98.05% compared to head (eed98dd) 98.05%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #280 +/- ## ======================================= Coverage 98.05% 98.05% ======================================= Files 27 27 Lines 4362 4371 +9 Branches 765 766 +1 ======================================= + Hits 4277 4286 +9 Misses 54 54 Partials 31 31 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.