-
Notifications
You must be signed in to change notification settings - Fork 266
feat: add --fields flag to filter JSON output #53
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
bf8f366
71fede1
1aabdc6
8ecbdcf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,13 +11,23 @@ pub(crate) mod series; | |
| pub(crate) mod sports; | ||
| pub(crate) mod tags; | ||
|
|
||
| use std::sync::OnceLock; | ||
|
|
||
| use chrono::{DateTime, Utc}; | ||
| use polymarket_client_sdk::types::Decimal; | ||
| use rust_decimal::prelude::ToPrimitive; | ||
| use serde_json::Value; | ||
| use tabled::Table; | ||
| use tabled::settings::object::Columns; | ||
| use tabled::settings::{Modify, Style, Width}; | ||
|
|
||
| /// field names to keep in JSON output; set once at startup via --fields | ||
| static JSON_FIELDS: OnceLock<Vec<String>> = OnceLock::new(); | ||
|
|
||
| pub(crate) fn set_json_fields(fields: Vec<String>) { | ||
| let _ = JSON_FIELDS.set(fields); | ||
| } | ||
|
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. Silent failure in
|
||
|
|
||
| pub(crate) const DASH: &str = "—"; | ||
|
|
||
| #[derive(Clone, Copy, Debug, clap::ValueEnum)] | ||
|
|
@@ -63,10 +73,32 @@ pub(crate) fn active_status(closed: Option<bool>, active: Option<bool>) -> &'sta | |
| } | ||
|
|
||
| pub(crate) fn print_json(data: &(impl serde::Serialize + ?Sized)) -> anyhow::Result<()> { | ||
| println!("{}", serde_json::to_string_pretty(data)?); | ||
| let value = serde_json::to_value(data)?; | ||
| let output = match JSON_FIELDS.get() { | ||
| Some(fields) => filter_fields(value, fields), | ||
| None => value, | ||
| }; | ||
| println!("{}", serde_json::to_string_pretty(&output)?); | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// keep only the requested keys from an object or each object in an array | ||
| fn filter_fields(value: Value, fields: &[String]) -> Value { | ||
| match value { | ||
| Value::Array(arr) => { | ||
| Value::Array(arr.into_iter().map(|v| filter_fields(v, fields)).collect()) | ||
| } | ||
| Value::Object(map) => { | ||
| let filtered = map | ||
| .into_iter() | ||
| .filter(|(k, _)| fields.iter().any(|f| f == k)) | ||
| .collect(); | ||
| Value::Object(filtered) | ||
| } | ||
| other => other, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn print_error(error: &anyhow::Error, format: OutputFormat) { | ||
| match format { | ||
| OutputFormat::Json => { | ||
|
|
@@ -185,4 +217,36 @@ mod tests { | |
| fn format_decimal_just_below_million_uses_k() { | ||
| assert_eq!(format_decimal(dec!(999_999)), "$1000.0K"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn filter_fields_keeps_only_requested_keys() { | ||
| let obj = serde_json::json!({"a": 1, "b": 2, "c": 3}); | ||
| let fields = vec!["a".into(), "c".into()]; | ||
| let result = filter_fields(obj, &fields); | ||
| assert_eq!(result, serde_json::json!({"a": 1, "c": 3})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn filter_fields_applies_to_each_array_element() { | ||
| let arr = serde_json::json!([{"a": 1, "b": 2}, {"a": 3, "b": 4}]); | ||
| let fields = vec!["a".into()]; | ||
| let result = filter_fields(arr, &fields); | ||
| assert_eq!(result, serde_json::json!([{"a": 1}, {"a": 3}])); | ||
| } | ||
|
|
||
| #[test] | ||
| fn filter_fields_returns_empty_object_when_no_match() { | ||
| let obj = serde_json::json!({"a": 1, "b": 2}); | ||
| let fields = vec!["z".into()]; | ||
| let result = filter_fields(obj, &fields); | ||
| assert_eq!(result, serde_json::json!({})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn filter_fields_passes_through_non_object_values() { | ||
| let val = serde_json::json!(42); | ||
| let fields = vec!["a".into()]; | ||
| let result = filter_fields(val, &fields); | ||
| assert_eq!(result, serde_json::json!(42)); | ||
| } | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.